Some questions about CIN (Sorry I'm a newb)

This is a discussion on Some questions about CIN (Sorry I'm a newb) within the C++ Programming forums, part of the General Programming Boards category; I'm sorry if something like this has already been posted. I looked through a couple of the threads and they ...

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

    Unhappy Some questions about CIN (Sorry I'm a newb)

    I'm sorry if something like this has already been posted. I looked through a couple of the threads and they were similar to my problem but not quite.

    This is part of my code.

    Code:
    int main()
    {
       cout << "Hi! What's your name? \n";
       cin >> UserName;
       cout << "\nWelcome, " << UserName << "!\a" << endl;
       MainMenu();
    }
    
    int Loop()
    {
       if (loop1 == true)
       {
          MainMenu();
       }
       if (loop1 == false)
       {
          //Do Nothing...
       }
       return 0;
    }
    
    
    int MainMenu()
    {
       cout << "\nChoose Wisely...\n";
       cout << "[1] Quake\n";
       cout << "[2] Unreal\n";
       cout << "[3] Half-Life\n";
       cout << "[4] Others\n";
       cout << "[5] Explore the CD\n";
       cin >> GamePref;
    
       if (int(GamePref) == 1)
       {
          cout << "\a" << endl;
          QuakeMenu();
          loop1 = false;
       }
       else if (int(GamePref) == 2)
       {
          cout << "\a" << endl;
          UnrealMenu();
          loop1 = false;
       }
       else if (int(GamePref) == 3)
       {
          cout << "\a" << endl;
          HLMenu();
          loop1 = false;
       }
       else if ((GamePref < 1) || (GamePref > 5))
    //Ok I know here is a problem because I need to 
    //specify that GamePref should not be a character either 
    //(Heh how would I do that?) Is it "&& (!=isalpha(GamePref)"?...
       {
          cout << "\nWrong Number!\n";
          Loop();
          loop1 = true;
       }
       else if (isalpha(GamePref))
       {
          cout << "\nThat's not a number!\n";
          Loop();
          loop1 = true;
       }
       return 0;
    }
    My biggest problem is that if I were to enter in the wrong number or a letter. The program would end. I've tried this several different ways, using while statements, case statements, ect. and the only thing I've been able to do is either A) The program closes after the wrong value is typed OR B) The program loops back and forth between MainMenu and WhateverMenuYouSelected every second. So I guess my question is "How do I get the CIN twice without having my program take the first input?"
    Last edited by Kain-Xavier; 06-26-2002 at 08:34 PM.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    63
    For the part in your main function where you try to cin a users name, use a character array, and to cin in the whole thing just type this: cin.getline(myarray, '\n');

    Is loop1 a global?
    If it isnt then you must give the loop() function an argument, else how is the MainMenu() function going to pass the value to the loop() func?

    Your code is messy, its hard to understand what it is supposed to do. So read up on coding style, just do a search on a search engine.

  3. #3
    Registered User snowy101's Avatar
    Join Date
    May 2002
    Posts
    22

    i see some problems

    ok for one thing in this part of your code

    int Loop()
    {
    if (loop1 == true)
    {
    MainMenu();
    }
    if (loop1 == false)
    {
    //Do Nothing...
    }
    } <--------------------------------------HERE
    return 0;
    }
    you have an extra } which could really do damage to your program. just had to point that out so you wouldn't be stuck for a few days wondering why your program was messy have a nice day and dont give up.
    Simple Programming

    :::: Error Message != A Smile ::::

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    2
    You know what heh. That was a typo since I cut out the rest of the Loop function. Thanks though Snowy for pointing that out. And to respond to Tudehopet. I actually abridged a lot before. I didn't show all the integers and functions I declared. Hope this helps...
    Code:
    #include <iostream.h>
    #include <ctype.h>
    #include <conio.h>  //Read something about getchar() that I might use
    
    int MainMenu();
    int QuakeMenu();
    int UnrealMenu();
    int HLMenu();
    int Loop();
    
    char UserName[16];
    int GamePref;
    int QuakePref;
    int UnrealPref;
    int HLPref;
    
    bool loop1;
    bool loop2;
    bool loop3;
    bool loop4;
    Also, I'm interested in simplifying my code if it seems messy. Perhaps you could give me some pointers? I know the whole loop function is not needed but that was just one way I was trying it. I'm open to plenty of suggestions though. Just please don't make them too over my head hehe. Thanks again.
    Last edited by Kain-Xavier; 06-26-2002 at 08:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  2. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 12:14 PM
  3. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. couple questions for newb
    By bluehead in forum C++ Programming
    Replies: 10
    Last Post: 11-24-2001, 03:32 AM

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