What am I doing wrong???

closed account (ENhkSL3A)
I keep getting some errors and I need to know what to do to stop them!

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

int first()
{
	int flag;
	int sentinel = 999;

	cout << "Please input value:  \n" << endl;
	cin >> flag;
	cin.get();

	while (flag != sentinel)
	{
	if (flag < 10)
	{ 
		cout << "The wire is: White" << endl;
		first();
	}
	else 
	if (flag < 30)
	{
		cout << "The wire is: Green" << endl;
		first();
	}
	else
	if (flag < 40)
	{
		cout << "The wire is: Blue" << endl;
		first();
	}
	else
	if (flag >= 40)
	{
		cout << "The wire is: Red" << endl;
		first();
	}
	else 
	if (flag = (999)
	{
	cout << "Have a great day & Thank You for using Katie's Program!" << endl;
	}
	else
	{
	    first();
	}
}
int main()
{
    cout << "Exercise 6A" << endl;
    cout << "Kaitlin N. Stevers" << endl;
    cout << "September 25, 2016" << endl;
    cout << "\t\t" << endl;
    cout << "\t\t" << endl;
    first();
}




closed account (LA48b7Xj)
I got it to compile and made a few changes. One of the if statements wasn't ended with a parentheses, the while statement wasn't ended with a brace, the last if statement has flag = (999), I'm guessing this wasn't intended. Also I changed the function's return value to void because it didn't return a value.

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

void first()
{
    int flag;
    int sentinel = 999;

    cout << "Please input value:  \n" << endl;
    cin >> flag;
    cin.get();

    while (flag != sentinel)
    {
        if (flag < 10)
        {
            cout << "The wire is: White" << endl;
            first();
        }
        else if (flag < 30)
        {
            cout << "The wire is: Green" << endl;
            first();
        }
        else if (flag < 40)
        {
            cout << "The wire is: Blue" << endl;
            first();
        }
        else if (flag >= 40)
        {
            cout << "The wire is: Red" << endl;
            first();
        }
        else if (flag == (999))
        {
            cout << "Have a great day & Thank You for using Katie's Program!" << endl;
        }
        else
        {
            first();
        }
    }
}

int main()
{
    cout << "Exercise 6A" << endl;
    cout << "Kaitlin N. Stevers" << endl;
    cout << "September 25, 2016" << endl;
    cout << "\t\t" << endl;
    cout << "\t\t" << endl;
    first();
}
Last edited on
Instead of a recursive approach, you could do something like this:
1
2
3
4
5
6
do {
    cout << "Please input value:  \n" << endl;
    cin >> flag;

    // ...
} while( flag != sentinel );


cin.get();
Purpose of this?
Topic archived. No new replies allowed.