|
Prev: Borland C++
Next: High Speed IRQ Timer/Clock in C
From: kimiraikkonen on 27 Dec 2007 10:55 Hi there, Sorry if this question so simple but i hope i learn, when i use "char" or "string" object to display entered message, if the message is more than one word(a sentence), my app gets closed with no complain. For example: If i enter "hello", there's no problem, if i enter "hello group" program goes closed. Here is the code: #include <iostream> using namespace std; int main(){ string message; //Asking for message cout<<"Enter your message: "; //Creating entry cin>>message; //Need to put to see message getchar(); //Here is my message cout<<"Your message is: "<<message; //Need to put to see message getchar(); return 0; } How can i correct this? Thanks!
From: R. Scott Mellow on 27 Dec 2007 11:41 "kimiraikkonen" wrote > Hi there, > Sorry if this question so simple but i hope i learn, when i use "char" > or "string" object to display entered message, if the message is more > than one word(a sentence), my app gets closed with no complain. > > For example: If i enter "hello", there's no problem, if i enter "hello > group" program goes closed. > > Here is the code: > > #include <iostream> > > using namespace std; > > int main(){ > string message; > > //Asking for message > cout<<"Enter your message: "; > > //Creating entry > cin>>message; > > //Need to put to see message > getchar(); > > //Here is my message > cout<<"Your message is: "<<message; > > //Need to put to see message > getchar(); > return 0; > > } > > How can i correct this? > > Thanks! #include <iostream> using std::cout; using std::cin; #include <string> using std::string; using std::getline; int main() { string message; cout << "Enter your message: "; /* The >> operator for istream objects (e.g. std::cin) only reads input until it sees a whitespace. Use std::getline to read an entire line of input. */ if(getline(cin,message)) { cout << "Your message is: " << message; } else { cout << "Unable to read your message."; } return 0; } -- Randy
From: Piranha on 27 Dec 2007 11:54 On 27 Dez., 16:55, kimiraikkonen <kimiraikkone...(a)gmail.com> wrote: > Hi there, > Sorry if this question so simple but i hope i learn, when i use "char" > or "string" object to display entered message, if the message is more > than one word(a sentence), my app gets closed with no complain. > > For example: If i enter "hello", there's no problem, if i enter "hello > group" program goes closed. > > Here is the code: > > #include <iostream> > > using namespace std; > > int main(){ > string message; > > //Asking for message > cout<<"Enter your message: "; > > //Creating entry > cin>>message; > > //Need to put to see message > getchar(); > > //Here is my message > cout<<"Your message is: "<<message; > > //Need to put to see message > getchar(); > return 0; > > } > > How can i correct this? > > Thanks! cin takes not just one input, but as many as you like and it separates the input by spaces If you put in 2 words separated by a space, cin expects 2 variables where to store the data and since you defined just one variable, your code crashes there To read a sentence including spaces you can use cin.getline();
From: Ulrich Eckhardt on 27 Dec 2007 11:43 kimiraikkonen wrote: > Sorry if this question so simple but i hope i learn, when i use "char" > or "string" object to display entered message, if the message is more > than one word(a sentence), my app gets closed with no complain. > > For example: If i enter "hello", there's no problem, if i enter "hello > group" program goes closed. > > Here is the code: > > #include <iostream> > > using namespace std; > > int main(){ > string message; You haven't included the header required for std::string. > //Asking for message > cout<<"Enter your message: "; Technically, you haven't included the header to use std::cout either, but this happens to work with most compilers. The one with std::string doesn't though. > //Creating entry > cin>>message; This will first skip any whitespace (space, tabs, newlines) and then copy characters up to but excluding the next whitespace into 'message'. That means that anything including the next whitespace remains in the input buffers, and that is probably the cause for your problems when you enter more. > //Need to put to see message > getchar(); This will read exactly one character from stdin. Note that it is considered bad style to mix IO using C++ IOStreams and C stdio. Use 'std::cin.get()' as a rough equivalent. Further, your comment is simply a misinterpretation of your perceptions. My guess is that in your environment, the program is launched in a separate window that is destroyed after the program terminates. This doesn't give you any time to read the output, but the output is there nonetheless. > How can i correct this? Several suggestions: - If you wanted the whole input up to the next newline in the 'message' variable, you should use the getline() function: getline( std::cin, message); Note that this extracts everything up to the next newline ('\n') from the input. - If you want to wait for the user to press enter, a good thing to do is to first flush the input buffers and then also use getline() to wait for the user to hit enter. Note that this doesn't work well for programs that you might want to feed automated input using input/output redirection. You can flush the buffers by using the ignore() function with a large number of chars to ignore (there is a technically better way to do this, use a websearch to find it). - You should run your programs directly from a commandline. This allows you to test IO redirection, too, and you don't need any hacks and documentation (you did document the reasons for first ignoring input and then reading a line, right?) to see the full output of your program. You can still set a breakpoint at the end of main() to get the same results. Uli
From: kimiraikkonen on 27 Dec 2007 12:01
On Dec 27, 6:41 pm, "R. Scott Mellow" <fil...(a)rsmellow.com> wrote: > "kimiraikkonen" wrote > > > > > Hi there, > > Sorry if this question so simple but i hope i learn, when i use "char" > > or "string" object to display entered message, if the message is more > > than one word(a sentence), my app gets closed with no complain. > > > For example: If i enter "hello", there's no problem, if i enter "hello > > group" program goes closed. > > > Here is the code: > > > #include <iostream> > > > using namespace std; > > > int main(){ > > string message; > > > //Asking for message > > cout<<"Enter your message: "; > > > //Creating entry > > cin>>message; > > > //Need to put to see message > > getchar(); > > > //Here is my message > > cout<<"Your message is: "<<message; > > > //Need to put to see message > > getchar(); > > return 0; > > > } > > > How can i correct this? > > > Thanks! > > #include <iostream> > using std::cout; > using std::cin; > > #include <string> > using std::string; > using std::getline; > > int main() > { > string message; > > cout << "Enter your message: "; > > /* > The >> operator for istream objects (e.g. std::cin) only reads input > until it sees > a whitespace. Use std::getline to read an entire line of input. > */ > if(getline(cin,message)) > { > cout << "Your message is: " << message; > } > else > { > cout << "Unable to read your message."; > } > > return 0; > > } > > -- > Randy Hi Mr. Mellow, Thanks for quick response and a working solution. I modified the code with the same with working result as follows: #include <iostream> #include <string> using namespace std; int main() { string message; cout << "Enter your message: "; getline (cin,message); cout<<message; getchar(); return 0; } which does the same job, thanks for pointing out using "getline" in string class. Regards. |