Loop issue

Pages: 12
Having trouble getting these menu options to loop properly.
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
#include <iostream>

using namespace std;

const int SENTINEL = -999;

int main()
//set variables
{	
	int highnum;
	int lownum;
	int limit;
	char choice; 
	int counter;
	int num;
	//iniatilize v ariables
	highnum = -999;
	lownum = 999;

	
		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;


	switch (choice)
	{

		case 'a':
		case 'A':
			cout << " How many numbers would you like to compare? ";
				cin >> limit;
				cout<< "Enter your number "; 		
				 cin >> num;
			do
				while (counter = 1);
				while (counter <= limit);
				while(counter ++)
		
				
				  
				if (num > highnum)
					int highnum(num);
				
				cout << "your highest number was "; 
				cout << highnum;
					 cout<< endl;

		}

	switch(choice)
	

		case 'b':
		case 'B':
			{
			num = 0;
		
			num > SENTINEL;
			cout << "Enter your numbers ending with ";
			cout<< SENTINEL ;
			cout << endl;
			cin >> num;
			if (num > lownum) 
				int lownum (num);
			
			
			cout<< "Your lowest number was ";
			cout << lownum; 
			cout<< endl;
		
	}
	
	switch (choice)

		case 'c':
		case 'C':
		cout << "thank you for playing. " << endl;
		
	cin.get();
	cin.get();

	return (0);
}
1
2
3
while (counter = 1);
				while (counter <= limit);
				while(counter ++)
what is this for? they are 3 separate loops that will run forever.
second
1
2
if (num > highnum)
					int highnum(num);
what in the world could that possibly do?. redeclaring int highnum as a function within main that requires and int? fix this please. int lownum (num);fix this also
last error i got right of the bat was
1
2
3
num = 0;
		
			num > SENTINEL;
what are you doing here? that is a Boolean operator that returns true or false depending on size. this will do exactly nothing.

now lets look at the program as a while.
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
	switch (choice)
	{

		case 'a':
		case 'A':
			cout << " How many numbers would you like to compare? ";
				cin >> limit;
				cout<< "Enter your number "; 		
				 cin >> num;
			do
				while (counter = 1);
				while (counter <= limit);
				while(counter ++)
		
				
				  
				if (num > highnum)
					int highnum(num);
				
				cout << "your highest number was "; 
				cout << highnum;
					 cout<< endl;

		}

	switch(choice)
	

		case 'b':
		case 'B':
			{
			num = 0;
		
			num > SENTINEL;
			cout << "Enter your numbers ending with ";
			cout<< SENTINEL ;
			cout << endl;
			cin >> num;
			if (num > lownum) 
				int lownum (num);
			
			
			cout<< "Your lowest number was ";
			cout << lownum; 
			cout<< endl;
		
	}
	
	switch (choice)

		case 'c':
		case 'C':
		cout << "thank you for playing. " << endl;
why do all these switch statements? and no break statements anywhere. the normal setup for a switch statement is
1
2
3
4
5
6
7
8
  switch (<#expression#>) {
        case <#constant#>:
            <#statements#>
            break;
            
        default:
            break;
    }
For the first it give me a excepted a while for the various parts if I try and make them one line. The point of the program in to return either the highest or lowest number in a user entered list. with the first A choice having a user defined limit and the b choice being SENTINEL controlled.
2 of those show code errors for my IDE and all 3 are said to(and tested to) run forever. what is should look like is
1
2
counter = 1; 
while(counter <= limit){counter ++;}
although this will work there is no difference here from just saying counter = limit;
Last edited on
That still will not loop and only runs once then exits for my a choice and still leaves choice b not working. I do appreciate the help. I am using visual studio for a compiler and have no idea what i am doing in C++
only runs once? it will run as many times are needed, there is just no output for a direct observation that it has looped.... do
1
2
counter = 1;
while(counter <= limit){std::cout << counter++;}
and you will see each time the loop is run.
Last edited on
it displayed a count to 5 then output the initialized variable as the highest number.
which is exactly what the loop is suppose to do.
the user inputs the amount of numbers to be compared. then the program tells the user which number was the highest as out put.
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
#include <iostream>
#include <vector> 
#include <algorithm>

