string problems

#include <iostream>
#include <string>
#include <strings.h>// just to make sure.

using namespace std;

void choicex();
void choicey();
int main()

{

int choice;


cout <<"Press '1' for choice 'x'"<<endl;
cout <<"Press '2' for choice 'y'"<<endl;

cin >> choice;
if (choice== 1)
choicex();
if (choice== 2)
choicey();

main();
return 0;

}
void choicex()
{
string anything;
int choicex1;
cout<<"Choose: 1. 'x1a', 2. 'x1b, 3.'x1c'."<<endl;
cin >>choicex1;
if (choicex1==1)
{
cout<<"choice 'x1a' "<<endl;
}
if (choicex1==2)
{
cout<<"choice 'X1b'"<<endl;
cout<<"choice 'x1b' requires a string input"<<endl;
cout<<"type anything and hit ENTER."<<endl;
getline (cin,anything);// why does this not work?
cout<<"\n\n"<<anything<<endl;
}
if (choicex1==3)
{
cout<<"choice 'X1c'"<<endl;
}
return;
}
void choicey()
{
string anything;
int choicey1;
cout<<"Choose: 1. 'y1a', 2. 'y1b, 3.'y1c'."<<endl;
cin >>choicey1;
if (choicey1==1)
{
cout<<"choice 'y1a' "<<endl;
}
if (choicey1==2)
{
cout<<"choice 'y1b'"<<endl;
cout<<"choice 'y1b' requires a string input"<<endl;
cout<<"type anything and hit ENTER."<<endl;
getline (cin,anything);// why does this not work?
cout<<"\n\n"<<anything<<endl;
}
if (choicey1==3)
{
cout<<"choice 'y1c'"<<endl;
}
return;
}
Last edited on
closed account (DSLq5Di1)
Question? error? output? expected output?

[code]Code tags[/code] would be lovely too.
It's buried in the middle: getline (cin,anything);// why does this not work?
There is no compiler error; The program does not execute and/or skips 'getline (cin, anything);'. On top of that the compiler states 'string anything' does not name a type if only set as a global string(before int main()). but apparently that is a different problem since it works fine in this program and not in the original. the program will output three blank lines after selecting 'choice 'y1b' ' or 'choice 'x1b' ' and then loop back to the beginning of the program. My apologies for being cryptic.
Last edited on
so far I have determined that 'string anything;' is not intializing 'anything'.
cin leaves a newline character that getline interprets as a line. If you're using cin, use cin. If you're using getline, use getline. Do not mix and match unless you're clearing buffers out first.


#include <strings.h>// just to make sure.
To make sure of what, exactly? I don't see you using any of these functions:
http://pubs.opengroup.org/onlinepubs/7908799/xsh/strings.h.html

Last edited on
thank you Moschops, I found that a redundant 'getline (cin,anything);' produced the effect that I desired. I suspect this will not work at higher levels. Thank you for the link, it will be invaluable.
I found that a redundant 'getline (cin,anything);'


That's something of a clumsy solution. A more reliable method would be to deliberately flush the buffer of the leftover newlines.
// yes, I was not able to implement a sub-function that I had found:


#include <ios>
#include <istream>
#include <limits>

void ignore_line ( std::istream& in )
{
in.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
}

/*I was not able to call the function properly, it may have had something to with 'using namespace std;'.*/

Topic archived. No new replies allowed.