Code problem

Pages: 12
Can't get it to go past the selection process. if I make a choice it does not even display the corresponding code for that selection.
Because of your infinite loop here:
1
2
3
while(choice == 'a')
    while (counter <= limit)
        ;


Anyways, what I was trying to get at was something like:

1
2
3
4
5
6
7
8
9
10
11
12
if (choice == 'a')
{
    // Do stuff
}
else if (choice == 'b')
{
    // Do other stuff
}
else if (choice == 'c')
{
    // You get the point
}


You could technically use a switch statement for something like that, but I don't know if you know how to use those yet....
i kind of do just could get that to work either
still won't let me past the menu for any selection.
ok got the other selection to somewhat work but the limit variable will not change to the new input or does any of the other variables.
Okay, let me give you a hint.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (choice == 'a')
{
    // Ask user how many numbers
    while (counter < limit) // I would use a for loop, but this works too
    {
        // Get the numbers
        ++counter;
    }
    // Print highest number
}
else if (choice == 'b')
{
    // And so on and so forth
}
program still stops cold after trying to set the limit variable wont even ask for the numbers to try an get to the limit. I am lost here and the b option will only allow a single number to be entered and returns a not entered number for the output on it. The only option that works correctly is c the exit option. I need this to compare a series of numbers also and it doesn't do that. It also will not change the variables to the input variables.
Last edited on
What do you have so far?

EDIT: Let me guess...
1
2
3
4
while (counter <= limit); // Should not be a semicolon here
{
    // ...
}
Last edited on
Took that completely out of the current program. so I could advance and see what it would do, even though it is supposed to be how the supposed loop is to be ended and return the user to the menu.
Current 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
#include <iostream>
#include <cctype>
using namespace std;

int main()
//set variables
{	
	int highnum(0);
	int lownum(100);
	int limit(0);
	char choice; 
	int counter(0);
	int num;
	
	
		cout<<   "A - Find the largest number with a known quantity of numbers. ";
		cout<<   endl;
		cout<<	 "B - Find the smallest number with an unknown quantity of numbers. ";
		cout<<   endl;
		cout<<	 "C - Quit";
		cout<<	 endl;
		cout<<	"Enter your choice. ";
		cin>>  choice;
		cout<<	endl;
	

		
	if(choice=='a')
	{
		 cout << " How many numbers would you like to compare? ";
		 cin >> limit;
		 
		 ++ counter;
		 cout << endl;
		 cout<< "Enter your number ";
		 cin >> num;
		 
	
		
	if (num > highnum)
		cout << "your highest number was ";
		 cout << highnum << endl; 
	}
	if  (choice == 'b')
	{
		cout << "Enter your numbers. ";
		cin >> num;
		cout<< "Your lowest number was ";
		cout << lownum << endl;
	}
		
	if (choice == 'c')
		cout << "thank you for playing. " << endl;
		
	cin.get();
	cin.get();

	return (0);
}
Let me show you where the loop goes for choice A.
Afterwards, see if you can figure out how to do choice B.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (choice=='a')
{
    cout << " How many numbers would you like to compare? ";
    cin >> limit;
        
    //while (counter < limit) { // Do not put a semicolon after the while!
    ++ counter;
    cout << endl;
    cout<< "Enter your number ";
    cin >> num;
    // If num is bigger than current highest number (highnum)
    // then set the highest number to num
    //}

    if (num > highnum) // Get rid of this line
    cout << "your highest number was ";
    cout << highnum << endl;
}
ok that works. now to actually get it to loop the num entry process and figure out how to stop the b option when I get it to actual y loop.
Topic archived. No new replies allowed.
Pages: 12