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'
{
cout << "\ninvalid input detected try again: ";
cin >> playagain;}
}while (playagain == 'y'
;
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.
#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'
{
cout << "\ninvalid input detected try again: ";
cin >> playagain;}
}while (playagain == 'y'
;
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'
{
cout << "\ninvalid input detected try again: ";
cin >> playagain;}
}while (playagain == 'y'
;
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.