I was writing this out for fun:
#include <iostream>
#include <cstdlib>
using namespace std;
int Arsenal = rand()%3;
int Norwich = rand()%3;
int main () {
if (Arsenal > Norwich) {
cout << "Arsenal win the three points, they are in the Top Four";
return 0;
} else if (Arsenal == Norwich) {
cout << "The game was a draw, both teams gained a point";
return 0;
} else (Norwich > Arsenal) {
cout << "Norwich won, Arsenal lost";
return 0;
}
}
and I tried to compile it in g++, yet I get this error:
arsenalNorwich.cpp: In function, 'int main'
arsenalNorwich.cpp:15:30: error: expected ';' before '{' token
I have no idea what I did wrong, and neither does the CS tutor at my school. Although it's just for fun, it's driving me crazy.
you missed one if
here:
else if (Norwich > Arsenal)
///^^^if missing
Meanwhile, it is not good to put
int Arsenal = rand()%3;
int Norwich = rand()%3;
before main
. Another point is that you should first set random seed before calling rand()
.
Your if-else
can be simplified as follows:
if (Arsenal > Norwich) {
cout << "Arsenal win the three points, they are in the Top Four";
} else if (Arsenal == Norwich) {
cout << "The game was a draw, both teams gained a point";
} else { //^^^no need to compare values again since it must be Norwich > Arsenal
//when execution reaches this point
cout << "Norwich won, Arsenal lost";
}
return 0;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.