Saving the greatest/ least value...

This is a discussion on Saving the greatest/ least value... within the C++ Programming forums, part of the General Programming Boards category; Okay, I have to create a menu ( which I did) and have 3 options. (done) In option A I ...

  1. #1
    Registered User mcweezle's Avatar
    Join Date
    Nov 2006
    Posts
    9

    Question Saving the greatest/ least value...

    Okay,

    I have to create a menu ( which I did) and have 3 options. (done) In option A I have to allow the user to enter how many numbers he wants to input (a limit). Then after that limit of numbers is input, the user is told which number was the largest. This is the part that I'm having trouble with. All it prints for the value is what the last number that was entered was.

    I'm getting the same error in section B. All it wants to display for the lowest number entered is -99... I think I have an idea for that one, but section A still gives me problems.

    I have to use loops.. do..while, for, while, if / else. Any pointers as to what I'm missing here?



    Code:
    // Week 5 - Control Structures II- Philip McCrary - 11-08-06
    
    #include<iostream>
    						
    using namespace std; 
    
    char menuItem;
    int number;
    int limit, counter;
    int temp1, temp2;
    const int SENTINEL = -99;
    
    int main() 
    { //open main
    
    MainMenu: while (menuItem != 'C' || 'c')
    	{ // open while 1
    	cout << "Welcome to CS106 Week 5: Control Structures II." << endl;
    	cout << "Please choose from the following options below." << endl;
    	cout << "A: What's the largest number?" << endl;		
    	cout << "B: What's the smallest number?" << endl;
    	cout << "C: Flee to Windows" << endl;
    	cout << "Please choose now: ";
    	cin >> menuItem;
    	cout << endl;
    	
    															
    		if ((menuItem >= 'A' || 'a') && (menuItem <= 'C' || 'c'))
    		{ // open if main 
    			switch (menuItem)
    			{ // open switch 1
    			case 'a':													
    			case 'A':
    
    			cout << "This section will find the largest number? " << endl;
    			cout << "How many numbers do you want to enter? ";
    			cin >> limit;
    			cout << endl;
    			cout << "Enter " << limit << " numbers: " << endl;
    			
    			counter = 0;
    						
    			for (counter = 0; counter < limit; counter++)
    				cin >> number;
    
    			
    			temp1 = 0;
    			if (number > temp1)
    				temp1 = number;
    			if (temp1 != 0)
    				cout << "\nYour largest number out of " << limit << " was " << temp1 << "." << endl;
    			cout << endl;
    			goto MainMenu;
    
    			break;													
    
    			case 'b':
    			case 'B':
    				
    				cout << " Enter a group of numbers. I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
    			cin >> number;
    
    			counter = 0;
    			temp2 = 0;
    				
    					while (number != SENTINEL)
    					{ // open while 3
    						counter++;
    						cin >> number;
    					}
    					if (number < temp2)
    						temp2 = number;
    
    					
    					
    						cout << "Your smallest number out of " << counter << " was " << temp2 << "." << endl;
    				cout << endl;
    				goto MainMenu;  // added as a reroute to the main menu
    					break;
    			case 'c':
    			case 'C':
    				cout << " Goodbye. " << endl;
    				return 0;
    			} //close switch 1
    		} // close if main
    		else
    		{
    			cout << "Please enter a valid menu option; A, B, or C." << endl;
    			cin >> menuItem;
    			cout << endl;
    		} // end else 
    
    		  } // close while 1
    	return 0;
    } // close main

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1. Do not use goto, simply break out of your while loop.
    2. You do not need to set counter twice:
    Code:
    counter = 0;
    for (counter = 0; counter < limit; counter++)
    Use this:
    Code:
    for(counter = 0; counter < limit; counter++)
    3. Number is overwriten every time you cin. Thus the last number entered is the greatest. Create an array of size limit and use that.
    Code:
    cin >> limit;
    cout << endl;
    cout << "Enter " << limit << " numbers: " << endl;
    int numbers[limit];
    for(counter = 0; counter < limit; counter++)
         cin >> numbers[counter];
    EDIT: This:
    Code:
    if (number < temp2)
         temp2 = number;
    needs to be in the while loop to be counted. Also, this will keep looping until you enter -99 (SENTINEL), as it says
    Code:
    while(number != SENTINEL)
    Unless you enter -99, this will keep looping. You neeed a way to know how to exit.
    Last edited by manutd; 11-12-2006 at 06:25 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User mcweezle's Avatar
    Join Date
    Nov 2006
    Posts
    9
    See that's my problem. We haven't covered arrays yet, so to include it would be just wrong.

    Is there any way of doing this in a while, for, or do.. while loop.... or even an if / else statement?



    Thanks for any help guys!

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I don't think so. You would overwrite the previous value Try the non-array fixes I suggested and see if that helps. If not, ask your teacher if you may use arrays. Hey, they might even be impressed that you are ahead (assuming you use them correctly).
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User mcweezle's Avatar
    Join Date
    Nov 2006
    Posts
    9
    There in lies the apparent problem though... I don't know what an array is.

    Shouldn't my if statement save only the highest value?

    Code:
    if (number > temp1)
        temp1 = number;

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I'll take your second question first: No, as right before that you set temp1 = to 0. Also, this does not loop. You need code like:
    Code:
    for (counter = 0; counter < limit; counter++)
    {
         cin >> number;
         if (number > temp1)
              temp1 = number;
    }
    This should work. As for your first question, check out the tutorials.
    Last edited by manutd; 11-12-2006 at 06:44 PM.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User mcweezle's Avatar
    Join Date
    Nov 2006
    Posts
    9
    DUDE!!!

    I am so dancing a jig in my living room right now! the brackets in the for statement worked!!!
    You are so awesome. I can't believe I didn't think of that!
    *Super-long-distance HIGH FIVE*

    Now I just have to figure out why section B returns only a 0 answer.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Almost the same thing:
    Code:
    cout << " Enter a group of numbers. I will tell you the smallest number. \n Enter " << SENTINEL << " to exit." << endl;
    counter = 0;
    temp2 = 0;
    				
    while (number != SENTINEL)
    { // open while 3
         counter++;
         cin >> number;
         if (number < temp2)
              temp2 = number;
    }
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User mcweezle's Avatar
    Join Date
    Nov 2006
    Posts
    9
    Okay. Here is the updated code.

    Section B returned -99 as the smallest value, so I added the != Sentinel in the if statement and now it returns 0 again.




    You've been a BIG help man. More so than the appointed programming tutor at the college... he's too hard to get a hold of!
    Thanks.. really I mean it.
    Last edited by mcweezle; 11-12-2006 at 07:33 PM.

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Sure thing Good luck! One last question: does B work? Will it show your smallest number?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    Registered User mcweezle's Avatar
    Join Date
    Nov 2006
    Posts
    9
    No. With the snippet of code you showed me, it returns -99 as the smallest number. I inserted a && != SENTINEL in the if staement and now it only returns a ) value.

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    hmm...why not try the for method:
    Code:
    int limit2 = 0;
    case 'B':
    int limit2 = 0;
    cout << "This section will show the smallest number."
    cout << "How many numbers do you want to enter? ";
    			cin >> limit2;
    cout << endl << "Enter " << limit2 << " numbers: " << endl;
    
    for (counter = 0; counter < limit2; counter++)
    {
         cin >> number;
         if (number < temp2)
              temp2 = number;
    }
    I know this does not probably do exactly what you want, but does it work?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  13. #13
    Registered User mcweezle's Avatar
    Join Date
    Nov 2006
    Posts
    9
    Section B requires a sentinel to flag the end of user input. The user has to be able to enter any amount of number and end with -99. Then it should display the proper results.

  14. #14
    Registered User mcweezle's Avatar
    Join Date
    Nov 2006
    Posts
    9
    I see part of the problem.. I have temp2 = 0, but then i ask it to replace the value of temp 2 with number if number is less than 0. I'm trying to rewrite that part now.

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I can't figure it out If you wait until tomorrow the forum gods (Salem, quzah, Prelude, etc) will answer your question. Sorry I can't be of more help!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop to get greatest and least number, its not working please help
    By stressedstudent in forum C++ Programming
    Replies: 27
    Last Post: 09-27-2007, 04:45 AM
  2. problems with greatest to least sorting
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2004, 10:12 PM
  3. File saving problem in DevC++ pls help ???
    By intruder in forum C Programming
    Replies: 3
    Last Post: 12-17-2002, 01:17 AM
  4. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 04:41 AM
  5. Greatest Avatar :)
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 09-17-2001, 07:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21