Thread: Help!

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Help!

    What's Up everyone, if anyone can help it would be appreciated.

    My problem with this program (see output at very bottom) is that
    I can use it once,and see one person's age, but when i try to input a second or third name after the first time, and press enter, the DOS Window closes on me. What can I do to keep this Program running for as long as I want?




    #include <fstream.h>
    #include <string.h>
    #include <conio.c>

    char response [10]; //not sure what this does (10)


    int main()
    {

    cout << "\n Input the name Christian, Valerie, or Olivia to see their age.\n";
    cin >> response;

    while(strstr(response,"Christian")!=NULL)
    {
    cout << response << " is 20 years old.\n";
    getch();
    return 0;
    }
    while(strstr(response,"Valerie")!=NULL)
    {
    cout << response << " is 15 years old.\n";
    getch();
    return 0;
    }
    while(strstr(response,"Olivia")!=NULL)
    {
    cout << response << " is 8 years old.\n";
    getch();
    return 0;
    }


    return 0;
    }


    /*OUTPUT*************

    Input the name Christian, Valerie, or Olivia to see their age
    Christian (let's say i put this in)
    Christian is 20 years old (this is the output)

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Let's take a look at just one while-loop:

    Code:
    while(strstr(response,"Christian")!=NULL) 
    { 
        cout << response << " is 20 years old.\n"; 
        getch(); 
        return 0; 
    }
    When entering this loop. It will print the output string and the response, then it will wait untill you press a key and then it returns 0. In other words, it prints something and after you've pressed a key, the program will finish.

    By the way, I do not really see the meaning of strstr. It seems to me that you're comparing strings. In that case I would recommend using strcmp, a function which is written with the purpose to compare strings.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    By the way, I believe that it would be more efficient to use if statements. They were created for the purpose you want to use.

Popular pages Recent additions subscribe to a feed