beginner exercise went wrong

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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

void pcguess();
void userinfo();
void intro();

int main()
{
    intro();
}

void pcguess(int useranswer, int hpcnumber, int pcnumber)
{


    cout <<endl;
    cout << "Is your number " << pcnumber <<endl;
    userinfo();
    if(useranswer == '2')
    {
        cout << "Is your number" << pcnumber <<endl;
    }//end of code for now
}

void userinfo(int useranswer, int hpcnumber, int pcnumber)
{

    cout << "1) Correct" <<endl;
    cout << "2) too low" <<endl;
    cout << "3) too high" <<endl;
    cin >> useranswer;
    if(useranswer == '1')
    {
        cout << "The computer won. Game over." <<endl;
        abort();
    }
    else if(useranswer == '2')
    {
        hpcnumber = (pcnumber / 2);
        pcnumber += hpcnumber;
        pcguess();
    }
        else if(useranswer == '3')
    {
        pcnumber = (pcnumber / 2);
        pcguess();
    }
    else
    {
        userinfo();
    }
    return useranswer, hpcnumber, pcnumber;
}

void intro()
{
    string redo;

    restart:

    cout << "Please guess a number from 1 to 100 and tell the computer whether it is correct, too high, or too low."<<endl<<endl;
    cout << "Press y and Enter to contiue" <<endl;
    cin >> redo;
    if (redo == "Y" || redo == "y")
    {
        pcguess();
    }
    else
    {
        goto restart;
    }
}


im having trouble passing variables from one function to another
Last edited on
Couple of things:
1) Line 56 - return useranswer, hpcnumber, pcnumber; - You cannot return multiple variables with "return".
2 ) You cannot return if you've declared your function "void" : Line 29.
3 ) Don't use "goto", it's bad practice.
4 ) At line 70 you call "pcguess()" with no arguments, but you define that it receives 3 arguments at line 16.

Btw, these are ( except mistake #3 ) errors that your compiler would notice, so you couldn't even compile this. First do something about that, and then I'll see about logical errors (if you have any)

Happy coding!
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

void pcguess();
int userinfo(int pcnumber);
void intro();

int main()
{
    intro();
}

void pcguess()
{

    int pcnumber = 50;
    char useranswer;

    cout <<endl;
    cout << "Is your number " << pcnumber <<endl;
    userinfo(pcnumber);
    if(useranswer == '2')
    {
        cout << "Is your number" << pcnumber <<endl;
    }
}

int userinfo(int pcnumber)
{

    char useranswer;

    cout << "1) Correct" <<endl;
    cout << "2) too low" <<endl;
    cout << "3) too high" <<endl;
    cin >> useranswer;
    if(useranswer == '1')
    {
        cout << "The computer won. Game over." <<endl;
        abort();
    }
    else if(useranswer == '2')
    {
        pcnumber +=  (pcnumber / 2);
       pcguess();
    }
        else if(useranswer == '3')
    {
        pcnumber = (pcnumber / 2);
       pcguess();
    }
    else
    {
        userinfo(pcnumber);
    }
    return pcnumber;
}

void intro()
{
    string redo;

    restart:

    cout << "Please guess a number from 1 to 100 and tell the computer whether it is correct, too high, or too low."<<endl<<endl;
    cout << "Press y and Enter to contiue" <<endl;
    cin >> redo;

    if (redo == "Y" || redo == "y")
    {
        pcguess();
    }
    else
    {
        goto restart;
    }
}


i get warning for line 25 'useranswer' is unititialized in this function?
Last edited on
I think you should to look in your code and actually SEE how it actually works! Remember to follow each line starting from main()-function. It is kind of easy to see what is wrong with your code.


REMEMBER:
a) follow each line of code starting from main() function.
b) if function is calling another function, jump there immediadly! Take a note which arguments you give (also check no GLOBAL variables are defined (as i looked it there was none)). Keep in mind from where you jumped in that function since you need to return to that exact line.
c) did any function used something that were not defined in it's code block (aka {}'s) or in the arguments?

From there you should find some errors (and maybe all)
Topic archived. No new replies allowed.