Thread: What's wrong with this???

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Lightbulb What's wrong with this???

    Can somebody please explain me what's wrong with this code???
    getline() here supposed to read a whole line at a time.... right...???
    but doesn't do anything... s1 doesn't have any value when you feed it a whole line...
    s reads a word terminated by '\0'... that's fine but s1 doesn't ...

    <code>
    #include <iostream>
    #include <string>

    using namespace std;

    int main(void)
    {
    cout << "Enter a word:" << endl;
    string s, s1;
    cin >> s;
    cout << "You Entered " << s << '\n';
    cout << "Enter a sentence: \n";
    getline(cin, s1);
    cout << "You Entered " << s1 << '\n';
    return 0;
    }
    </code>

    Thanks
    RamBUS.net

    I'm using Visual Studio 6.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Try this out. I dont preferably use strings i like char better but see if it helps.

    Code:
    #include <iostream>
    #include <conio.h> //for compiler
    
    using namespace std;
    
    int main()
    {
    cout << "Enter a word:" << endl;
    char s[50];
    cin >> s;
    cout << "You Entered " << s << '\n';
    cout << "Enter a sentence: \n";
    char s1[150];
    cin >>s1;
    cout << "You Entered " << s1 << '\n';
    getch(); // for compiler
    return 0;
    }
    NOTE its [code] not <code>
    Last edited by gamer4life687; 11-24-2002 at 07:10 PM.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    Personally i dislike using string variables and use charracter arrays instead

    To see what an array is visit the tutorial(this will help the understanding of using the array).
    . But for this purpose s is a string with a limit of 50 characters, and s1 is a string with a limit of 150 characters.

    See what happens with it like this.

    The person who posted above used cin to input the sentence. Cin does not read null characters(any type of space like a space or an enter).

    Code:
    --------------------------------------------------------------------------------
    #include <iostream>
    #include <conio.h> //for compiler
    
    using namespace std;
    
    int main()
    {
    cout << "Enter a word:" << endl;
    char s[50];
    cin >> s;
    cout << "You Entered " << s << '\n';
    cout << "Enter a sentence: \n";
    char s1[150];
    cin.getline(s1,150,'/n'); //reads spaces with getline and stops inpt 
    //when it reaches a '\n' (aka enter) the 150 is the max amount of 
    //chars that can be inputted
    cout << "You Entered " << s1 << '\n';
    getch(); // for compiler
    return 0;
    }
    Last edited by bc17; 11-24-2002 at 07:40 PM.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Question So......

    Thanks guys...

    so.. does it mean that this is wrong...???

    http://www.research.att.com/~bs/bs_faq2.html
    will be there one day!

    I usually use VStudio 6!

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Exclamation Oooopss....

    Anyways... i tried both of your ways.... but didn't work for me..

    I don't know if it runs fine with your compiler...

    I know that
    file.getline(var, len, delimeter);
    works fine for reading sentence from a file but had never tried reading string from the keyboard...

    hum... I don't know...!!!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The problem is that your call to cin results in a newline character being left in the input stream. Since getline stops reading at a newline character, it reads the leftovers of cin and nothing else, resulting in an empty string. To solve the problem, flush the input stream with cin.ignore():
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
      cout << "Enter a word:" << endl;
      string s, s1;
      cin >> s;
      cin.ignore(); // Eat the newline
      cout << "You Entered " << s << '\n';
      cout << "Enter a sentence: \n";
      getline(cin, s1);
      cout << "You Entered " << s1 << '\n';
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    30
    dope!!

    Yea they got it. You gotta clear the buffer. I geuss their way would work. You can also getline the leftover'\n' into a dummy variable.
    Last edited by bc17; 11-24-2002 at 08:54 PM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Yea he got it. You gotta clear the buffer. I geuss his way would work.
    Welcome to cprogramming btw. ^_^

    -Prelude
    My best code is written with the delete key.

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Prelude
    >Yea he got it. You gotta clear the buffer. I geuss his way would work.
    Welcome to cprogramming btw. ^_^

    -Prelude
    You're just going to let that slide? Cmon!

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You're just going to let that slide? Cmon!
    I'm in a good mood tonight.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Thumbs up swee....t!

    Thnaks Prelude!
    It did work that way....
    However, it requires 2 return keys to get the output written on the screen... That is little too interesting to me... :-) I'm not sure..
    I tried to play around with the codes but still needs you to enter "enter" twice to get the s1 printed out on the console.
    Any further little explanation would be great to clear my little brain.

    Thank you in advance!
    will be there one day!

    I usually use VStudio 6!

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Any further little explanation would be great to clear my little brain.
    I would wager that you use MS Visual C++ 6. That compiler has a bug in getline that requires you to press return twice. The MS web site has a fix here if it really bothers you.

    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Thumbs up Thank you!

    That clears it up!)
    Thanks a lot Prelude!
    will be there one day!

    I usually use VStudio 6!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM