game menu problems

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2358 Views - Last Post: 26 September 2010 - 04:29 PM Rate Topic: -----

#1 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

game menu problems

Posted 25 September 2010 - 12:24 PM

I am trying create the guessing number game but im adding a little bit more items to it but i am having some serious problems in putting them all together. I can create a guess the number game in one file, which i have the code, i can create the menu well kinda but on my menu the following...

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	cout << "Menu\n\n";
	cout << "1 - 1 player\n";
	cout << "2 - 2 player\n";
	cout << "3 - High Scores\n";
	cout << "4 - Exit\n\n";


	int choice;
	cout << "Choice: ";
	cin >> choice;

	switch (choice)
	{
	case 1:	
			cout << "Your the only one\n";
			break;
	case 2:	
			cout << "2 players it is.\n";
			break;
	case 3:	
			cout << "High scores.\n";
			break;
	default:
			cout << "You made an illegal choice.\n";
	}

	
	system("PAUSE");
	return 0;

}


///but with this it will only show one line of text. but what i want it to do is option one is play the game that i made ///with only one player. then option 2 have it play with two players. and now for my high scores i have this...

// High Scores
// Demonstrates algorithms

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    vector<int>::const_iterator iter;

    cout << "Creating a list of scores.";
    vector<int> scores;
    scores.push_back(1500);
    scores.push_back(3500);
    scores.push_back(7500);

    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;
        
    cout << "\nFinding a score.";
    int score;
    cout << "\nEnter a score to find: ";
    cin >> score;
    iter = find(scores.begin(), scores.end(), score);
    if (iter != scores.end())
        cout << "Score found.\n";
    else
        cout << "Score not found.\n";

    cout << "\nRandomizing scores.";
    srand(time(0));
    random_shuffle(scores.begin(), scores.end());
    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;

    cout << "\nSorting scores.";
    sort(scores.begin(), scores.end());
    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;
    
 return 0;
}



at which i only know how to make it that the scores are already there. But i want it to have it to were i can enter the players high scores while in the game.


then last but not least option four is end the game which i alread have that on the options menu.

so if any one can help me piece all these individual programs together id greatly appreciate it.

This post has been edited by JackOfAllTrades: 25 September 2010 - 01:04 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: game menu problems

#2 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: game menu problems

Posted 25 September 2010 - 12:29 PM

:code:
Was This Post Helpful? 0
  • +
  • -

#3 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

Re: game menu problems

Posted 25 September 2010 - 12:32 PM

MENU
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	cout << "Menu\n\n";
	cout << "1 - 1 player\n";
	cout << "2 - 2 player\n";
	cout << "3 - High Scores\n";
	cout << "4 - Exit\n\n";


	int choice;
	cout << "Choice: ";
	cin >> choice;

	switch (choice)
	{
	case 1:	
			cout << "Your the only one\n";
			break;
	case 2:	
			cout << "2 players it is.\n";
			break;
	case 3:	
			cout << "High scores.\n";
			break;
	default:
			cout << "You made an illegal choice.\n";
	}

	
	system("PAUSE");
	return 0;

}



The game

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
	int yourHealth = 100;
	int oppHealth = 100;
	int yourAttack;
	int oppAttack;

	srand(time(0));	

	while(yourHealth > 0 && oppHealth > 0)
	{
		cout << "Your health: " << yourHealth << endl;
		cout << "opponent's health: " << oppHealth << endl << endl;
		system("PAUSE");
		system("CLS");
		
		yourAttack = rand() % 5 + 6;
		oppAttack = rand() % 5 + 6;

		cout << "you hit for " << yourAttack << endl;
		cout << "your opponent hit for " << oppAttack << endl;

		yourHealth -= oppAttack;
		oppHealth -= yourAttack;
		
		system("PAUSE");
		system("CLS");


	}
	if(yourHealth <= 0 && oppHealth <= 0)
		cout << "A double knockout!" << endl;
	else if(yourHealth > 0)
		cout << "you won! Congrats at winning something that took no skill you are now stupid for playing" << endl;
	else
		cout << "You Lose....and really suck at life" << endl;
	
	cout << endl;

	system("PAUSE");
	return 0;

}



High score

// High Scores
// Demonstrates algorithms

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    vector<int>::const_iterator iter;

    cout << "Creating a list of scores.";
    vector<int> scores;
    scores.push_back(1500);
    scores.push_back(3500);
    scores.push_back(7500);

    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;
        
    cout << "\nFinding a score.";
    int score;
    cout << "\nEnter a score to find: ";
    cin >> score;
    iter = find(scores.begin(), scores.end(), score);
    if (iter != scores.end())
        cout << "Score found.\n";
    else
        cout << "Score not found.\n";

    cout << "\nRandomizing scores.";
    srand(time(0));
    random_shuffle(scores.begin(), scores.end());
    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;

    cout << "\nSorting scores.";
    sort(scores.begin(), scores.end());
    cout << "\nHigh Scores:\n";
    for (iter = scores.begin(); iter != scores.end(); ++iter)
        cout << *iter << endl;
    
 return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: game menu problems

Posted 25 September 2010 - 12:36 PM

You can only have one main function in a C++ program.

Add a case for entering number 4 as its currently "an illegal choice."

This tutorial on Functions may be useful. Give the main funcitons a name and call them from ONE main function.

This post has been edited by simeesta: 25 September 2010 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

Re: game menu problems

Posted 25 September 2010 - 12:46 PM

I realize that i would need one funtion only. I think i didnt word my question right sorry about. But what i was trying to say what that while the user is at the menu they would press 1 for the game for 1 player and what i want it to do is start the game which is the second code. Ive tried taking out the
break;
at the end of the
case 1:	
	cout << "Your the only one\n";
	break; 


