By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

input problem

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.