why getline(cin,string) doesn't work for my code?

Hello,

I need to translate the word no matter if there's a space I need to get the whole line translated. Translating is not the problem but to get whole line is the problem. Can somebody help me fix this?

It is inside the switch statements.

1
2
3
4
5
6
7
string word;
        system("cls");
                cout << "Please enter a word for Ubi Dubbi translating "<< endl;
                cout << "You input :";
                getline(cin,word); // here where it doesn't allow me to enter anything and for your input doesn't display also.
                cout<<"your translated word is : "<<add_prefix( word, vowels, prefix );
                break;
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
   while(1)
   {
       std::string word;
       std::cout << "Please enter a word for Ubi Dubbi translating ";
       std::getline( std::cin,word);
    
       std::cout<<"your translated word is : "<< word << '\n';
   }
    
    return 0;
}


Please enter a word for Ubi Dubbi translating ffffa   s 
your translated word is : ffffa   s 
Please enter a word for Ubi Dubbi translating hjjhkj ihjnklnj,klm
your translated word is : hjjhkj ihjnklnj,klm
Please enter a word for Ubi Dubbi translating hhhhhhh jjjjjj
your translated word is : hhhhhhh jjjjjj
Please enter a word for Ubi Dubbi translating iiiiiiw n ejjje n
your translated word is : iiiiiiw n ejjje n
Please enter a word for Ubi Dubbi translating  


As you can see, this works perfectly using the shell here
Last edited on
does it matter if I don't put std? because I am using using namespace std;
no it won't work..it doesn't allow me to enter anything .it just display my menu instead..

@selflearner

Did you put #include <string> with your other #include's?
closed account (48T7M4Gy)
does it matter if I don't put std?
It's not the end of the world. There are good reasons for doing it if you surf around when/if u have time.

because I am using using namespace std;
see above.

if you dont put std:: in front of stuff then you have a couple of alternatives, one of which is using namespace std; which is the quick and dirty way. Up to you.

no it won't work..it doesn't allow me to enter anything .it just display my menu instead..
My posted program works on the console here as the sample output I posted shows. That's why I posted it. Something else must be going on with your program your snippet is not enough to test on it's own. You can run mine yourself by hitting the gear wheel. :)
Last edited on
> no it won't work..it doesn't allow me to enter anything .it just display my menu instead..

You have probably performed a formatted input operation on the stream prior to attempting std::getline()

You need to extract and discard trailing white space left by the formatted input operation.
See: http://www.cplusplus.com/forum/general/69685/#msg372532
closed account (48T7M4Gy)
Did you put #include <string> with your other #include's?

Yep you definitely need that include and that maybe why he can't get it to run. For some reason the shell here turns a blind eye, but it is a little strange at times. :)
Topic archived. No new replies allowed.