and then pasting the game under the cout but that just starts the game underneath the menu until after i make a guess it will then clear the screen and i keep on playing the game.

So all in all how do I attach the game to the first option meaning
case 1:

Was This Post Helpful? 0
  • +
  • -

#6 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: game menu problems

Posted 25 September 2010 - 12:51 PM

Your 'Game' is in a main function, as are your menu and highscore. Use other names - game and menu would be good e.g.

int game()
{
   //code here
}


int game();
int main()
{
  int a=1;
  switch(a)
  {
    case 1 : cout << "1 Player";
             game();//call the game function
             break;
  }
//etc.


This post has been edited by simeesta: 25 September 2010 - 12:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

Re: game menu problems

Posted 25 September 2010 - 12:57 PM

Awesome man thanks a whole bunch.
Was This Post Helpful? 0
  • +
  • -

#8 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: game menu problems

Posted 25 September 2010 - 01:46 PM

Also don't use
system("pause");
it's bad for many reasons. Instead use
 cin.get();
and sometimes that dosn't work if there is already input in the buffer then use
cin.ignore(); 
cin.get();


Also if you want to have letters on your menu instead of numbers you can do this.

P.S I don't use this much white space in my programs this was just a example i made in the websites text editor and you can't format code that easily in the text editor.
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	cout << "Menu\n\n";
	cout << "P - 1 player\n";
	cout << "PP - 2 player\n";
	cout << "H - High Scores\n";
	cout << "E - Exit\n\n";


	char choice;
	cout << "Choice: ";
	cin >> choice;

	switch (choice)
	{
	case 'p':
        case 'P':
             {	
			cout << "Your the only one\n";


              }
			break;
	case 'pp':
        case 'PP':
 
                {	
			cout << "2 players it is.\n";

                 }
			break;
	case 'h':
        case 'H': // so lowercase h can be entered and uppercase a you can also use toupper() google it.
                
                   {	
			cout << "High scores.\n";

                    }
			break;
                  

        case 'e':
        case 'E':
  
            {
             
           cout << "Exiting..." << endl;
 
            }
        break;

	default:
                 {
			cout << "You made an illegal choice.\n";

                 }

        break;
	}

	
	cin.get();
	return 0;

}


Hope i was helpful if i was please click the green plus button.

This post has been edited by Alex6788: 25 September 2010 - 07:10 PM

Was This Post Helpful? 1
  • +
  • -

#9 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: game menu problems

Posted 25 September 2010 - 03:53 PM

I thought you can't store 'pp' or 'PP' in choice as 'pp' is a string not a character.
Was This Post Helpful? 0
  • +
  • -

#10 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: game menu problems

Posted 25 September 2010 - 05:09 PM

View Postsimeesta, on 25 September 2010 - 04:53 PM, said:

I thought you can't store 'pp' or 'PP' in choice as 'pp' is a string not a character.

You sure because i've used stuff like 'st' for stats before in programs and it works fine.

This post has been edited by Alex6788: 25 September 2010 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#11 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: game menu problems

Posted 25 September 2010 - 05:52 PM

View PostAlex6788, on 26 September 2010 - 01:09 AM, said:

View Postsimeesta, on 25 September 2010 - 04:53 PM, said:

I thought you can't store 'pp' or 'PP' in choice as 'pp' is a string not a character.

You sure because i've used stuff like 'st' for stats before in programs and it works fine.

The code doesn't compile on g++. It says 'pp' it too large for type char. it

errors

a.cpp:32: warning: case label value exceeds maximum value for type
a.cpp:33: warning: case label value exceeds maximum value for type

This post has been edited by simeesta: 25 September 2010 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#12 blaxarbush  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 25-September 10

Re: game menu problems

Posted 25 September 2010 - 06:01 PM

View PostAlex6788, on 25 September 2010 - 04:09 PM, said:

View Postsimeesta, on 25 September 2010 - 04:53 PM, said:

I thought you can't store 'pp' or 'PP' in choice as 'pp' is a string not a character.

You sure because i've used stuff like 'st' for stats before in programs and it works fine.

ok i got the code ive placed the game into the menu but it comes up saying that the int main(void) alread has a body. and another error 'argument' : conversion from 'time_t
to 'unsigned int', possible loss of data. Now i did not chage the code that you provide excpet for adding
int game();
ive even tried taking away the ; and it just comes up with more errors.

This post has been edited by blaxarbush: 25 September 2010 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: game menu problems

Posted 25 September 2010 - 06:06 PM

You still can only have one function called main(). so
int main()
{
     //code here
}


should only appear once. it appears 3 times in yours.

try posting your new code - and for now stick with the numbers.

edit

ahh it could be function prototypes. perhaps you should have another look at the link i posted earlier.

This post has been edited by simeesta: 25 September 2010 - 06:09 PM

Was This Post Helpful? 0
  • +
  • -

#14 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: game menu problems

Posted 25 September 2010 - 06:30 PM

View Postsimeesta, on 25 September 2010 - 06:52 PM, said:

View PostAlex6788, on 26 September 2010 - 01:09 AM, said:

View Postsimeesta, on 25 September 2010 - 04:53 PM, said:

I thought you can't store 'pp' or 'PP' in choice as 'pp' is a string not a character.

You sure because i've used stuff like 'st' for stats before in programs and it works fine.


The code doesn't compile on g++. It says 'pp' it too large for type char. it

errors


a.cpp:32: warning: case label value exceeds maximum value for type

a.cpp:33: warning: case label value exceeds maximum value for type

I compiled it with Microsoft Visual C++ 2008 express edition and it works fine.
Was This Post Helpful? 0
  • +
  • -

#15 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: game menu problems

Posted 25 September 2010 - 06:37 PM

Does it run correctly when you enter 'pp' or any of the other characters??
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2