If, Else Statements, Correct Codes

Ok so I think I have all of the right code here, if there's anything I don't really need in there, your suggestions are welcomed. Thank you in advanced to whom responds to this post...
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include<cstdlib>
#include<fstream>
#include "random.cpp"


using namespace std;
            
int main()
{

ofstream outs;
    
int diceThrow1, diceThrow2, diceThrow3; 
double seed;   
int random(double seed);



//Describe the program to the user
    
cout<<"This program will roll three dice. Based on the outcome\n";
cout<<"of each roll, the user will win or lose a certain amount of money.\n";
cout<<"The rules state, if the user rolls and all three dice are sixes, the\n";
cout<<"user wins $50.00. If the user rolls and two dice are similar, except for\n";
cout<<"the third dice, the user wins $5.00. If the user rolls\n";
cout<<"and all three dice have similar numbers(but not sixes), the user wins\n";
cout<<"$20.00. Lastly, if the user rolls and none of the dice are\n";
cout<<"similar, the user will lose $5.00. Let us begin!\n\n\n\n";


//Prompt the user to enter either the a value between 0 and 1 to
//simulate the first roll of the dice

cout<<"Enter a value between 0 and 1 to simulate the first dice roll:\n";
cin>>(seed);
diceThrow1= random(seed);



//The following code below allows data to be inputed to the external file 
//game_results.txt when using outs.open("game_results.txt",ios::app)

outs.open("game_results.txt",ios::app); 



//Prompt the user the first number that appears on the first dice

cout<<"Your first roll came out to a "<<diceThrow1<<".\n\n";


outs<<"Your first roll came out to a "<<diceThrow1<<".\n\n";

//Prompt the user to enter either the a value between 0 and 1 to
//simulate the second roll of the dice

cout<<"Enter a value between 0 and 1 to simulate the second dice roll:\n";
cin>>seed;
diceThrow2= random(seed);

//Prompt the user the first number that appears on the second dice roll

cout<<"Your second roll came out to a "<<diceThrow2<<".\n\n";


outs<<"Your second roll came out to a "<<diceThrow2<<".\n\n";
 
//Prompt the user to enter either the a value between 0 and 1 to
//simulate the third roll of the dice

cout<<"Enter a value between 0 and 1 to simulate the third dice roll:\n";
cin>>seed;
diceThrow3= random(seed);


//Prompt the user the number that appears on the third dice roll and apply the
//if rules

cout<<"Your third roll came out to a "<<diceThrow3<<".\n\n\n";


outs<<"Your third roll came out to a "<<diceThrow3<<".\n\n\n";
       


//Present more if statments based off the given rules, as well output the results     
{ 
      if ((diceThrow1 == 6 )&& (diceThrow2 == 6) && (diceThrow3 == 6))
         outs<<"You let out a boisterous guffaw, for you have rolled all sixes.\n"
             <<"You've won $50.00!\n\n";
}
{
      if ((diceThrow1 == 6 )&& (diceThrow2 == 6) && (diceThrow3 == 6)) 
         cout<<"You let out a boisterous guffaw, for you have rolled all sixes.\n"
             <<"You've won $50.00!\n\n";           
}   
{
      if ((diceThrow1 == 5) && (diceThrow2 == 5) && (diceThrow3 == 5))
         outs<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, you have rolled all fives. You win $20.00!\n\n";
      if ((diceThrow1 == 5) && (diceThrow2 == 5) && (diceThrow3 == 5))   
         cout<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, you have rolled all fives. You win $20.00!\n\n";                       
}
{
      if ((diceThrow1 == 4) && (diceThrow3 == 4) && (diceThrow2 ==4))
         outs<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, you have rolled all fours. You win $20.00!\n\n";
      if ((diceThrow1 == 4) && (diceThrow3 == 4) && (diceThrow2 ==4))   
         cout<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, you have rolled all fours. You win $20.00!\n\n";        
}   
{
      if ((diceThrow1 == 3) && (diceThrow2 == 3) && (diceThrow3 ==3))
         outs<<"Your voice resonates through the halls as you celebrate your\n"
              <<"utmost glory, you have rolled all three's. You win $20.00!\n\n";
      if ((diceThrow1 == 3) && (diceThrow2 == 3) && (diceThrow3 ==3))   
         cout<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, you have rolled all three's. You win $20.00!\n\n";    
}
{
      if ((diceThrow1 == 2) && (diceThrow2 == 2) && (diceThrow3 ==2))
         outs<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, you have rolled all two's. You win $20.00!\n\n";
      if ((diceThrow1 == 2) && (diceThrow2 == 2) && (diceThrow3 ==2))   
         cout<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, you have rolled all two's. You win $20.00!\n\n";        
}
{
      if ((diceThrow1 == 1) && (diceThrow2 == 1) && (diceThrow3 ==1))
         outs<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, have you rolled all two's. You win $20.00!\n\n";
      if ((diceThrow1 == 1) && (diceThrow2 == 1) && (diceThrow3 ==1))   
         cout<<"Your voice resonates through the halls as you celebrate your\n"
             <<"utmost glory, you have rolled all two's. You win $20.00!\n\n";     
}


//Present more if statments based off the given rules, as well output the results
{
      if ((diceThrow2 == diceThrow3) && (diceThrow1 != diceThrow2) && (diceThrow1 != diceThrow3))
         outs<<"Your luck is running out for you have rolled only two the same.\n"
             <<"You win a mere $5.00!\n\n";
      if ((diceThrow2 == diceThrow3) && (diceThrow1 != diceThrow2) && (diceThrow1 != diceThrow3))   
         cout<<"Your luck is running out for you have rolled only two the same.\n" 
             <<"You win a mere $5.00!\n\n";        
}
{  
      if ((diceThrow3 == diceThrow1) && (diceThrow2 != diceThrow3) && (diceThrow2 != diceThrow1))
         outs<<"Your luck is running out for you have rolled only two the same.\n"
             <<"You win a mere $5.00!\n\n";
      if ((diceThrow3 == diceThrow1) && (diceThrow2 != diceThrow3) && (diceThrow2 != diceThrow1))   
         cout<<"Your luck is running out for you have rolled only two the same.\n"
             <<"You win a mere $5.00!\n\n";           
}
{
      if ((diceThrow1 == diceThrow2) && (diceThrow3 != diceThrow1) && (diceThrow3 != diceThrow2))
         outs<<"Your luck is running out for you have rolled only two the same.\n"
             <<"You win a mere $5.00!\n\n";
      if ((diceThrow1 == diceThrow2) && (diceThrow3 != diceThrow1) && (diceThrow3 != diceThrow2))   
         cout<<"Your luck is running out for you have rolled only two the same.\n"
             <<"You win a mere $5.00!\n\n";         
}       


//Present the last if statments based off the given rules, as well output the results
{
      if((diceThrow1 != diceThrow2) && (diceThrow2!= diceThrow3) && (diceThrow1 != diceThrow3))
         outs<<"The program cackles maniacally at you as none of the dice\n"
             <<"are similar. You lose your $5.00 bet!\n\n";
      if((diceThrow1 != diceThrow2) && (diceThrow2!= diceThrow3) && (diceThrow1 != diceThrow3))   
         cout<<"The program cackles maniacally at you as none of the dice\n"
             <<"are similar. You lose your $5.00 bet!\n\n";        
}


//Thank the user for participating in this program

         cout<<"\n\nThank you for participating in this dice game...\n\n";
         outs<<"\n\nThank you for participating in this dice game...\n\n";
                       
    

outs.close();                                                          
                   
system("PAUSE");
return(0);            
}
Last edited on
Well for starters (as the syntax highlight here clearly shows), you are mismatching many of your quotes for strings. Line 107, for example. The string has no opening quote, which is causing the rest of the line to be treated as code.

!> is not a C++ operator (line 67). If you want the opposite of >, use <=.

You're also mismatching braces. The close brace on line 66 has no mathcing opening brace (it's closing the function)

Don't put semicolons after if statements:
1
2
3
4
5
if(something)    // good
  do_something;

if(something);   // bad
  do_something;
___
Last edited on
you have a few logic errors:

 
if(diceThrow2 == diceThrow3 && diceThrow1 != diceThrow2 && diceThrow3)


That does not do what you might expect. You probably meant something like:

 
if((diceThrow2 == diceThrow 3) && (diceThrow1 != diceThrow2) && (diceThrow1 != diceThrow3))

(added parenthesis because that's how I code -- it isn't necessary and you don't have to do it if it's not your style)


You can't declare functions inside of other functions. Move your random() function so that it's not inside main(). I'm surprised your compiler isn't giving you an error.

It also says [linker error] undefined reference to random`(double)'.


This indicates that the function 'random' has no body. You need to supply the code that makes a random number. I assume code for this came with your assignment. random() is not a standard C/C++ function -- the closest thing is rand(), but it does not use a double seed, nor does it return a number between 1-6.

Then at the bottow it says ID return 1 status.


I don't understand what that means.
Alright so I believe everything is correctly coded. If there's any changes or any code that isn't neccessary please comment. I welcome any suggestions. Thank you in advance.
Oh I had a question,before I changed this text around. I had the outs statements after the cout statements. And when that happend it would give me all the Congratulations you've won X amount of dollars. If I am importing this information to another file and have if statements, do they always need to be added before every outs statement. Also, when I make these statements of cout and outs, must they always be between the brackets that seperate them as one big function?
Topic archived. No new replies allowed.