largest and smallest #

My assignment is :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
Please enter your choice ___

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.

Having some problems with this, My code is:

#include <iostream>

using namespace std;


const int B_SENTINEL = -99; //sentinal used to end choice B

int main()
{
// variables
bool again = true; //used to control main choice loop
char choice; //stores the choice made
int large ; //stores # user inputs
int largest = 0; //stores current biggest #
int temp = 0; //stores next number for comparison
int i; //controls the For loop
int num = 0; //stores #'s user inputs
int small; //stores # user inputs
int smallest; //stores current smallest #
int next = 0; //stores next number for comparison

while (again)
{
//displays instructions for main options
cout << "What would you like to do?" << endl;
cout << "Please Enter the a letter of your choice and click Enter" << endl;
cout << "A - Find the largest # within a known quantity of #s" << endl;
cout << "B - Find the smallest # within an unknown quantity of #'s"<< endl;
cout << "C - Quit this program." << endl;
cout << "Please enter your choice" " ";

//obtains choice user inputs
cin >> choice;
cout <<endl;

if (choice == 'A' || choice == 'a')
{

// asks user to enter how many numbers they would like to enter
cout <<"Please enter how many #'s you would like to enter:" <<" ";
cin >> num;
cout <<endl;
cout <<endl;

// lets the user know how many numbers user chose to enter
cout <<"The # you entered was:" <<" ";
cout << num << " " <<endl;

// tells user how many number to enter
cout << "Please enter " << num <<" #'s an click Enter." <<endl;
cout << "Please use spacebar to input spaces between each number." <<endl;
cout <<"Remember to put all " << num << " #'s in or program will not finish till they are." <<endl;
cout << "If you enter more then " << num <<" #'s the program will only read the first " <<num <<" #'s."<<endl; cin >> large;


largest= large;
}

for (i = 0; i < num -1; i++)
{
cin >> temp;

}
if (largest < temp)
{
largest = temp;

//output of largest number entered
cout << "The largest number entered was." <<" ";
cout << largest << endl;
cout <<endl;
}

if (choice == 'B' || choice == 'b')
{
// ask user to input #'s and tells them to input -99 to exit to main menu
cout << "Enter #'s then click Enter:" <<endl;
cout <<"To exit to main menu please input: " << B_SENTINEL <<" and click Enter." << endl;
cout <<"Please space #'s using spacebar" <<endl;
cin >> small;
smallest = small;
}
//start of sentinel controlled while loop
while (next != B_SENTINEL)
{ cin >> next;

}
if (smallest > next && next != B_SENTINEL)
{
smallest = next;

//output of smallest number entered
cout << "The smallest number entered was: " <<endl;
cout << smallest << endl;
}

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

}

return 0;
}


my first problem in choice A is that after I find the largest # I do not go back to main menu options.

choice B is when I run the program and I choose B and input 102 100 99 65 87
and click enter it does nothing.

choice C: when I choose this option it does nothing .


Any help that any one can give my would be very appreciated. have been up all night trying to figure this one out and my be that is my problem I need to walk away from this.

You have control flow problems. It's a case of losing the forest for the trees because a single function -- main -- does everything. Let me remove some of the details from your code and maybe then you'll see the problems:

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
int main() {
   quit = false;
   while( quit == false ) {
       // Display menu
       // Ask user for choice A, B, C

      if( choice is 'A' or 'a' ) {
          // Ask user for number of numbers
          // Tell user what they just enetered
          // Give user instructions

          // Ask user to enter 1 number:
          cin >> large;
       
          // Largest is this number
         largest = large;
      } // End if statement

      // This is outside the above if() statement, so it happens in every case, right?
      // Read in num - 1 numbers
      for( i = 0; i < num - 1; ++i )
      {
           // Read in a number, overwriting previous number read in
           cin >> temp;
      }

      // temp now holds the last number entered
      if( temp /* last number entered */ > largest )
           largest = temp;

     // ...
   }
}


I encourage you to translate the rest. Do you see the problems so far?
Oh, and by the way:

We WILL NOT do your homework. I realize you tried and only asked for help. I'm glad you made an effort. Just don't try to come here with no source code, begging for an answer we will NEVER give you.

Lesson to beginners taking classes:

DON'T ASK US TO DO YOUR HOMEWORK.

</rant>
Homework would be fun if you could do it your own way...like:
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
#include <iostream>
#include <set>

using namespace std;

int main( int argc, char* args[] ) {

    unsigned int count;
    cout << "How many numbers: ";
    cin >> count;

    set<int> numbers;

    unsigned int number;
    for( ; count > 0; --count )
    {
        cin >> number;
        numbers.insert( number );
    }

    cout << "The smallest number is: " << *numbers.begin() << endl;
    cout << "The biggest number is: " << *numbers.rbegin() << endl;

    return 0;
}
Really sorry QWERTYman that you thought I wanted you to do my homework for me I just wanted someone to point me in the right direction to finding what I had done wrong or if I was totaly off here I would never ask someone to do my home work for me that would not teach me a thing. Just need a little help in right the direction every now any then. I really do appreciate you all responding to me and I do see the problem I did.
I'm not sure what he was ranting about..usually that stuff is reserved for the people that just list their assignment and say "please help",without showing any of their code at all.
you're fine
I was P.Oed at someone.

Just blowing off steam. I realize you weren't doing that.

BUT LET THAT BE A LESSON TO J00 N00BS WHO EXPECT THE WORLD TO BE HANDED TO THEM.

