program will only accept one word

this is starting to drive me out of my mind!!

Page 1 of 1

8 Replies - 1354 Views - Last Post: 28 February 2008 - 06:12 PM Rate Topic: -----

#1 aj32  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 577
  • Joined: 30-August 07

program will only accept one word

Posted 28 February 2008 - 04:36 PM

Now this is driving me nuts :crazy: , perhaps there is a simple error and I just am not seeing it, but anyway.

When I have a simple program like below:
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

string somevariable;

int main(int argc, char *argv[])
{
    cout << "Enter something: " << endl;
    cin >> somevariable;
    cout << somevariable;
    system("PAUSE");
    return EXIT_SUCCESS;
}




When I input 2 words (e.g. one two), the program only stores the first one, and the next on is either dropped or sent on to the next cin. It does the same thing with char[] and integers! Maybe this is just being caused by a setting on command prompt?

Thank in advance for any help.
AJ32

Is This A Good Question/Topic? 0
  • +

Replies To: program will only accept one word

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: program will only accept one word

Posted 28 February 2008 - 04:46 PM

that's a limitation with the >> operator - it ceases to read when it encounters the first whitespace character. you need to use the getline() function instead, which will read everything until the 'newline' character (ie, the point when the user pressed enter).

 string mystring;
 cout << "Enter something: " << endl;
 getline( cin, mystring ); 

This post has been edited by Bench: 28 February 2008 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: program will only accept one word

Posted 28 February 2008 - 05:02 PM

#include <cstdlib>  
#include <iostream>  
#include <string.h>  
   
using namespace std;  
  
 
  
int main(int argc, char *argv[]) {  
	char somevariable[20]; 
	cout<<"Enter something: "<< endl; 
	cin.getline(somevariable,20,'\n');
	cout << somevariable;
        system("pause");
	return EXIT_SUCCESS;  

}  



Variation: This works for char-arrays.
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: program will only accept one word

Posted 28 February 2008 - 05:39 PM

View Postletthecolorsrumble, on 29 Feb, 2008 - 12:02 AM, said:

Variation: This works for char-arrays.

Yes, although you shouldn't ever use char arrays in C++, unless you really know what you're doing and why.
Was This Post Helpful? 0
  • +
  • -

#5 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: program will only accept one word

Posted 28 February 2008 - 05:47 PM

I wouldn't want to have char-arrays in C/C++, if I could make the rules :)
Was This Post Helpful? 0
  • +
  • -

#6 aj32  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 577
  • Joined: 30-August 07

Re: program will only accept one word

Posted 28 February 2008 - 05:51 PM

Thank you Bench, that worked. :^:

But the odd thing is, I have always used cin >>, and it always worked untill today.? :blink:
Was This Post Helpful? 0
  • +
  • -

#7 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: program will only accept one word

Posted 28 February 2008 - 05:56 PM

View Postaj32, on 28 Feb, 2008 - 06:51 PM, said:

Thank you Bench, that worked. :^:

But the odd thing is, I have always used cin >>, and it always worked untill today.? :blink:


You just think you have. :)
Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: program will only accept one word

Posted 28 February 2008 - 06:09 PM

View Postaj32, on 29 Feb, 2008 - 12:51 AM, said:

Thank you Bench, that worked. :^:

But the odd thing is, I have always used cin >>, and it always worked untill today.? :blink:

Here's one of the reasons why, in software development, you can never guarantee that something is 100% bug free - You might have tested and tested, and tested something over again, and you're absolutely sure that nothing which any user could ever throw at it could possibly break your program. After you issue the new release, a user discovers a huge flaw in something which you thought worked fine, but it turns out to have had some weird side effect that you'd never really considered before. You'll swear blind that the program didn't behave like that when you tested it, but its amazing the kind of bugs you miss out when you think something is doing what you want it to do. :)
Was This Post Helpful? 0
  • +
  • -

#9 aj32  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 577
  • Joined: 30-August 07

Re: program will only accept one word

Posted 28 February 2008 - 06:12 PM

View PostBench, on 28 Feb, 2008 - 08:09 PM, said:

View Postaj32, on 29 Feb, 2008 - 12:51 AM, said:

Thank you Bench, that worked. :^:

But the odd thing is, I have always used cin >>, and it always worked untill today.? :blink:

Here's one of the reasons why, in software development, you can never guarantee that something is 100% bug free - You might have tested and tested, and tested something over again, and you're absolutely sure that nothing which any user could ever throw at it could possibly break your program. After you issue the new release, a user discovers a huge flaw in something which you thought worked fine, but it turns out to have had some weird side effect that you'd never really considered before. You'll swear blind that the program didn't behave like that when you tested it, but its amazing the kind of bugs you miss out when you think something is doing what you want it to do. :)


yeah I know what you mean! :rolleyes:

Again, thank you for your help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1