Thread: C++ Problem

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    C++ Problem

    I've been learning C++ for about a year now, and it's the first lower-level language I've learned. Anyway, here's my problem:

    I have a program with a
    Code:
    while()
    loop in it. Inside this loop is a
    Code:
    char
    integer. It looks something like this:

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    int x = 0;
     while (x == 0)
     {
        char question[80];
        cin >> question;
        cout << "\nWould you like to play again? (Y/N): ";
        char yorn[10];
        char y[] = "y";
        char n[] = "n";
        cin >> yorn;
        x = strcmp(yorn, y);
     }
    return 0;
    }
    Now, for some reason, when the user enters two words with a space in between them in the "question" entry, the program ignores the loop. So then I tried this:

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    int x = 0;
     while (x == 0)
     {
        char question[80];
        cin.get(question, 79);
    ect...

    That works, but then when you run the program again, the program ignores the
    Code:
    cin.get(question, 79);
    . What is causing this? is there any way to clear the data out of
    Code:
    char buffer?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Use the first code snippett, but use fgets...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    int main()
    {
    int x = 0;
     while (x == 0)
     {
        char question[80];
        cin.getline(question,80);
        cout << "\nWould you like to play again? (Y/N): ";
        char yorn[10];
        char y[] = "y";
        char n[] = "n";
        cin >> yorn;
    //Add ignore() here
        cin.ignore(80,'\n');
        x = strcmp(yorn, y);
     }
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    Thumbs up Thanks

    Thanks everybody!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM