Post: user function help? C++
02-06-2012, 10:26 PM #1
snig08
Keeper
(adsbygoogle = window.adsbygoogle || []).push({}); ok so i have created a simple subtracting game but i have found it is way to hard to beat. so i want to add the option to use two players and still have single player. i have coded this whole single player program inside of the main function but what i want is to move this chunk of code inside of a user defined function and then code another function called twoplayer then use a simple switch statement inside the main function to all the selected mode. i am having trouble in creating the function. i have tried declaring void twoplayer() ....MAIn FUNCTION HERE... followed then by a copy and paste of the important code inside the twoplayer function.

right now i do not have access to my program but if no one has suggestions then i will get it and post it here. thanks in advance.
02-06-2012, 10:33 PM #2
|C++|
< ^ > < ^ >
Originally posted by snig08 View Post
ok so i have created a simple subtracting game but i have found it is way to hard to beat. so i want to add the option to use two players and still have single player. i have coded this whole single player program inside of the main function but what i want is to move this chunk of code inside of a user defined function and then code another function called twoplayer then use a simple switch statement inside the main function to all the selected mode. i am having trouble in creating the function. i have tried declaring void twoplayer() ....MAIn FUNCTION HERE... followed then by a copy and paste of the important code inside the twoplayer function.

right now i do not have access to my program but if no one has suggestions then i will get it and post it here. thanks in advance.


i see, you want the same mechanics but implement them into 2 player.
i dont understand the game, but you need to ask for the input of the 2nd player and replace it with the answer of the ai.

i dont know if that makes any sense to you
02-07-2012, 12:39 AM #3
snig08
Keeper
Yeah i know what you mean and i know how i will create both modes the problem is, is that i need to create two functions but i dont know how. I think i am titling them wrong i use
Void oneplayer{copy and paste from main} i will upload the code tomorrow but thanks Happy
02-07-2012, 04:53 AM #4
Epic?
Awe-Inspiring
Originally posted by snig08 View Post
Yeah i know what you mean and i know how i will create both modes the problem is, is that i need to create two functions but i dont know how. I think i am titling them wrong i use
Void oneplayer{copy and paste from main} i will upload the code tomorrow but thanks Happy


You should upload your code as soon as possible, it's hard to fix a problem without context.

The following user thanked Epic? for this useful post:

Docko412
02-07-2012, 06:53 AM #5
Originally posted by snig08 View Post
Yeah i know what you mean and i know how i will create both modes the problem is, is that i need to create two functions but i dont know how. I think i am titling them wrong i use
Void oneplayer{copy and paste from main} i will upload the code tomorrow but thanks Happy


agreed with epic? would be much quicker of a problem solved if we could see your code.

and it shouldn't be too hard. you can get two users input by using cin>>variable;
or use getline depending on how your game works.

The following user thanked Docko412 for this useful post:

Epic?
02-07-2012, 11:10 AM #6
snig08
Keeper
ok heres my code i have single player just how i want it and multiplayer wont be to hard.

#include <iostream>
using namespace std;

