Thread: Console input

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    36

    Console input

    Hi all

    I'm reading in some data from the user but have some problems when they seperate words by a space - each word is a taken as a seperate input :-(

    Code:
    	string name, address, account;
    	cout << "Input suppliers name: ";
    	cin  >> name;
    	cout << "Input suppliers address: ";
    	cin  >> address;
    	cout << "Input suppliers account name: ";
    	cin  >> account;
    So if I entered "Nice one" for the suppliers address Then the address would end up as "Nice" and account name would end up as "one".

    Any ideas what I need to do to get correct input??

    Thanks

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    2
    you could try to insert this instead:

    Code:
               char name[256];
               char address[256];
               char account[256];
               cout << "Input suppliers name: ";
               cin.getline(name, 256, '\n') //the userinput is added to variable: name
               cout << "Input suppliers address: ";
               cin.getline(address, 256, '\n') //the userinput is added to variable: address
               cout << "Input suppliers account name: ";
               cin.getline(account, 256, '\n') //the userinput is added to variable: account
    And to check if it worked:
    Code:
               cout << name << endl << address << endl << account;
    Hope it works

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    36

    mmm

    Thanks for the come back.

    I could use an array but then I'm limiting the user to a max number of characters.

    How can I catter for spaces and not run into this limitation?

    Ta

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by ozzy34
    Thanks for the come back.

    I could use an array but then I'm limiting the user to a max number of characters.

    How can I catter for spaces and not run into this limitation?

    Ta
    getline is overloaded for the string class.
    Code:
    string name;
    string address;
    string account;
    
    cout << "Input suppliers name: ";
    getline(cin, name);
    cout << "Input suppliers address: ";
    getline(cin, address);
    cout << "Input suppliers account name: ";
    getline(cin, account);
    When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    36

    Not quite there

    mmm Somthing is not working with the getline routine.

    I have

    Code:
    	string name, address, account;
    	cout << "Input suppliers name: ";
    	getline(cin,name);
    	cout << "Input suppliers address: ";
    	getline(cin,address);
    	cout << "Input suppliers account name: ";
    	getline(cin, account);
    	cout << "name: " << name
    		 << ", address: " << address
    		 << ", account: " << account << endl;
    If I run this I enter the following at the prompts:-

    Input suppliers name: jas
    Input suppliers address: here
    Input suppliers account name: ja1

    however this cout out gives me:

    name: , address: jas, account: here

    So things are out by one position :-( BTW - This is in a sub menu and the menu that calls this thinks there has been a user input, so something is being buffered somewhere.

    Any ideas how to overcome?

    Cheers

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Change your prompt lines from...

    cout << "Input suppliers name: ";

    ... to...

    cout << "Input suppliers name: " << flush;

    ... and try again.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    36

    still no joy

    I have changed my code as you suggested and still get the same issue...

    I came across something that mentioned that getLine also inputs a \n. Is this doing something??

    Ta

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sounds like there is a newline char as the first char in the input buffer when getline() is called, probably left there by a previous call to >> or get() or something. Try placing the following line before the call to getline()


    cin. ignore(8000, '\n');

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    36
    Adding that certainly gets my strings populated properly but I now have to hit the enter key twice when I enter the suppliers name....

    Do I need to add anything else?

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    It is possible that hitting enter twice is a result of a bug in your standard library implementation (if you use VC++ 6.0). Otherwise, show more code so we can see what is being called before these calls to getline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. Reading console input (character mode applications)
    By maxhavoc in forum Windows Programming
    Replies: 12
    Last Post: 11-27-2005, 04:13 AM
  4. How to put a Char into the Input buffer of a console?
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2003, 10:05 AM
  5. Need help with Console Keyboard Input
    By pawelx2 in forum Game Programming
    Replies: 5
    Last Post: 05-30-2002, 11:03 PM