Thread: Can Anyone Help!! This Is Bugging Me

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

    Unhappy Can Anyone Help!! This Is Bugging Me

    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
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Your program's doing exactly what you told it to...

    You input the name.
    The name is Christian, so it does the stuff in the while loop.
    Then, it returns 0 (quits the program).

    I think this'll do more what you want.

    Code:
    #include <fstream.h> //Why are you including this?
    #include <string.h> 
    #include <conio.c> 
    
    char response [10]; //Response is a character array to store the input.
    
    int main() 
    { 
    
    while((response != 'q') && (response != 'Q'))
    {
    cout << "\n Input the name Christian, Valerie, or Olivia to see their age. (q to quit)\n"; 
    cin >> response; 
    
    if(strstr(response,"Christian")!=NULL)  
        cout << response << " is 20 years old.\n"; 
     
    if(strstr(response,"Valerie")!=NULL) 
        cout << response << " is 15 years old.\n"; 
    
    if(strstr(response,"Olivia")!=NULL) 
         cout << response << " is 8 years old.\n"; 
    }
    
    getch();
    return 0; 
    }

  3. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    THanks GOVT...very helpful..but i get 1 error message which reads


    " ANSI C++ forbids comparison between pointer and integer"

    In response to :
    while((response != 'q') && (response != 'Q'))

    What might be the solution?

  4. #4
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    You need to check only the first letter of the array
    so instead of:
    Code:
    while((response != 'q') && (response != 'Q'))
    you'd do this:
    Code:
    while((response[0] != 'q') && (response[0] != 'Q'))
    Not the best way to do it though (in case someone has a name starting with Q) but it works for your program.

    To make life easy you could use the toupper() or tolower() functions so you don't have to check for both upper and lower case.

    ** You should also initialize response[] because you're using it before you get a value in the while loop.

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

    Thanks

    Very Very Comprehensive Reply..THanks a lot Taylor..and Govt..you too..I understand this now..Take care

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bugging ... Compile Time Error
    By keats in forum C Programming
    Replies: 7
    Last Post: 03-25-2007, 04:54 AM
  2. This error is bugging me!
    By ct28au in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2002, 09:18 AM
  3. Pointer Riddle that is bugging me
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-29-2001, 11:11 PM