You are viewing our Forum Archives. To view or take place in current topics click here.
c++ help? i have source code
Posted:
c++ help? i have source codePosted:
Status: Offline
Joined: Sep 05, 201212Year Member
Posts: 384
Reputation Power: 14
Status: Offline
Joined: Sep 05, 201212Year Member
Posts: 384
Reputation Power: 14
i cant seem to be able to build and run this source i just wrote up, can somebody help me? it expects a ] before the ; on line : cout << "hello \n";
#include <iostream>
using namespace std;
int guessingnumber;
int number = 5;
main () [
cout << "hello \n";
cout << " \n";
cout << "This is the guessing game \n";
cout << "Guess a number between 0-10 \n";
cout << " \n";
cout << " \n";
cout << "type number here please:" << guessingnumber << "| \n";
if( guessingnumber == number)
cout << " your number was correct, well done :D";
else
if (guessingnumber < number)
cout << "wrong number, try higher";
else
if (guessingnumber > number)
cout << "wrong number, try lower";
else
return main();
]
#2. Posted:
Status: Offline
Joined: Nov 05, 201113Year Member
Posts: 232
Reputation Power: 11
Status: Offline
Joined: Nov 05, 201113Year Member
Posts: 232
Reputation Power: 11
I'll write you up a script, gimme a minute..
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Sep 05, 201212Year Member
Posts: 384
Reputation Power: 14
Status: Offline
Joined: Sep 05, 201212Year Member
Posts: 384
Reputation Power: 14
thankyou, i only know the basics but cant even get this working *5paperbag5* :facepalm:
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Nov 05, 201113Year Member
Posts: 232
Reputation Power: 11
Status: Offline
Joined: Nov 05, 201113Year Member
Posts: 232
Reputation Power: 11
OK, I have not tested this and I've kinda stopped doing C++ for a few months so I'm a bit edgy (I've being learning Java and PHP etc.) but here's my rough attempt:
Sorry, I couldn't test it.. My IDE (Codeblocks) screwed up O.o
Oh yeah, I never included a loop.
#include <iostream>
using namespace std;
int main(){
int number = 5;
cout << "Hello, welcome to the guessing game." << endl;
cout << "\n Please guess a number between 1 - 10.\n\n" << endl;
int guess;
cin >> guess;
if(guess == number){
cout << "Well Done!, The number matched!" << endl;
} else if(guess < number){
cout << "Wrong, try higher!" << endl;
} else if(guess > number){
cout << "Wrong, try lower!" << endl;
} else if(guess > 10 || guess < 0){
cout << "Your guess either exceeds 10 or is lower than 0. Please choose between 1-10" << endl;
} else {
cout << "Error." << endl;
}
system("pause");
return 0;
}
Sorry, I couldn't test it.. My IDE (Codeblocks) screwed up O.o
Oh yeah, I never included a loop.
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Sep 05, 201212Year Member
Posts: 384
Reputation Power: 14
Status: Offline
Joined: Sep 05, 201212Year Member
Posts: 384
Reputation Power: 14
thankyou, system pause dint work but i added more to the source code so if you were to guess the right number, the cmd prompt would close but if you got the wrong number say 6 then it would say lower guess again and return main();
also how would i make it so it thinks of a random number itself instead of it always been 5?
[/code]
also how would i make it so it thinks of a random number itself instead of it always been 5?
#include <iostream>
using namespace std;
int main () {
int number = 5;
int playerguess;
bool alive = true;
cout << "enter a number between 0 - 10 \n";
cin >> playerguess;
if (playerguess == number && alive == true)
cout << "you guessed the right number!\n";
else
if (playerguess < number)
cout << "higher \n";
else
if (playerguess > number)
cout << "lower \n";
else
cout << "error \n";
if (playerguess == number)
return 0;
else
return main();
}
[/code]
- 0useful
- 0not useful
#6. Posted:
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
One solution to keep asking for guesses would be to wrap the functionality in a while loop. So:
while(playerguess != number){
Take guess;
Do if checks
output lower/higher
}
Output player has won
For a different number, you want to use rand() which you can read up on here: [ Register or Signin to view external links. ]
while(playerguess != number){
Take guess;
Do if checks
output lower/higher
}
Output player has won
For a different number, you want to use rand() which you can read up on here: [ Register or Signin to view external links. ]
- 0useful
- 0not useful
#7. Posted:
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
The reason for your error was because you were putting in "..main [" rather than "..main {" they use curly braces rather than square brackets.
I also noticed you missed quite a lot of them throughout your if and else statements; They work like "if(..){...}else if(..){....}else{...}"
Rather than shoving a solution in your face; I believe you can sort it out from there.
I also noticed you missed quite a lot of them throughout your if and else statements; They work like "if(..){...}else if(..){....}else{...}"
Rather than shoving a solution in your face; I believe you can sort it out from there.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.