int main()
{
char playagain = 'y';

do
{
int number, total = 21;
system("CLS");

cout << "Welcome to snigs super subtracting game." << endl << "The rules are:\nyou will take 1 or 2 away from the total number.\nthen the computer will do the same.\nthe first one to get the number to 0 wins." << endl;
cout << "\nThe total is: " << total << endl;
while (total >= 0)
{
cout << "----------------------------------------\n";
cout << "Your Turn.\n You MUST choose 1 or 2.\n";
cin >> number;

while(number < 1 || number > 2)
{cout << "this is an invalid number try again.\n";
cin >> number;
}
total = total - number;
cout << "The new total is " << total << endl;

if (total == 0)
{cout << "**YOU WIN.**";
break;}

cout << "Computers turn.\n";
if ((total % 3) == 2)
{
cout << "Im going to subtract 2.\n";
total = total - 2;
}
else
{
total --;
cout << "\nI am Subtracting 1.\n";
}
cout << "\nThe new total is: " << total << endl;
if (total == 0)
{
cout << "\n**I WIN YOU'RE A LOOSER.**\n";
break;
}

}
cout << "\ndo you want to play again? (y/n)";
cin >> playagain;
while (playagain != 'y' && playagain != 'n'Winky Winky
{
cout << "\ninvalid input detected try again: ";
cin >> playagain;}


}while (playagain == 'y'Winky Winky;
cout << endl;
system ("PAUSE");
return 0;
}


i just need to take all of the code inside of main and turn it into a function. so that later on i can just use the two functions in a switch statement. i am sort of new to this but im sure it can be done. thanks

---------- Post added at 11:10 AM ---------- Previous post was at 09:23 AM ----------

ok guys i have made some progress on what i want it to do. here is the new code. it doesn't work but its ok becuase thats just fine tuning. Happy




#include<iostream>

using namespace std;

void single(void);
void two(void);

int main()
{
int choice;

cout << "Enter 1 to play single player/Enter 2 to play with a friend." << endl;
cin >> choice;

if (choice == 1){
single();}
if (choice == 2){
two();}
//if (choice 1 < || > 2){
//cout << "Invalid Choice Try Again." << endl;
//
//cin >> choice;}

system ("PAUSE");
return 0;}


void single(void)
{
char playagain = 'y';

do
{
int number, total = 21;
system("CLS");

cout << "Welcome to snigs super subtracting game --SINGLE PLAYER--." << endl << "The rules are:\nyou will take 1 or 2 away from the total number.\nthen the computer will do the same.\nthe first one to get the number to 0 wins." << endl;
cout << "\nThe total is: " << total << endl;
while (total >= 0)
{
cout << "----------------------------------------\n";
cout << "Your Turn.\n You MUST choose 1 or 2.\n";
cin >> number;

while(number < 1 || number > 2)
{cout << "this is an invalid number try again.\n";
cin >> number;
}
total = total - number;
cout << "The new total is " << total << endl;

if (total == 0)
{cout << "**YOU WIN.**";
break;}

cout << "Computers turn.\n";
if ((total % 3) == 2)
{
cout << "Im going to subtract 2.\n";
total = total - 2;
}
else
{
total --;
cout << "\nI am Subtracting 1.\n";
}
cout << "\nThe new total is: " << total << endl;
if (total == 0)
{
cout << "\n**I WIN YOU'RE A LOOSER.**\n";
break;
}

}
cout << "\ndo you want to play again? (y/n)";
cin >> playagain;
while (playagain != 'y' && playagain != 'n'Winky Winky
{
cout << "\ninvalid input detected try again: ";
cin >> playagain;}


}while (playagain == 'y'Winky Winky;
cout << endl;
system ("PAUSE");
return;
}


void two(void){
char playagain = 'y';

do
{
int number, total = 21;
system("CLS");

cout << "Welcome to snigs super subtracting game. --TWO PLAYER--" << endl << "The rules are:\nP1 will take 1 or 2 away from the total number.\nthen P2 will do the same.\nthe first player to get the number to 0 wins." << endl;
cout << "\nThe total is: " << total << endl;
while (total >= 0)
{
cout << "----------------------------------------\n";
cout << "P1 Turn.\n You MUST choose 1 or 2.\n";
cin >> number;

while(number < 1 || number > 2)
{cout << "this is an invalid number try again.\n";
cin >> number;
}
total = total - number;
cout << "The new total is " << total << endl << "---------------------------------------";

if (total == 0)
{cout << "**P1 WIN.**";
break;}

cout << "P2 turn.\n";
while(number < 1 || number > 2)
{cout << "this is an invalid number try again.\n";
cin >> number;
}
total = total - number;
cout << "The new total is " << total << endl;
cout << "\nThe new total is: " << total << endl;
if (total == 0)
{
cout << "\n**P2 WIN.**\n";
break;
}

}
cout << "\ndo you want to play again? (y/n)";
cin >> playagain;
while (playagain != 'y' && playagain != 'n'Winky Winky
{
cout << "\ninvalid input detected try again: ";
cin >> playagain;}


}while (playagain == 'y'Winky Winky;
cout << endl;
system ("PAUSE");
return;
}





if you look you can see my int main is very short that i what i ment at the start.
02-11-2012, 03:47 PM #7
Default Avatar
mikaman
Guest
mate, try to use stackoverFlow when you got problems with such huge codes. SO really helps very fast.
02-13-2012, 03:32 AM #8
|C++|
< ^ > < ^ >
Originally posted by mikaman View Post
mate, try to use stackoverFlow when you got problems with such huge codes. SO really helps very fast.


this code is not huge
02-13-2012, 09:11 PM #9
|C++|
< ^ > < ^ >
Originally posted by Docko412 View Post
lol right. also i don't really see how a stack overflow will help in this situation.
alls he has to do is get input for the same player twice.

like this for example
    
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::cin;

int main(){
int player1;
int player2;
int match = 3;
/*can replace the regular input number with match e.g.
if(player1 == match){}
instead of
if(player1 == match){}
*/
cout<<"Enter Number(player one): ";
cin>>player1;
cout<<"Enter Number(player two): ";
cin>>player2;
if(player1 == 3 && player2 != 3){
cout<<"Player one is correct - Winner!"<<endl;
cout<<"Player two is incorrect - Loser!"<<endl;
}
else if(player2 == 3 && player1 != 3){
cout<<"Player two is correct - Winner!"<<endl;
cout<<"Player one is incorrect - Loser!"<<endl;
}
else if(player2 && player1 == 3){
cout<<"You both guessed it - Winners!"<<endl;
}
else{
cout<<"Neither player is correct! - You both lose!"<<endl;
}
return 0;
}


ikr, that wasn't too hard

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo