input problem

M

MC felon

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
 
L

Lionel B

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.
 
O

osmium

:

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.
 
M

MC felon

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?
 
T

Tim Love

MC felon said:
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;
 
M

MC felon

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?
 
O

osmium

:

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;
}
 
M

MC felon

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top