3

I have wrote a code for taking string from the user in C++. here is the code

#include<iostream>
#include<string>
using namespace std;
string input;
void enterstring()
{
    cout<<"\nEnter input: ";
    getline(cin,input);

}
void displaystring()
{
    cout<<"\nyour string is "<<input<<endl;

}
{
    int main()
{
int choice=0;
while(choice!=3)
{

    cout<<"Enter choice: "<<;
          cin>>choice;
    switch(choice)
    {
        case 1: enterstring();
        break;
        case 2: displaystring();
        break;
        case 3: cout<<"\nQuit";
        break;
        default: cout<<"\ninvalid choice try again";
        break;


    }
    return 0;
}

output of the above code:

Enter choice: 1
Enter input: 
Enter choice:

the input section is skipped I don't know why where is the problem. Is the logic wrong, problem with syntax anything. when I call function without using while loop etc it's working fine but in this case it's not working. help me.

6
  • Try using cin>>input; in the enterString() function Commented Oct 15, 2012 at 8:16
  • 1
    A program shall contain a global function called main , which is the designated start of the program. --ISO/IEC 14882-2011 3.6.1 p1 Commented Oct 15, 2012 at 8:18
  • @Shashwat used cin>>input when I write a string suppose "The world is full of selfish people" entering space gives infinite loop. to avoid this i used getline. Commented Oct 15, 2012 at 8:31
  • but my program contains main function. @pwned . Commented Oct 15, 2012 at 8:32
  • I am surprise why it has skipped the main function.. very strange. I edited it so sorry @Shashwat Commented Oct 15, 2012 at 8:38

2 Answers 2

3

The problem is that you are reading choice as int but the input is a string with a newline.

In your example the input is not 1 is "1\n" 1 goes in choice as int, '\n' is in the buffer. When you call the getline function that function read from the buffer, found the newline and return an empty string. To avoid that you should read choice as string and than use atoi to cast to int.

EDIT:

you are right. It's still not working. But here there is a version that work.

#include <iostream>
#include <string>

using namespace std;
string input;
void enterstring(){
    cout<<"\nEnter input: ";
    cin.ignore();
    getline(cin,input);
}

void displaystring(){
    cout<<"\nyour string is "<<input<<endl;
}

int main(){
    int choice=0;
    while(choice!=3){
        cout<<"Enter choice: ";
        cin>>choice;
        switch(choice){
            case 1: enterstring();
            break;
            case 2: displaystring();
            break;
            case 3: cout<<"\nQuit";
            break;
            default: cout<<"\ninvalid choice try again";
            break;
        }
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

6 Comments

used choice as a string and then convert it to int using atoi . still it skips the input part. :) all of the answers here didnt worked.
thanks for the new code . :) but this code only works when you are typing strings without spaces e-g "thisistheworldfullofgoodpeople" whenever you type string e-g "this is the world full of good people" you are subjected to infinite loop... :D that's why I used getline();
right.. now it's fixed. cin.ignore() will get rid of anything still in the buffer before the read
and my problem is fixed :) thanks a lot. means everytime I had to use cin.ignore() in order to get rid of everything before in buffer.. !
Ok, if the problem is fixed don't forget to accept the answer.
|
1

The code you posted is invalid: you are missing a " after Enter choice :.

Besides from that, your code seems to work in ideone (I added some indentation and corrected some small bugs. I also added a main function). You can see it here : http://ideone.com/ozvB1

1 Comment

sorry I forgot to write " after Enter choice that was when I was typing here. I checked it in my code there " is present. but beside that it's not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.