functions
Sep 27, 2011 at 1:52pm UTC
i was bored today in class so i decided to do some coding. i was trying to learn more about how functions worked. i created a simple "number guessing" program were there are 3 functions. one for getting the useres number guess, one for the main program (telling the user if he guessed right or not) and finally int main().
the problem i have is that my repeating prosess does not work like its suppose to. anyways this is my code:
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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
bool mainGame(int number){
bool tryAgain = false ;
string answer = "" ;
if (number < 821){
cout << "that number is too low\n" ;
cout << "wanna try again? " ;
cin >> answer;
if (answer == "yes" ){
tryAgain = true ;
}
else if (answer == "no" ){
tryAgain = false ;
}
else {
cout << "that is an invalid answer." ;
}
}
else if (number > 821){
cout << "that number is too high\n" ;
cout << "wanna try again? " ;
cin >> answer;
if (answer == "yes" ){
tryAgain = true ;
}
else if (answer == "no" ){
tryAgain = false ;
}
else {
cout << "that is an invalid answer." ;
}
}
else if (number == 821){
cout << "that is correct" ;
}
return tryAgain;
}
int getNumber(){
cout << "guess my number: " ;
int number = 0;
cin >> number;
return number;
}
int main(){
bool tryAgain = true ;
while (tryAgain == true ){
int number = getNumber();
bool tryAgain = mainGame(number);
}
system("Pause>nul" );
return 0;
}
can anyone tell me what i did wrong?
please keep in mind that im still just a beginner at coding so i might not have gotten anything right...
Last edited on Sep 27, 2011 at 1:53pm UTC
Sep 27, 2011 at 2:12pm UTC
on line 59, you are creating a new variable named tryAgain
don't do that. Just assign the return value.
Sep 27, 2011 at 2:22pm UTC
thanks mate. i was starting to feel stupid cause i couldn't figure it out:P
Topic archived. No new replies allowed.