C++ Menu making
Page 1 of 1
Steve-O 2004




Posts: 2851

PostPosted: Sun, 19th Feb 2012 20:24    Post subject: C++ Menu making
Just wondering does anyone know whats wrong with this code? Trying to make a menu for my minesweeper C++ assignment but cant get it so that when you enter anything it says "Not a valid choice" I only want to be able to enter the values 1 - 4 oh and if you enter a character it messes up and just keeps printing out to the screen. -


Code:

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include <cmath>
#include <stdio.h>
#include <crtdbg.h>
using namespace std;

int main()
{
   
   int default;
   int choice;
   bool gameOn = true;

   while (gameOn != false)
   {
   cout << "*************************************\n";
   cout << " 1 - Start the game \n";
   cout << " 2 - Story \n";
   cout << " 3 - Help \n";
   cout << " 4 - Exit \n";
   cout << " Enter your choise and press return: ";
   
   cin >> choice;


      switch(choice)
      {
   case 1:
      cout << "game start!\n";
      // rest of game code here
      break;
   case 2:
      cout << "Story so far....\n";
      // rest code here
      break;
   case 3:
      cout << "Help\n";
      // rest code here
      break;
   case 4: ;
      cout << "End of Program \n";      
   gameOn = false;
   break;
   default;
   cout << "Not a Valid Choice \n";
   cout << "Choose again \n";
   cin >> choice;
   break;

      }
      }
   return 0;
}



Thanks


George W Bush -

'...more and more of our imports are coming from overseas.'
Back to top
Steve-O 2004




Posts: 2851

PostPosted: Sun, 19th Feb 2012 21:19    Post subject:
sorted slightly, it should be a : instead of a ; after default just need to sort the entering character problem now


George W Bush -

'...more and more of our imports are coming from overseas.'
Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Sun, 19th Feb 2012 21:56    Post subject:
uh ah, i have to fresh up my c++, but you are entering a char with cin, right? but you read it into an int.. may this lead to a problem?

fuck i should know this stuff better:lol:


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Steve-O 2004




Posts: 2851

PostPosted: Sun, 19th Feb 2012 22:17    Post subject:
yea thats what Im thinking, is there a way to stop you from entering a character instead of crashing the program? Might have a go making the menu with 'if', 'if else' and 'else'


George W Bush -

'...more and more of our imports are coming from overseas.'
Back to top
Steve-O 2004




Posts: 2851

PostPosted: Sun, 19th Feb 2012 22:39    Post subject:
ok sorted Very Happy

Ive used

cin.clear();
cin.ignore();

before each break to clear the cin

feels good when you sort it and it works Very Happy

Hardest part next, coding Minesweeper


George W Bush -

'...more and more of our imports are coming from overseas.'
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group