int main(){
    std::vector<int> mynumbers; 
    int temp, number;
    bool valid = false; 
    
    
    while(!valid){
    std::cout << "Enter the number of numbers you want to be input: ";
    std::cin >> temp;
       if(temp <= 0) std::cout << "invalid number entered\n";
       else valid = true; 
    }
    

    for(int i = 1; temp != 0; temp--){
        std::cout << "Enter number " << i++ << ":";
        std::cin >> number;
        mynumbers.push_back(number);
    }
    sort(mynumbers.begin(), mynumbers.end()); 
    
    std::cout << "your highest number is: " << mynumbers.back();
    std::cout << '\n' << "your lowest number is: " << mynumbers.front(); 
    
    return 0;
}
is this similar to what you are trying to do?
Last edited on
yes, i'm just not familiar with that syntax you are using.
so here my thought proccess
int main(){
//variables

//get number of numbers, make a loop so no errors occur

//sort numbers, i used 1 in the algorithm library

//output the highest number, .back() if using vectors
//output lowest number, .end() if using vectors

return 0;
}
vectors = http://www.cplusplus.com/reference/stl/vector/
algorithm = http://www.cplusplus.com/reference/algorithm/
Last edited on
thanks ill give that a try.
just post any questions you have.
Ok so I am trying to go at this slightly different than above. This is just the first step to the program still trying to figure out the second step. Having a slight issue with the function headers saying the need a declaration.
the commented variables are just there for the second menu option I have yet to 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
using namespace std;
#include <iostream>
#include <cctype>

using namespace std;

int main;
	//int low (low = 999);
	//const int SENTINEL = -9999;


	int menu;
	{
		int choice();
		for (menu);
			cout<<"Select a chioce from the menu below.";
			cout<<" A- Find the highest number in a series. ";
			cout<<" B- find the lowest number in a series. ";
			cout<<"C- Exit ";
				cout<< "Enter your selection ";
		cin>> choice;
		
			if choice = a
				do OptA
	}

	int OptA;
	{
		int number()
		int limit;
		int high (high = -999);
		int counter = 0;
		cout<<"how many number would you like to compare? ";
		cin>>limit;
		cout<<"Enter your numbers. ";
		cout<< endl;
		cin>>number;
			if (number > high);
				int high = number;
			int counter = counter + 1;
			while counter < limit;
			else cout<< "The highest number is " << high);
			cout<< endl);
				return(menu);
	}
declare functions as such.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int menu();
int OptA();

int main(){
//stuff here
return 0;
}

int menu(){
//stuff here
return int;
}

int OptA(){
//stuff here
return int;
}
Did not fix the header problem.
post your current code please.
Haven't changed much.
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>
#include <cctype>

using namespace std;


int main();

	// PLACE HOLDERS FOR TESTING ONE SECTION AT A TIME.
	//int low (low = 999);
	//const int SENTINEL = -9999;

	int menu();
	//Trying to get a repeatable menu option
{
		int choice();
		for (menu);
			cout<<"Select a chioce from the menu below.";
			cout<<" A- Find the highest number in a series. ";
			cout<<" B- find the lowest number in a series. ";
			cout<<"C- Exit ";
				cout<< "Enter your selection ";
		cin>> choice;
		
			if (choice = a)
				do OptA
			if (choice = b)
				do OptB
			if (choice = c)
				do OptC
	return int;
}
//this option should output only the highest number for a usre defined limit to end the loop. Then return to menu.
	int OptA();
			{
		int number()
		int limit;
		int high (high = -999);
		int counter = 0;
		cout<<"how many number would you like to compare? ";
		cin>>limit;
		cout<<"Enter your numbers. ";
		cout<< endl;
		cin>>number;
			if (number > high);
				int high = number;
			int counter = counter + 1;
			while (counter < limit);
			else cout<< "The highest number is " << high);
			cout<< endl);
				return int;
			}
// OptB should output the lowest number entered with a sentinel value ending the loop. Then return to menu
// OptC exits the program. 
read what i posted about how to declare functions and loop up while and for loops please.
Pages: 12