Function Values Not Passing

This program has been giving me a lot of issues. I'm positive the syntax is correct but the program won't compile. Here's a few sticking points

1) I must have a function I can call at the beginning of the application as well as when the user gets the answer correct

2) I must come up with a loop that causes the program the check to see if the user has given the correct answer. If so it generates a new question and if not it waits until the user gives the correct answer then loops back to the question generator.



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
  // Function Prototypes
int   numGen   ();
int   questSol ();
int   questAns ();

int main() {

    //int secAnswer = questGen();
    //int secSolution = questtGen();
    
    
    int ans = questAns();
    int sol = questSol();
    
    
    
    if (ans == sol){
        cout << "Very good! \n";
        questAns();
        
    } else {
        cout << " Incorrect. Please try again. " << endl;
        cin >> ans;
        if (ans == sol)
        questAns();
    }
}

////////////////////////////////////////
int numGen () {
    
    srand(time(0));
    int one = rand() % 10;
    int two = rand() % 10;
    
    return one;
    return two;
};

///////////////////////////////////////


int questSol (int one, int two) {
    
    
    int solution = one * two;
    
    
    return solution;
}
//////////////////////////////////////


int questAns (int one, int two) {
    
    int answer;
    
    cout << "How much is " << one << " times " << two << "? \n";
    cin >> answer;
    
    
    return answer;
}




closed account (48T7M4Gy)
No wonder it won't compile. What happened to your #includes and some sort of a reference to namespaces or similar?
closed account (1vD3vCM9)
You need to #include <iostream> for input/output on the console.
And try NOT to use using namespace std;
closed account (1vD3vCM9)
If you wont do: using namespace std;
You will have to put std:: before every cout.
Like so: std::cout << "Hello C++" << std::endl;
And the same about cin.
Last edited on
closed account (48T7M4Gy)
No ; at line 38

int ans = questAns() ; and the next line have missing parameters. Same problem with the function prototypes at lines 3 and 4.
closed account (1vD3vCM9)
True as well, I've missed that.
Sorry, I have the proper headers #include <isotream>, using namespace std, etc. My main issue, apparently, is that I have functions a bit confused. My new plan of attack is to create the random numbers in the main function, save their results as variables and then call a function using those two values. Am I on the right track?



closed account (48T7M4Gy)
Yeah, that sounds like a plan. But you need to read up a bit on functions.

OPne thing I noticed is you can't really say return solution;

I think I know what you mean but it's not C++ because unless you declare an integer variable called solution (or one, two as you've done elsewhere nothing will happen except error messages.

http://www.cplusplus.com/doc/tutorial/functions/
ok I'm still running into the same basic problem. What the code listed below should do, from my point of view, is generate two random numbers and then pass them along to the function I've called. Same error and I'm still confused. I've read the function tutorial but...I'm still missing something

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>

using namespace std;

int questGen (int, int);

int main(){

srand (time(0));
int one = rand() % 10;
int two = rand() % 10;

questGen(one, two);


// Call function and plus in one and two


return 0;
}

int questGen(){
int first;
int second;
int answer;

cout << "What's " << first << " times " << second << "?" << endl;
cin >> answer;

return answer;
}

closed account (48T7M4Gy)
The prototype and the implementation parameter list must match exactly. When you call the function the parameters have to match.

int questGen (int, int);
questGen(one, two);

int questGen() - doesn't %$@! match - STUDY the tutorial otherwise you'll just go through the same cycle of making the same mistake whereupon people will rapidly lose interest.
Last edited on
Topic archived. No new replies allowed.