And fear this Ackbar, n00bs (courtesy of helios):
............................................________
....................................,.-‘”...................``~.,
.............................,.-”...................................“-.,
.........................,/...............................................”:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:”........./
..............?.....__.........................................:`.........../
............./__.(.....“~-,_..............................,:`........../
.........../(_....”~,_........“~,_....................,:`........_/
..........{.._$;_......”=,_.......“-,_.......,.-~-,},.~”;/....}
...........((.....*~_.......”=-._......“;,,./`..../”............../
...,,,___.\`~,......“~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-”
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\
It's cool did not take it personal one question though how did you guys get your code in that blue back ground that showes the number of lines never mind found it and another thing I am a noob for now but just watch it I wont be for long
#include <iostream>

using namespace std;


const int B_SENTINEL = -99; //sentinal used to end choice B

int main()
{
// variables
bool again = true; //used to control main choice loop
char choice; //stores the choice made
int large ; //stores # user inputs
int largest = 0; //stores current biggest #
int temp = 0; //stores next number for comparison
int i; //controls the For loop
int num = 0; //stores #'s user inputs
int small; //stores # user inputs
int smallest; //stores current smallest #
int next = 0; //stores next number for comparison

while (again == true)
{
//displays instructions for main options
cout << "What would you like to do?" << endl;
cout << "Please Enter the letter of your choice and click Enter" << endl;
cout << "A - Find the largest # within a known quantity of #s" << endl;
cout << "B - Find the smallest # within an unknown quantity of #'s"<< endl;
cout << "C - Quit this program." << endl;
cout << "Please enter your choice" " ";

//obtains choice user inputs
cin >> choice;
cout <<endl;

if (choice == 'A' || choice == 'a')
{

// asks user to enter how many numbers they would like to enter
cout <<"Please enter how many #'s you would like to enter:" <<" ";
cin >> num;
cout <<endl;
cout <<endl;

// lets the user know how many numbers user chose to enter
cout <<"The # you entered was:" <<" ";
cout << num << " " <<endl;

// tells user how many number to enter
cout << "Please enter " <<num << " #'s an click Enter." <<endl;
cout << "Please use spacebar to input spaces between each number." <<endl;
cin >> large;
largest= large;

for ( i = 0; i < num - 1; i++)
{
// Read in a number, overwriting previous number read in
cin >> temp;

}
if (temp > largest)
{
largest = temp;

//output of largest number entered
cout << "The largest number entered was: ";
cout << largest << endl;
cout <<endl;

cout <<"Please enter next # to see if it is larger than " << largest <<" " <<endl;
cin >>temp;
}
if (largest < temp)
{
// outputs this if this # is largest
cout << "The largest # now is: " << temp <<endl;
cout <<endl;
}
else if (largest > temp)
{
// outputs if this is # is larger then temp
cout << "The largest # now is: ";
cout << largest;
cout <<endl;
}
}
else if (choice == 'B' || choice == 'b')
{
// ask user to input #'s and tells them to input -99 to exit to main menu
cout << "Enter #'s to find the smallest # you enter then click Enter:" <<endl;
cout <<"To exit to main menu please input: " << B_SENTINEL <<" and click Enter." << endl;
cout <<"Please space #'s using spacebar" <<endl;
cin >> small;
smallest = small;

//start of sentinel controlled while loop
while (next != B_SENTINEL)
{ cin >> next;

if (smallest > next)
{
smallest = next;
//output of smallest number entered
cout << "The smallest number entered was: " <<endl;
cout << smallest << endl;
}
}
}
// closes the program
else
return 0;
}
}
what I am getting now is for a it works fine if I put the largest number in last but lets say i inter for choice Please enter how many #'s you would like to enter: I put 6 here then ask me to enter my 6 numbers and I do 23 56 12 36 69 1
and says my largest number is 23 what am I missing here
Please place the code tags around your code so that we can see it more clearly.

Now, what is really happening is that you are using:

1
2
int large;
cin>>large;


Which will not accept a "space-delimited list of numbers". It will read the first number that you enter. For example, entering "23 56 12 36 69 1" and pressing enter causes the cin call to read 23 and stop as soon as it hits the space character.

It appears that you want to enter numbers 1 at a time, and press enter after each one. That is how the code looks to expect and that will also be easier to do.

The next problem is that the tests for the size of the number entered:

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
if (temp > largest)
{
largest = temp;

//output of largest number entered
cout << "The largest number entered was: ";
cout << largest << endl;
cout <<endl;

cout <<"Please enter next # to see if it is larger than " << largest <<" " <<endl;
cin >>temp;
}
if (largest < temp)
{
// outputs this if this # is largest
cout << "The largest # now is: " << temp <<endl;
cout <<endl;
}
else if (largest > temp)
{
// outputs if this is # is larger then temp
cout << "The largest # now is: ";
cout << largest;
cout <<endl;
} 


Should be performed for each number entered, right? So they should be inside the for loop. Which also means that you have an extra cin call that will need to be removed.

Additionally, please clean up your comments and name your variables appropriately. It will only help to name them such that you understand what they will be used for, rather than having comments all over the place! Also, I assume that you are already using indentation in your code but it was removed by posting without code tags.

So, in order to complete this on your own you will have to read it from top to bottom very carefully. You must understand what each line is really for, or you will have a very difficult time.
i also suggest using an case statement for chaises A,B,C
Thank you all for helping me out got 44 out of 45 on this assignment . really appreciate you fast responses to my Questions. and for the one point I missed all it was I did not int largest to 9999999999 and smallest to -99999999

again thanks for all the help and hopefully when I get some experience under my belt I will be able to do the same for all of you.
Topic archived. No new replies allowed.