whats wrong with this code

I'm getting these errors:
1 IntelliSense: type name is not allowed
2 IntelliSense: expected a ')' ...............
User is supposed to roll a die, get a random number and that number is output to the screen
if the user chooses no then it says 'maybe next time'..... Here is my code.......

#include <iostream>
using namespace std;
int main (){
cout<<"Do you want to roll a 6 sided die?"<<endl;
cout<<"Enter 1 for yes or 2 for no."<<endl;
int a;
cin>>a;
if (int a=1){
int r;
do {
r=rand()%7; }
while (r==0);
switch (r){
case 1:cout<<"You got a One."<<endl;break;
case 2:cout<<"You got a Two."<<endl;break;
case 3:cout<<"You got a Three."<<endl;break;
case 4:cout<<"You got a Four."<<endl;break;
case 5:cout<<"You got a Five."<<endl;break;
case 6:cout<<"You got a Six."<<endl;break;
}
}
else {
do{
cout<<"Maybe next time."<<endl;
}
while (int a!=1);
system("pause");
return 0;
}
Try this one;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
int main (){
cout<<"Do you want to roll a 6 sided die?"<<endl;
cout<<"Enter 1 for yes or 2 for no."<<endl;
int a;
cin>>a;
if (a==1){
int r;
do {
r=rand()%7; } 
while (r==0);
switch (r){
case 1:cout<<"You got a One."<<endl;break;
case 2:cout<<"You got a Two."<<endl;break;
case 3:cout<<"You got a Three."<<endl;break;
case 4:cout<<"You got a Four."<<endl;break;
case 5:cout<<"You got a Five."<<endl;break;
case 6:cout<<"You got a Six."<<endl;break;
}
}
else {
do{
cout<<"Maybe next time."<<endl;
}
while (a!=1);
system("pause");
return 0;
}
}
Last edited on
still doesnt work it just flashes if i press '1' or it would say "maybe next time" infinitely if i press 2 (or any other key)
you have under cin >> a; the statement if (int a=1){ which is wrong. You are re-declaring your variable a within a condition after it's already been declared.

Also I think you know about this but use the code tags around all code please, I could have given you number line specific answers

Also, add a seed. srand(time(NULL));. You havn't generated a seed for rand() function to work with.
Last edited on
ok i got it to work with entering 1. if i press any other key it says maybe next time but it should only say that if i press 2, anything else it should say 'invalid'.
by the way thanks for all the help everyone
Last edited on
i got it thanks for steering me in the right direction.
and do i use code tags by the way??.....

here is my final code.....
#include <iostream>
using namespace std;
int main (){
cout<<"Do you want to roll a 6 sided die?"<<endl;
cout<<"Enter 1 for yes or 2 for no."<<endl;
int a;
cin>>a;
if (a==1){
int r;
do {
r=rand()%7; }
while (r==0);
switch (r){
case 1:cout<<"You got a One."<<endl;break;
case 2:cout<<"You got a Two."<<endl;break;
case 3:cout<<"You got a Three."<<endl;break;
case 4:cout<<"You got a Four."<<endl;break;
case 5:cout<<"You got a Five."<<endl;break;
case 6:cout<<"You got a Six."<<endl;break;
}
}
else if (a==2) {
do{
cout<<"Maybe next time."<<endl;
system("pause");
return 0;
}
while (a!=1);
}
else if (a>=3) {
cout<<"Error, please try again."<<endl;
}
system("pause");
return 0;
}
Select the <> icon next to the input box here ------------------------->

and enter all your code inbetween the opening code tag and closing code tag
Last edited on
thanks!!!
Topic archived. No new replies allowed.