473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

input problem

hello all.
I am back again. i have a slight problem with input here. here's the
code compiled under the GNU standard c++ compiler. somehow,
getline(std::cin, string h) doesn't seem to be working. can anybody
please help me know what's happening and why it's happening? thanks.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
void save();
class std_employee
{
private:

int number;
string name;
string ans;

public:
std_employee()
{ system("CLS"); }

void disp_data()
{
cout<<"\nthe employee's name is: ";
cout<<name<<endl;
cout<<"\nthe employee's number is: ";
cout<<number<<endl;
}
void take_data()
{
cout<<"\nenter the employee name: ";
getline(cin,name);
cout<<"\nenter the employee's number: ";
cin>>number;
}
void store()
{
cout<<"\n\nsave file?"<<endl;
getline(cin,ans);
if(strcmp(ans,"yes") == 0)
{
save();
}
else
{
exit(0);
}
save();
}
};

void save()
{
std_employee emp1;
string file;
cout<<"save as?: ";
getline(cin,file);
ofstream out("test.txt");
out<<"it aint wurking";
out.close();
cout<<"\nsaved successfully.\n"<<endl;
}

int main()
{
std_employee emp1,*ptr;
ptr = &emp1;
ptr->take_data();
ptr->disp_data();
cin.get();
ptr->store();
return 0;
}

cheers

Apr 25 '07 #1
7 2895
On Wed, 25 Apr 2007 06:12:19 -0700, MC felon wrote:

[snip]

You appear to have posted the same message four times now (and received
several replies). I'm charitably assuming you have a problem with your
newsreader rather than having done this deliberately. If so, please try
and sort it out... if not then just don't do it.

--
Lionel B
Apr 25 '07 #2
"MC felon" writes:

I am back again. i have a slight problem with input here. here's the
code compiled under the GNU standard c++ compiler. somehow,
getline(std::cin, string h) doesn't seem to be working. can anybody
please help me know what's happening and why it's happening? thanks.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
void save();
class std_employee
{
private:

int number;
string name;
string ans;

public:
std_employee()
{ system("CLS"); }

void disp_data()
{
cout<<"\nthe employee's name is: ";
cout<<name<<endl;
cout<<"\nthe employee's number is: ";
cout<<number<<endl;
}
void take_data()
{
cout<<"\nenter the employee name: ";
getline(cin,name);
cout<<"\nenter the employee's number: ";
cin>>number;
}
void store()
{
cout<<"\n\nsave file?"<<endl;
getline(cin,ans);
if(strcmp(ans,"yes") == 0)
{
save();
}
else
{
exit(0);
}
save();
}
};

void save()
{
std_employee emp1;
string file;
cout<<"save as?: ";
getline(cin,file);
ofstream out("test.txt");
out<<"it aint wurking";
That looks like you wanted cout rather than out.

A description of what happens would be more useful than speculation on the
source of the trouble.
I have no reason to think this is related to your problem, just one thing I
noted.
out.close();
cout<<"\nsaved successfully.\n"<<endl;
}

int main()
{
std_employee emp1,*ptr;
ptr = &emp1;
ptr->take_data();
ptr->disp_data();
cin.get();
ptr->store();
return 0;
}

cheers

Apr 25 '07 #3
Sorry!
there was a problem with the server, i think.
anyway, i tried getline(). it's working with the cin.ignore() function
but there is a new problem.

[source]
getline(cin,ans);
if( ans == "yes")
{ //do something
}
else if(ans == "no")
{ //do something else
}
else
{
cout<<"invalid answer";
}

[/source]

when i type "yes" OR if i type "no", it says "invalid answer".
what's wrong?

Apr 26 '07 #4
MC felon <pa******@gmail.comwrites:
>getline(cin,ans);
if( ans == "yes")
...
>when i type "yes" OR if i type "no", it says "invalid answer".
what's wrong?
Well, I suggest you print out ans. Do it so that you can see if
there are trailing invisible characters - maybe
cout << "|" << ans << "|" << endl;

Apr 26 '07 #5
Well, I suggest you print out ans. Do it so that you can see if
there are trailing invisible characters - maybe
cout << "|" << ans << "|" << endl;
yes! i tried it. it prints out "es" for "yes". what's happening now?

Apr 26 '07 #6
"MC felon" writes:

