So here is what I am trying to accomplish. I have been trying and trying for a few hours to figure out how to do this.

Write a program that displays a menu with the following choices to the user.
A - Find the largest # with a known quantity of numbers
B - Find the smallest # with an unknown quantity of numbers
C - Quit

This menu needs to be repeatedly displayed until the user chooses to quit.

If A is chosen, you should ask the user how many numbers he wants to enter. If he enters 5, you should read 5 numbers from him. You should then display the largest number he entered. Use a for loop to read these and display the largest.

If B is chosen, you should keep reading numbers no matter what until the user enters -99. Then you should display the smallest number he entered not counting the -99.

#include <iostream>

using namespace std;

const int A_QUANTITY = 10;    // Amount of #'s entered for option A
const int B_SENTINEL = -99;   // Sentinal used to end option B

int main ()
{
bool again = true;		  // Used to control main source loop
char choice;              // Stores the choice made
int biggest;			  // Stores largest #
int smallest;			  // Stores smallest #
int temp;                 // Stores next number for comparison
int i;					  // Controls the For loop

while (again)
{
// Displays instructions to user
cout << "A - Find the largest # with a known quantity of numbers" << endl;
cout << "B - Find the smallest # with an unknown quantity of numbers" << endl;
cout << "C - Exit" << endl;
cout << "Please enter your choice" << endl;

// Gets input from user
cin >> choice;

if (choice == B)	  // Beginning of if statement
{
i = 0;
do
{
cout << "Enter number (-99 to end): ";
cin >> i >> endl;
i++;
cout << "You entered: " << i << "\i";
}while (i != 0);
}

else if (choice == A)  // Beginning of else if statement
{
for( i = 0; i < 10; i++)
cout << "i = " << i << endl;

for(i = 10; i>=0; i--);
}

{return 0;
}

## All 9 Replies

line 36: delete the endl from that line because it doesn't go with cin. But because i is an int you will probably have to flush the '\n' from the keybord buffer, so add cin.ignore() after line 36 and before line 37.

line 37: delete this because there is no need to increment the value of i.

line 39: should be while( i != -99) because that's what your instructions say.

What you are missing in the loop is a counter that keeps track of the minimum value entered.

>>If A is chosen, you should ask the user how many numbers he wants to enter
You have not done that yet. And you need to use another int variable to keep track of the largest number entered.

Do you have a question?

So im not exactly sure of how to do a counter see I left my book at home and I flew to florida without so i can't look up examples.

I made some changes to your smallest integer do while loop. I'll leave the rest for you to try to figure out.

#include <iostream>

using namespace std;

const int A_QUANTITY = 10;		// Amount of #'s entered for option A
const int B_SENTINEL = -99;		// Sentinal used to end option B

int main ()
{
bool again = true;			// Used to control main source loop
char choice;				// Stores the choice made
int biggest = 0;			// Stores largest #
int smallest = 0;			// Stores smallest #
int temp;					// Stores next number for comparison
int i;						// Controls the For loop

while (again)
{
// Displays instructions to user
cout << "A - Find the largest # with a known quantity of numbers" << endl;
cout << "B - Find the smallest # with an unknown quantity of numbers" << endl;
cout << "C - Exit" << endl;
cout << "Please enter your choice" << endl;

// Gets input from user
cin >> choice;

if (choice == 'B' || 'b')	  // Beginning of if statement
{
i = 0;
do
{
cout << "Enter number: (-99 to exit)" ;
//cin >> i;
i++;

cin >> temp;	//this sets temp to what the user inputs
//cout << "You entered: " << temp << endl; //<< "\i"   <---What was this for?

if(i == 1)
smallest = temp;

if(smallest > temp && temp !=-99)
smallest = temp;

}while (temp != B_SENTINEL);

cout << endl << smallest << endl;
}

else if (choice == 'A' || 'a')  // Beginning of else if statement
{
for( i = 0; i < 10; i++)
cout << "i = " << i << endl;

}

else if (choice == 'C' || 'c')
again = false;
}

return 0;
}

I get the program to run but no matter what choice i make it comes up with -99 to exit...

#include <iostream>

using namespace std;

const int A_QUANTITY = 10;
const int B_SENTINEL = -99;

int main ()
{
bool again = true;
char choice;
int biggest = 0;
int smallest = 0;
int temp;
int i;

while (again)
{
// Displays instructions to user
cout << "A - Find the largest # with a known quantity of numbers" << endl;
cout << "B - Find the smallest # with an unknown quantity of numbers" << endl;
cout << "C - Exit" << endl;
cout << "Please enter your choice" << endl;

// Gets input from user
cin >> choice;

if (choice == 'B' || 'b')
{
i = 0;
do
{
cout << "Enter number: (-99 to exit)" ;
//cin >> i;
i++;

cin >> temp;
//cout << "You entered: " << temp << endl; //<< "\n"

if(i == 1)
smallest = temp;

if(smallest > temp && temp !=-99)
smallest = temp;

}while (temp != B_SENTINEL);

cout << endl << smallest << endl;
}

else if (choice == 'A' || 'a')
{
for( i = 0; i < 10; i++)
cout << "i = " << i << endl;

}

else if (choice == 'C' || 'c')
again = false;
}

return 0;
}

>>I get the program to run
No you didn't because there are syntax errors on lines 30, 53 and 60. So if you ran a program it was not the code you posted. Line 30 should have been coded like this: if (choice == 'B' || choice == 'b') and the others similar.

>>I get the program to run
No you didn't because there are syntax errors on lines 30, 53 and 60. So if you ran a program it was not the code you posted. Line 30 should have been coded like this: if (choice == 'B' || choice == 'b') and the others similar.

This isn't a syntax error, the code compiles fine. It also runs, but does not have the desired operation since a statement like 'b' always returns true, hence the error. As you said, it should be if (choice == 'B' || choice == 'b') to get the desired behaviour, but it's not a syntax error.

On slightly different point, in the program you go to the trouble of defining B_SENTINEL on line 6. However, on line 45 you test temp == -99 . You should use your B_SENTINEL variable here as it helps keep your code in a maintainable state; that is, you will only have to change the exit value at the top and it will be changed everywhere.

commented: You are right -- it isn't a syntax error +21

I'll do an example up and get back to you.

Sorry it took so long to send this example, i was quite busy. I hope this example helps. Note This program doesn't have ANY error checking regarding converting characters to integers, so don't enter any funny stuff at the prompt that asks you for the numbers ;).

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
#include<vector>
using std::vector;

/*
Function Description: Looks through a int vector and finds the largest or smallest int
large_small = true if looking for largest int else false to get smallest int
*/
int compare(bool large_small,vector<int> numbers)
{

int biggestNum = numbers[0];
if(large_small)// finding largest number
{
for(int i=0; i<numbers.size();++i)
{
if(biggestNum < numbers[i])
biggestNum = numbers[i];
}
}
else// used to find smallest number
{
for(int i=0; i<numbers.size();++i)
{
if(biggestNum > numbers[i])
biggestNum = numbers[i];
}
}
return biggestNum;
}

/*
Function Description: gets largest number
*/
int getNumbers(bool large_small)
{
int size;
int tmp;
vector<int> numbers;

cout <<"Enter how many numbers to read.";
cin >>size;

//reads all numbers in
for(int i=0; i < size; ++i)
{
cout <<(i+1)<<": ";// prompt with numbers entered
cin >>tmp;// reads in number
numbers.push_back( tmp ); // added number
}
return compare(large_small,numbers);
}

int main()
{
//attributes
string str;
int largestNum=0;
int smallestNum=0;

cout <<"Welcome to This appliction :)\n"<<endl;//Welcome message

while(true)//never ending loop
{
//Options presented to
cout <<"Please make a selection...."<<endl;
cout <<"A - Find the largest number"<<endl;
cout <<"B - Find the smallest number"<<endl;
cout <<"C - Quit"<<endl;
cin >>str;// reading in input

if((str != "a") && (str != "A") && (str != "b") && (str != "B") && (str != "c") && (str != "C"))
{
cout <<"Invalid option..."<<endl;
}

if((str == "a") || (str == "A"))
{
cout <<"Largest size: "<< getNumbers(true) <<endl;//returns largest number
}

if((str == "b") || (str == "B"))
{
cout <<"Smallest size: "<< getNumbers(false) <<endl;
}

if((str == "c") || (str == "C"))
exit(0);//exiting
}
return 0; //returning 0 to operating system saying all is good.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.