while question

This is a discussion on while question within the C++ Programming forums, part of the General Programming Boards category; ok so why doesn't this work. i mean i can go into the different sections but when i'm back at ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Question while question

    ok so why doesn't this work. i mean i can go into the different sections but when i'm back at main i can't go into, lets say new game again. ok. i know i'm a super noob but i'm eager to learn. thx a bunch.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
        int x;
        while (x!=3) {
            cout<<"**MAIN MENU**\n\n";
            cout<<"1.New game\n2.Options\n3.Exit\n";
            cout<<"Choice:";
            cin>>x;
            cout<<"\n";
    
                /*NEW GAME*/
                if (x==1) {
                    int y;
                    while (y!=2) {
                    cout<<"*New game*\n\n";
                    cout<<"1.Map\n2.Back\n";
                    cout<<"Choice:";
                    cin>>y;
                    cout<<"\n";}
                }
    
                /*OPTIONS*/
                if (x==2) {
                    int z;
                    while (z!=2) {
                    cout<<"*Options*\n\n";
                    cout<<"1.Difficulty\n2.Back\n";
                    cout<<"Choice:";
                    cin>>z;
                    cout<<"\n";}
                    }
    
                }
    
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    int x;
        while (x!=3) {
    x is not initialized

    Code:
    int y;
                    while (y!=2) {
    y is not initialized
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    THX vart that worked but why doesn't it work unless i define x, because isn't what i do whith the cin>> comand, right?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    But you are trying to compare x != 3 before you're reading anything into the variable.
    I suggest using Visual Studio, if you can. It easily catches uninitialized variables errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    If you're making program that utilizes a menu selection system, use a case switch. you can google a tutorial, but it goes something like this.

    Code:
    switch (test) // where test is a declared int used to decide which case to execute
    {
      case 1 : 
        // code for menu 1
        break;
    
      case 2 : 
        // code for menu 2
        break;
    
      default : 
        // action that occurs when any other case happens (i.e. user presses a number that is not a part of your menu)
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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