cin.getline, help

This is a discussion on cin.getline, help within the C++ Programming forums, part of the General Programming Boards category; I've made this code for a simple test quiz program, it's inside an if statement: Code: cout<<"What is Bush's firstname?\n\n"; ...

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

    cin.getline, help

    I've made this code for a simple test quiz program, it's inside an if statement:


    Code:
                cout<<"What is Bush's firstname?\n\n";
                cout<<"Answer: ";
    
                cin.getline(string, 1000, '\n');
        
                if (!strcmp("george", string) || !strcmp("George", string) || !strcmp("GEORGE", string))
                {
                    cout<<"Correct!\n\n";
                }
                else
                {
                    cout<<"WRONG!\n\n";
                }
    but when i get to the point where you are to input, then it skips it and goes right to the "else" part (because "nothing" is a wrong answer)

    I can't figure out what is wrong... plz help

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Code:
    cin >> string;
    ? any help

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'm not very good with cin, but from previous threads I've read, it has to do with the stream containing characters already. Try using cin.ignore(80). I am probably wrong though.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    20
    Code:
    int string[1000];
    that's the name of the var, it hasn't been used before in the script (first question).

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by bennyandthejets
    I'm not very good with cin, but from previous threads I've read, it has to do with the stream containing characters already. Try using cin.ignore(80). I am probably wrong though.
    The chances of your being right are high, but using a constant for cin.ignore() is dicey. A better way, if more obscure, is to use numeric_limits on the streamsize type:
    Code:
    #include <limits>
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    My best code is written with the delete key.

  6. #6
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    well here's your working code in beginners lanuage which you should be able to make sense of (albeit slightly modified)

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    //declare your variable
       char s[100];  
    
       cout << "What is Bush's firstname?\n\nAnswer: ";
    //push input into variable
       cin.getline(s, 100, '\n');
    //run a loop to change any uppercase chars to lower
       for (int i=0; i<100; i++) {
          s[i] = tolower(s[i]);
       }
    //run your comparison
       if (!strcmp("george", s)) {
           cout<<"Correct!\n\n";
       } else {
           cout<<"WRONG!\n\n";
       }
    
       system("PAUSE");
       return 0;
    }
    hope that helps
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 11:01 AM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 08:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21