CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2010
    Location
    Ohio
    Posts
    27

    Post No output for "smallest number"? Why?

    I just realized I posted this same post in Visual C++ when I should've posted it here in Non-visual C++ since this is a console program. Forgive my double posting in two different forums. It will not happen again.

    I have been working on this particular case (case B, in my example). I'm in a beginning program class and have been ok at it but this one is stumping me in why it wouldn't display the smallest number. It loops back to the menu option.

    Can someone point me in the right direction as to why it's not giving me output? Thank you for any advice.

    Code:
    #include <iostream>
    
    using namespace std;
    
    char menuChoice;	
    	int count = 0;
    	int number;
    	int groupNbrs;
    	int temp_A = 0;
    	int temp_B = 0;
    	int SENTINEL = -99;
    	int limit;
    	
    int main()
    	{
    		cout <<"This program will determine the largest or smallest number. " <<endl;
    		cout <<"out of the group of numbers that the user chooses." <<endl; 
    		cout <<"\n";
    		cout << "\n";
    
    	while (menuChoice != 'C' || menuChoice != 'c')
    	{
    		cout <<"Choose from one of the following menu choices:" <<endl;
    		cout <<" " <<endl;
    		cout <<"A - Find the largest number with a known quantity of numbers." <<endl;
    		cout <<"B - Find the smallest number with an unknown quanity of numbers." <<endl;
    		cout <<"C - Quit \n\n\n";	
    		cout <<"Enter your choice now: ";
    		cin >> menuChoice;	
    		cout <<" " <<endl;
    			  
    		if((menuChoice == 'A' || menuChoice == 'a' || menuChoice == 'B' || menuChoice == 'b'))
    			{	
    				switch(menuChoice)
    				{
    					case 'A':
    					case 'a':
    						cout <<"You chose A - Find the largest number with a known 
                                                                 quantity. \n\n";	
    						cout <<"Please enter how many numbers you want to enter: ";
    						cin >> limit;
    						cout <<"Please enter your " <<limit<< " numbers now: "; 
    																		
    						for (; count < limit; count++)
    						{
    						    cin >> number;
    
    							if (number >= temp_A)							
    							    temp_A = number;
    						}
    								cout <<"The largest number out of the " <<limit<< " 
                                                                                 you entered is: " <<temp_A<< "\n\n\n";
    								cout <<"\t=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" <<endl;
    					break;
    
                                      //**********************************
                                      //CASE A WORKS - do not mess with!!!	
    				  //**********************************
    
    					case 'B':
    					case 'b':
    						cout <<"You chose B - Find the smallest number with an unknown
                                                                quanity of numbers. \n\n";
    						cout <<"Begin to type in your numbers: ";	
    												 				
          						for(; groupNbrs >= 0; count++)  
    							{
    								cin >> groupNbrs;
    
    								if(groupNbrs <= temp_B)
    								  {
    									temp_B = groupNbrs;	
    								   	cout <<"The smallest number in the group is: " 
                                                                                       <<groupNbrs;
    								  }
    							}  	  
    					break;
    				}
    
    	  if(menuChoice == 'C' || menuChoice == 'c')
    	  {
    		  switch(menuChoice)
    		  {
    					case 'C':
    	                                case 'c':
    						    cout <<"Please type -99 to quit.";
    							break;
    							return 0;
    							
    	     }
    	                       							
    				
    	  }
    			}
    	}
    return 0;
    }

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: No output for "smallest number"? Why?

    You initialized temp_b to 0, hence, any number you type is not smaller than 0, so it just waits till you type something smaller than 0. Try typing -1, and it will say it is your smallest number. You will also break out of your loop though. The entire logic structure looks a bit glitchy, you should try to ask yourself what exactly happens when you go through your algorithm.

    On a side note:
    1 - You check menuChoice against 'C' without ever initializing it.
    2 - Count is in conflict with std::count, since you are using namespace std
    3 - You can't go to a new line if you are writing a char array. you should do this:
    Code:
                            cout <<"You chose B - Find the smallest number with an unknown"
                                                                "quanity of numbers. \n\n";
    I suppose you are using visual studio. It lets you get away with a lot of things, but you should know it is not legal c++

    EDIT: 4 You program doesn't quit when you type c. This is because of two things:
    4.1 (menuChoice != 'C' || menuChoice != 'c') ALWAYS evaluates to true you want: (menuChoice != 'C' && menuChoice != 'c')
    4.2 Inside your loop, when you write if(menuChoice == 'C' || menuChoice == 'c'), you are already inside a loop that has made sure menuChoice can only be A or B. This is a nesting problem, probably due to the fact you don't have proper indentation.
    Last edited by monarch_dodra; April 10th, 2010 at 07:45 PM.

  3. #3
    Join Date
    Apr 2010
    Location
    Ohio
    Posts
    27

    Question Re: No output for "smallest number"? Why?

    Monarch,

    Thank you very much for your input! I got case C working now, so it quits properly when that case is chosen. However, I removed "temp_B = 0" and left it as "temp_B". I still have the same problem....after I enter a group of numbers, it immediately loops back to the menu.

    Sidenotes:

    1. I don't know what you mean about the count being in conflict for using namespace std? Why is it? I'd like to understand for future reference.

    2. Yes, I am using VS but when I pasted my code in here, it appeared the format didn't hold. The char array format in my code is one line:

    cout <<"You chose B - Find the smallest number with an unknown quantity of numbers. \n\n";

    It just showed up wrong in the paste here.

    3. I went through my case B logic and it makes sense to me but I'm a beginner so my logic could make sense to me but logically incorrect and spit out no output. This case is the only thing that's left I need to get done. I'm thinking there might be something out of whack with my IF statement within Case B, but I'm not sure. Does the logic look ok or...?


    Code:
    case 'B':
    case 'b':
    
    cout <<"You chose B - Find the smallest number with an unknown quantity of numbers. \n\n";
    cout <<"Begin to type in your numbers: ";	
    												 				
    	for(; groupNbrs <= 0; count++)  
    	{
            	cin >> groupNbrs;
    
    		if(groupNbrs <= temp_B)
    	    {
    		temp_B = groupNbrs;	
    	  	cout <<"The smallest number in the group is: " <<groupNbrs;
    	    }
    	}  	  
    	break;

    Quote Originally Posted by monarch_dodra View Post
    You initialized temp_b to 0, hence, any number you type is not smaller than 0, so it just waits till you type something smaller than 0. Try typing -1, and it will say it is your smallest number. You will also break out of your loop though. The entire logic structure looks a bit glitchy, you should try to ask yourself what exactly happens when you go through your algorithm.

    On a side note:
    1 - You check menuChoice against 'C' without ever initializing it.
    2 - Count is in conflict with std::count, since you are using namespace std
    3 - You can't go to a new line if you are writing a char array. you should do this:
    Code:
                            cout <<"You chose B - Find the smallest number with an unknown"
                                                                "quanity of numbers. \n\n";
    I suppose you are using visual studio. It lets you get away with a lot of things, but you should know it is not legal c++

    EDIT: 4 You program doesn't quit when you type c. This is because of two things:
    4.1 (menuChoice != 'C' || menuChoice != 'c') ALWAYS evaluates to true you want: (menuChoice != 'C' && menuChoice != 'c')
    4.2 Inside your loop, when you write if(menuChoice == 'C' || menuChoice == 'c'), you are already inside a loop that has made sure menuChoice can only be A or B. This is a nesting problem, probably due to the fact you don't have proper indentation.

  4. #4
    Join Date
    Apr 2010
    Location
    Ohio
    Posts
    27

    Re: No output for "smallest number"? Why?

    Just made an adjustment in the IF statement of case B. Although I'm now getting output, it always spits out the FIRST number I input. Why is that? Basically, I just changed the operator from "<=" to ">"

    The code above is the same except for the IF:

    Code:
    if(groupNbrs > temp_B)
      {
    	temp_B = groupNbrs;	
       	cout <<"The smallest number in the group is: " <<groupNbrs;
            break;
      }

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: No output for "smallest number"? Why?

    Quote Originally Posted by SoloXX View Post
    Just made an adjustment in the IF statement of case B. Although I'm now getting output, it always spits out the FIRST number I input. Why is that? Basically, I just changed the operator from "<=" to ">"

    The code above is the same except for the IF:

    Code:
    if(groupNbrs > temp_B)
      {
    	temp_B = groupNbrs;	
       	cout <<"The smallest number in the group is: " <<groupNbrs;
            break;
      }
    Because that's still not what you want to do. You want to find the smallest number. Now you're looking for the biggest.

  6. #6
    Join Date
    Apr 2010
    Posts
    7

    Re: No output for "smallest number"? Why?

    Thank you very much for your input! I got case C working now, so it quits properly when that case is chosen. However, I removed "temp_B = 0" and left it as "temp_B". I still have the same problem....after I enter a group of numbers, it immediately loops back to the menu.
    So, the answer has already been given but it sounded like you didn't understand it. The way you check temp_B is your problem. Yes, you removed its initialization to 0, but you didn't re-consider any of your logic in checking it. When you don't explicitly initialize a value in C++, it will generally leave whatever value happened to already be there, though it's very possible that in visual studio it auto-initializes it to 0 for you.

    What you want to do is make sure that temp_B has a valid value before you try to check it against anything. If temp_B does not have a valid value for your current data range, you need to give it one instead of checking it. You can do this either with an if statement inside your loop, or by accepting the first input outside of the loop. Which you go with depends on many factors regarding performance and constraints that I don't have information about, so I'll just pick the easiest to implement in place as a demonstration.

    Code:
                case 'B':
                case 'b':
                    cout <<"You chose B - Find the smallest number with an unknown quanity of numbers. \n\n";
                        cout <<"Begin to type in your numbers: ";	
    
                    for(; groupNbrs >= 0; count++)  
                    {
                        cin >> groupNbrs;
                        if (count == 0)
                            temp_B = groupNbrs;
    
                        if(groupNbrs <= temp_B)
                        {
                            temp_B = groupNbrs;	
                            cout <<"The smallest number in the group is: " 
                                <<groupNbrs;
                        }
                    }  	  
                    break;
                }
    Hope that helps.

    -Thinias

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: No output for "smallest number"? Why?

    This contains some ideas that will help you. Use the numeric_limits template to initialize your min and max variables.
    http://cplusplus.com/reference/std/l...umeric_limits/

    Consider using std algorithms instead of your own handwritten checks.
    http://cplusplus.com/reference/algorithm/max/
    http://cplusplus.com/reference/algorithm/min/

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: No output for "smallest number"? Why?

    Quote Originally Posted by Thinias View Post
    So, the answer has already been given but it sounded like you didn't understand it. The way you check temp_B is your problem. Yes, you removed its initialization to 0, but you didn't re-consider any of your logic in checking it. When you don't explicitly initialize a value in C++, it will generally leave whatever value happened to already be there, though it's very possible that in visual studio it auto-initializes it to 0 for you.

    What you want to do is make sure that temp_B has a valid value before you try to check it against anything. If temp_B does not have a valid value for your current data range, you need to give it one instead of checking it. You can do this either with an if statement inside your loop, or by accepting the first input outside of the loop. Which you go with depends on many factors regarding performance and constraints that I don't have information about, so I'll just pick the easiest to implement in place as a demonstration.

    Code:
                case 'B':
                case 'b':
                    cout <<"You chose B - Find the smallest number with an unknown quanity of numbers. \n\n";
                        cout <<"Begin to type in your numbers: ";	
    
                    for(; groupNbrs >= 0; count++)  
                    {
                        cin >> groupNbrs;
                        if (count == 0)
                            temp_B = groupNbrs;
    
                        if(groupNbrs <= temp_B)
                        {
                            temp_B = groupNbrs;	
                            cout <<"The smallest number in the group is: " 
                                <<groupNbrs;
                        }
                    }  	  
                    break;
                }
    Hope that helps.

    -Thinias
    I still think that's a really goofy loop construct. I'd be initializing temp_B to _MAX_INT and using a while loop.

  9. #9
    Join Date
    Apr 2010
    Posts
    7

    Re: No output for "smallest number"? Why?

    Quote Originally Posted by GCDEF View Post
    I still think that's a really goofy loop construct. I'd be initializing temp_B to _MAX_INT and using a while loop.
    It is a pretty goofy way to do it, I agree. I probably wouldn't initialize temp_B at all until I got my first data member, and I certainly wouldn't check if (count == 0) every iteration. I was just attempting to solve the problem in-place in a way that would work regardless of any potential constraints the original source didn't already consider.

  10. #10
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: No output for "smallest number"? Why?

    Quote Originally Posted by Thinias View Post
    It is a pretty goofy way to do it, I agree. I probably wouldn't initialize temp_B at all until I got my first data member, and I certainly wouldn't check if (count == 0) every iteration. I was just attempting to solve the problem in-place in a way that would work regardless of any potential constraints the original source didn't already consider.
    Without a count, you have to initialize it. The only reason that the count is needed is because it wasn't initialized properly. This whole example could be rewritten in about 30 minutes so there is no need to preserve any of the original source.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: No output for "smallest number"? Why?

    Quote Originally Posted by Thinias View Post
    It is a pretty goofy way to do it, I agree. I probably wouldn't initialize temp_B at all until I got my first data member, and I certainly wouldn't check if (count == 0) every iteration. I was just attempting to solve the problem in-place in a way that would work regardless of any potential constraints the original source didn't already consider.
    If you initialize it to _MAX_INT, you don't need a counter. How would you avoid checking count == 0 every iteration if you did it that way?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured