Code Error Help :(

NEED HELP! I am working on my code and line largest(num1,num2) are giving me an error. They say they are not declared in the scope. Anyone know why?

Thanks in advance
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>
#include <string>
#include <sstream>

using namespace std;

string name();
void welcome(string input);

int menu(int answer);
int print=0;

void largest(int num1, int num2);

int main() {
    string input=name();
    welcome(input);
    int answer;
    bool keeplooping = true;
    
    while (keeplooping)
    {
        cout << "1- Monday\n";
        cout << "2-Tuesday\n";
        cout << "3-Wednesday\n";
        cout << "4-Thursday\n";
        cout << "5-Friday\n";
        cout << "6-Saturday\n";
        cout << "7-Sunday\n";
        cout << " Please Enter the Number of the Day (1-7): ";
        cin >> answer;
        
        menu(answer);
        
        if (answer < 1 || answer > 7)
        {
            keeplooping = true;
        }
        else
        {
            keeplooping = false;
        }
    }
    largest (num1,num2);
    return 0;
}

string name (){
    string input = "";
    cout << "Please enter your name (first and last):\n>";
    getline(cin, input);
    return input;
}
void welcome(string input)
{
    cout << "Welcome " << input<< "!"<< endl << endl;
}
int menu(int answer)
{
    switch (answer)
    {
        case 1:
            cout << "So today is Monday!\n";
            break;
        case 2:
            cout << "So today is Tuesday!\n";
            break;
        case 3:
            cout << "So today is Wednesday!\n";
            break;
        case 4:
            cout << "So today is Thursday!\n";
            break;
        case 5:
            cout << "So today is Friday!\n";
            break;
        case 6:
            cout << "So today is Saturday!\n";
            break;
        case 7:
            cout << "So today is Sunday!\n";
            break;
        default:
            cout << "Looks like that isn't a good day! \n";
            cout << "Choose again.\n";
            
    }
    return answer;
}
void largest(int num1, int num2)
    {
        int max = 0;
        
        cout << "Enter two integers: ";
        cin >> num1 >> num2;
        cin.ignore();
        
        cout << "The maximum number is " << max << endl;
        
        cin.ignore();
        if(num1 > num2)
        {	
            if(num1 > max)
                max = num1;
        }
        else if (num2 > num1)
        {
            if (num2 > max)
                max = num2;
        }
    }
Last edited on
closed account (E0p9LyTq)
So what is the error?

You posted your code, that is helpful. But not telling us exactly what the error is not.
The error your compiler gives you is:
44|error: 'num1' was not declared in this scope
44|error: 'num2' was not declared in this scope

If you than look at your code you will find that the compiler is right, you are calling the function with two undefined arguments. num1 and num2 could be std::strings for all the compiler knows, it sure is not able to guess what the value of those undefined variables should be.

Try changing line 44 to largest ( 7 , 42 );
Hi Nico, that is exactly my problem. Do you mean just the values 7 and 42? Why is that?
Because they are integers, you could put any integer value there, but I like lucky seven and the answer to everything :-p

You probably want to make two int variables, you could even call then num1 and num2, but you have to make those variables and assign a value to them before you call the function. I would suggest initializing them to 7 and 42 (I am that original), but it could be any integer value for as long as it is defined before your program reaches the function call.
Hi Nico,

I thought I had already assigned them? Have I not?
I tried doing exactly that but then what do I put under main for the last part of my code to actually run. It ends before the void largest function is even called. This is my code thus far...
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string name();
void welcome(string input);

int menu(int answer);
int hello();

void largest(int num1, int num2);

int main() {
    string input=name();
    welcome(input);
    int answer;
    bool keeplooping = true;
    
    while (keeplooping)
    {
        cout << "1- Monday\n";
        cout << "2-Tuesday\n";
        cout << "3-Wednesday\n";
        cout << "4-Thursday\n";
        cout << "5-Friday\n";
        cout << "6-Saturday\n";
        cout << "7-Sunday\n";
        cout << " Please Enter the Number of the Day (1-7): ";
        cin >> answer;
        
        menu(answer);
        
        if (answer < 1 || answer > 7)
        {
            keeplooping = true;
        }
        else
        {
            keeplooping = false;
        }
    }
    int x=hello();
    void largest();
    int num1=0;
    int num2=0;
    return 0;
}

string name (){
    string input = "";
    cout << "Please enter your name (first and last):\n>";
    getline(cin, input);
    return input;
}
void welcome(string input)
{
    cout << "Welcome " << input<< "!"<< endl << endl;
}
int menu(int answer)
{
    switch (answer)
    {
        case 1:
            cout << "So today is Monday!\n";
            break;
        case 2:
            cout << "So today is Tuesday!\n";
            break;
        case 3:
            cout << "So today is Wednesday!\n";
            break;
        case 4:
            cout << "So today is Thursday!\n";
            break;
        case 5:
            cout << "So today is Friday!\n";
            break;
        case 6:
            cout << "So today is Saturday!\n";
            break;
        case 7:
            cout << "So today is Sunday!\n";
            break;
        default:
            cout << "Looks like that isn't a good day! \n";
            cout << "Choose again.\n";
            
    }
    return answer;
}
int hello(){
    int x=0;
    cout<<"Enter number of times to repeat: ";
    cin>>x;
    for(int i=0;i<x;++i)
        cout<<"Hello!\n";
    return x;
}
void largest()
{
    int num1, num2;
    int max = 0;
    
    cout << "Enter two integers: ";
    cin >> num1 >> num2;
    
    if(num1 > num2)
    {
        if(num1 > max)
            max = num1;
    }
    else if (num2 > num1)
    {
        if (num2 > max)
            max = num2;
    }
    cout << "The maximum number is " << max << endl;
}
Last edited on
Would you mind copying and pasting what you mean?
Functions should strive to only do one thing and do it well. So largest() should only be comparing two numbers to find the largest and return it. The problem arises that you have to overload the function so that if a user passes float or double instead of int the function has a variant to return it instead of cutting off the float or double. I could write three functions, or I could use Templates (a little advanced but largest() is the best time to show it).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

template <typename Type>
Type largest(Type t1, Type t2)
{
	return (t1 > t2) ? t1 : t2;
}

int main()
{
	double dMax = largest(2.0, 3.4);
	float fMax = largest(43.0f, 23.2f);
	int iMax = largest(10, 3);
	
	std::cout << "dMax largest:\t " << dMax << '\n'
	          << "fMax largest:\t " << fMax << '\n'
	          << "iMax largest:\t " << iMax << std::endl;
	
	return 0;
}
dMax largest:    3.4
fMax largest:    43
iMax largest:    10
Topic archived. No new replies allowed.