Are you paying any attention at all to the answers you get? I think the
proper answer has been posted two times. If you don't understand the
answer, pursue it. Don't address your follow up to the guy who answered, he
may well be bored with you by now. I made one post, which did not have to
do with your current problem, but was still germane, and you ignored it.
You are convinced you know where the problem was, but your guess was
*wrong*. Wrong, wrong, wrong. I went back to your original post, and I
don't see why it even compiled. It won't comple for me. I see you have
been fiddling with the code, but not fixed it. Here is your modified
original code, I don't know if it works completely but it at least solves
your proximate problem. The STOP macro and cin.get are provided to humor my
compiler, you don't need them

#include <iostream>

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

You had <stdlib.h>. Don't mix the old and new stuff, it makes people
nervous.

using namespace std;
void save();
class std_employee
{
private:

int number;
string name;
string ans;

public:
std_employee()
{ system("CLS"); }

void disp_data()
{
cout<<"\nthe employee's name is: ";
cout<<name<<endl;
cout<<"\nthe employee's number is: ";
cout<<number<<endl;
}
void take_data()
{
cout<<"\nenter the employee name: ";
getline(cin,name);
cout<<"\nenter the employee's number: ";
cin>>number;
}
void store()
{
cout<<"\n\nsave file?"<<endl;
getline(cin,ans);

if(strcmp(ans.c_str(),"yes") == 0)

There are two kinds of stings in C++. C strings, grandfathered in, and C++
strings. They mix like oil and water. strcmp works with C strings but ans
is a C++ string. One way to fix it in this particular case is to use a
conversion function, as above.
if(

{
save();
}
else
{
exit(0);
}
save();
}
};

void save()
{
std_employee emp1;
string file;
cout<<"save as?: ";
getline(cin,file);
ofstream out("test.txt");
cout<<"it aint wurking";
Recognizing my earlier post.

Now about the *logic*. There is no logic there, is there? Test to see if
the file opened, if not, refuse to continue. I conclude this will not work
yet. It certainly will not work properly.

cin.get();
out.close();
cout<<"\nsaved successfully.\n"<<endl;
}


#define STOP while(1) cin.get();

int main()
{
cout << __TIME__ << endl;

std_employee emp1,*ptr;
ptr = &emp1;
ptr->take_data();
ptr->disp_data();
cin.get();
ptr->store();
return 0;
STOP;
}
Apr 26 '07 #7
I think there's complete logic in that code. i wanted to learn to
write objects to a file (a .txt). Therefore, i set up this object,
"out" of class "ofstream" (if i'm right) to buffer data to txt. you
mistake my "out" for a "cout". It is not so. i simply named an object
"out" to write to a file. I realized my mistake of using strcmp() for
the strings and i heard the '==' operator's been overloaded for this
very purpose. i have incorporated the required change in the code. It
is working well now. Thank you for helping me out!

Expect the felon to post more queries (which may appear mundane to the
professional's eye, but humour me, for the learner and the infernal
newbie that i am. I'm sure i'll cross this stage soon.).

thanks again to all!

cheers!

Apr 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kyle E | last post by:
Ok, I have a little tiny problem, a beginner problem, that I am overlooking. I am writing a Information Gathering Program, that takes user input from scripted questions and prints them in a handy...
2
by: Kai Grossjohann | last post by:
I would like to put a text input field (in the sense of <input type="text">) and an image next to each other, where I know the size in pixels of the image, and I know the total width in em. I...
37
by: Jason Heyes | last post by:
A pythagorean triple is a triple <a,b,c> whose components are positive integers satisfying a*a + b*b = c*c. An example is <3,4,5> since 3*3 + 4*4 = 9 + 16 = 25 = 5*5. I want to write a function...
2
by: SophistiCat | last post by:
Hi, I am working on a computational program that has to read a number of parameters (~50) from an input file. The program contains a single class hierarchy with about a dozen member-classes or...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
28
by: n00m | last post by:
Both codes below read the same huge(~35MB) text file. In the file 1000000 lines, the length of each line < 99 chars. Stable result: Python runs ~0.65s C : ~0.70s Any thoughts?
8
by: Bruce A. Julseth | last post by:
I'm "Failing" a trying to create a multicolum text input page. I have found many examples of two column pages where the first column is the field label and the second column is input text box. But,...
209
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.