This is my first game/program, the drinking game. The problem I'm having is the 'drinks' function isn't displaying any text, it always just displays 1. Why/how would it even display a number? Also, for my drinks function, is it better to use a switch like I am or put the choices into an array of strings and choose from there?
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#define CHOICES 7
#define TIME 10
void drinks();
int rand0toN(int n);
usingnamespace std;
int main(){
int p;
srand(time(NULL)); // Set seed for Randomizing
//Prompt user with instructions and ask for players
cout << "~~~~~Welcome to the drinking game!!~~~~~\n\n";
cout << "Instructions:\n";
cout << "1: Enter number of players and assign each player a number.\n";
cout << "2: Each player has 10 seconds to complete drinking task. Start when\n";
cout << "it says go.\n";
cout << "3: If can/glass is not on the table before time is up\n";
cout << "players loses and is out of the game.\n\n";
cout << "Enter number of players(0 to exit): ";
cin >> p;
//Cycle through number of players, 0 to exit.
while(p!=0){
for(int i=1;i<=p;i++){
cout << "Player " << i << endl;
cout << drinks << " Ready... Set...\n";
Sleep(6000);//Give them some time to read the challenge.
cout << "GOOOOO!\n";
//Count down timer.
for(int s=1;s<=TIME;s++){
Sleep(1000);
cout << s << endl;
}
cout << "Time's up!!!\n\n";
Sleep(1000);
}
cout << "Enter new number of players.(0 to exit)";
cin >> p;
}
system("pause");
return 0;
}
//This function makes a random choice for the player.
void drinks(){
int c = rand0toN(CHOICES);
switch(c){
case 1:
cout << "Take 1 gulps!";
break;
case 2:
cout << "Take 2 gulps!";
break;
case 3:
cout << "Take 3 gulps, or 1 shot!";
break;
case 4:
cout << "Slam your drink! Go, go, go!";
break;
case 5:
cout << "Choose someone for 2 gulps! Pick quick!";
break;
case 6:
cout << "Choose someone for 3 gulps or 1 shot! Pick quick!";
break;
case 7:
cout << "Take shots until the clock runs out!(Or slam your drink) Go, fast!";
break;
}
}
//Random number selection for function "drinks"
int rand0toN(int n){
return (rand()%n)+1;
}
Also your switch case doesn't have a default argument, why?
That's simple, I forgot. It's my first program, so I'm bound to forget some things. But of course I overlook simple things like "()" on the end of my function. Thanks for your help!
Here is the completed version. Any ways to make it more efficent?
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#define CHOICES 7
#define TIME 10
void drinks();
int rand0toN(int n);
usingnamespace std;
int main(){
int p;
srand(time(NULL)); // Set seed for Randomizing
//Prompt user with instructions and ask for players
cout << "~~~~~Welcome to the drinking game!!~~~~~\n\n";
cout << "Instructions:\n";
cout << "1: Enter number of players and assign each player a number.\n";
cout << "2: Each player has 10 seconds to complete drinking task. Start when\n";
cout << "it says go.\n";
cout << "3: If can/glass is not on the table before time is up\n";
cout << "players loses and is out of the game.\n\n";
cout << "Enter number of players(0 to exit): ";
cin >> p;
//Cycle through number of players, 0 to exit.
while(p!=0){
for(int i=1;i<=p;i++){
cout << "Player " << i << endl;
drinks();
cout << " Ready... Set...\n";
Sleep(6000);//Give them some time to read the challenge.
cout << "GOOOOO!\n";
//Count down timer.
for(int s=1;s<=TIME;s++){
Sleep(1000);
cout << s << endl;
}
cout << "Time's up!!!\n\n";
Sleep(1000);
}
cout << "Enter new number of players.(0 to exit)";
cin >> p;
}
system("pause");
return 0;
}
//This function makes a random choice for the player.
void drinks(){
int c = rand0toN(CHOICES);
switch(c){
case 1:
cout << "Take 1 gulps!";
break;
case 2:
cout << "Take 2 gulps!";
break;
case 3:
cout << "Take 3 gulps, or 1 shot!";
break;
case 4:
cout << "Slam your drink! Go, go, go!";
break;
case 5:
cout << "Choose someone for 2 gulps! Pick quick!";
break;
case 6:
cout << "Choose someone for 3 gulps or 1 shot! Pick quick!";
break;
case 7:
cout << "Take shots until the clock runs out!(Or slam your drink) Go, fast!";
break;
default:
break;
}
}
//Random number selection for function "drinks"
int rand0toN(int n){
return (rand()%n)+1;
}
Right on, I'm new to C++ programming as well, I wasn't sure if there was something going on that I wasn't aware of haha, like some special use of switch. Good luck!
edit: first program? pretty ambitious if you ask me! way to go.
edit #2:
1 2 3 4 5 6
case 7:
cout << "Take shots until the clock runs out!(Or slam your drink) Go, fast!";
break;
default:
break;
}
make case 7 the default case and it looks great to me! Also are you actually using the windows.h header file? I'm not to knowledgeable when it comes to c++ but I thought that was more for GUI kind of programming.
Well I decided I would teach myself programming about a week ago. I was going to start with Java, but I like C++ better. I'm about finished reading C++ without fear so I thought I would go ahead and start writing some programs. This is the best I could come up with to start. I am ambitious, I love programming! (That's a nerd statement if ever heard one.) Anyway, thanks for your help again.