Thread: My new programm......having problems.

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Exclamation My new programm......having problems.

    So I started this new program and I can't seem to get past the first step.

    Code:
     
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char Menu, Start, Recall, Exit,choice;
    
    cout <<"Hello, how are we today?" <<endl;
    cout <<"|===================|" <<endl;
    cout <<"|       (M)enu                          |" <<endl;
    cout <<"|       (S)tart                           |" <<endl;
    cout <<"|       (R)ecall                          |" <<endl;
    cout <<"|       (E)xit                             |" <<endl;
    cout <<"|______________________|" <<endl;
    cout <<"Please choose a destination" <<endl;
    cin >>choice;
       if (choice == Menu)
       {
           cout <<"You are in menu now!" <<endl;
       }
    system("pause");
    return 0;
    }
    The menu choice option won't work. Any ideas?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    I think it's the if statement. It should be

    if(choice == 'M')
    cout << "You are in menu now!" << endl;

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    12

    Re: My new programm......having problems.

    Originally posted by RealityFusion
    So I started this new program and I can't seem to get past the first step.

    Code:
     
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char Menu, Start, Recall, Exit,choice;
    
    const char Menu = 'M';
    const char Start = 'S';
    const char Recall = 'R';
    const char Exit = 'E';
    char choice;
     
    cout <<"Hello, how are we today?" <<endl;
    cout <<"|===================|" <<endl;
    cout <<"|       (M)enu                          |" <<endl;
    cout <<"|       (S)tart                           |" <<endl;
    cout <<"|       (R)ecall                          |" <<endl;
    cout <<"|       (E)xit                             |" <<endl;
    cout <<"|______________________|" <<endl;
    cout <<"Please choose a destination" <<endl;
    cin >>choice;
       if (choice == Menu)
       {
           cout <<"You are in menu now!" <<endl;
       }
    system("pause");
    return 0;
    }
    The menu choice option won't work. Any ideas?
    If you make a comparison (choice == Menu) you have to have declared both variables before. In your program you declared choice when typing it in, but you never declared what Menu is.
    I added const in front of the variables, since I'm guessing that they won't be changed...

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    I changed the code a bit and it still won't work.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
     char m,s,r,e,choice;
     
    
    cout <<"Hello, how are we today?" <<endl;
    cout <<"|======================|" <<endl;
    cout <<"|       (M)enu         |" <<endl;
    cout <<"|       (S)tart        |" <<endl;
    cout <<"|       (R)ecall       |" <<endl;
    cout <<"|       (E)xit         |" <<endl;
    cout <<"|______________________|" <<endl;
    cout <<"Please choose a destination" <<endl;
    cin >>choice;
       if (choice == m)
       {
           cout <<"You are in menu now!" <<endl;
       }
    system("pause");
    return 0;
    }
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Change this:
    Code:
    if (choice == m)
    {
        cout <<"You are in menu now!" <<endl;
    }
    to:
    Code:
    if (choice == 'm')
    {
        cout <<"You are in menu now!" <<endl;
    }
    Without the apostrophe's, you are comparing it to a variable, not a value. The VARIABLE m doesn't equal anything unless you initialize it.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Your checking to see if the user's input is equal to an uninitialized variable. You have to assign some kind of value to your chars:
    Code:
    char m='m'; //one way to do it
    const char s='s'; //this way the value assigned to the variable cant change
    or if you want you can:
    Code:
    #define m 'm'
    at the top of your program.

    or even simpler (but without variables)

    Code:
    if (choice=='m') //but remember if the user inputs 'M' this wont work since it is case sensitive ('m' is a different character)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thanks, that fixed the problem!
    So the single aposthropy's (sp?) initialize things. I was kind of thinging that. Since M was declared as a char as well as choice it wouldn't output my if statement because choice and m are not the same value. It makes sense. Thank you.
    Knowledge is power and I want it all

    -0RealityFusion0-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. HexDump Programm....
    By Kdar in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 05:13 AM
  3. programm will not start
    By keeper in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 06:02 AM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM