Thread: I always seem to be one step off (Loop question)

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    I always seem to be one step off (Loop question)

    I can't seem to get this code to work 100%, the smaller number keeps coming up as a -8******* number, everything else seems to be in order.

    Code:
    //This program will display the largest and smallest numbers entered.
    
    #include <iostream>
    using namespace std;
    
    int main()
    
    {
    	int num;
    	int LargeNum;
    	int SmallNum;
    
    
    	cout << "Enter an integer: "; 
    		cin >> num;
    
    while (num != -99)
    	{
    		cout << "Enter another integer or enter -99 to stop." << endl;
    			cin >> num;
    				if (num > LargeNum)
    					LargeNum = num;
    				if (num < SmallNum && num !=-99)
    					SmallNum=num;
    	}
    
    	cout << "The largest number entered was: " << LargeNum << endl;
    	cout << "The smallest number entered was: " << SmallNum << endl;
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    cout << "Enter an integer: "; 
            cin >> num;
    
    while (num != -99)
        {
            cout << "Enter another integer or enter -99 to stop." << endl;
                cin >> num;
    1)If you enter 100 and then enter 20, what does num equal? What happened to the other value?

    2)
    Code:
    int LargeNum;
    int SmallNum;
    When you declare a variable it has a junk value. Didn't your C++ book say to always initialize variables? 'Initializing' a variable means to assign it a value when you declare it.
    Last edited by 7stud; 02-15-2005 at 10:22 PM.

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    You must initialize the SmallNum and LargeNum, otherwise, it will be trash values. For example, SmallNum start as -5689596. A doubt that you will enter a number smaller than this.

    SmallNum = MAX_VALUE; // you choose the maximum value
    LargeValue = -99; // the user will alwas enter a value greater than this


    EDIT: 7stud, sorry we reply at same time.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    In any case, I would not recommend your implementation. There is no reason to hard code max and min values

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    I don't agree with alot of what the book says, but i'm a beginner and these are simple homework assignments, so I can't do it any other way at this point.

  6. #6
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    As you wish 7stud :


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    
    {
        int num;
        int LargeNum;
        int SmallNum;
    
    
        cout << "Enter an integer: "; 
            cin >> num;
           LargeNum = SmallNum = num;
    while (num != -99)
        {
            cout << "Enter another integer or enter -99 to stop." << endl;
                cin >> num;
                    if (num > LargeNum)
                        LargeNum = num;
                    if (num < SmallNum && num !=-99)
                        SmallNum=num;
        }
    
        cout << "The largest number entered was: " << LargeNum << endl;
        cout << "The smallest number entered was: " << SmallNum << endl;
    
    
    
    return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Thanks alot Mortissus, explain to me in Lehman's terms how that computes?

    EDIT- When I assigned the variables values, the output was giving me the smallest number only, not the largest, that's why I ask.

  8. #8
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    I do not know if I really understood your question. If you are asking about what Iīve changed, I can explain. You initialize with the first value you have, all others values will be smaller or grater than the first (it does not matter if this is true). In any case, you have a right initialization, since it is user input. Donīt you agree that the first value entered is the greater and smaller value at the same time? Anyway... if this was not your question, ignore this post and ask again, please.

    EDIT - I saw you edit now, are you telling that the code does not worK?
    Last edited by Mortissus; 02-15-2005 at 10:38 PM.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Quote Originally Posted by Mortissus

    EDIT - I saw you edit now, are you telling that the code does not worK?

    no no, it works now, I also see what you are saying as far as LargeNum technically being the same as SmallNum.

    Once I read these codes a few times it sinks in.


    Thank again

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hey Mortissus,

    Even without the poster saying, it was pretty obvious this was homework. Maybe you should consider not just posting solutions, which provide very little real help to a beginner.

  11. #11
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Homework or not, he posted a great piece of code. He was not asking for us to do it. And, it is his responsability to learn. If he donīt do this now it is his problem later. Iīve made little to help, one line of code... do you think this is such a problem? I donīt...

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Homework or not, he posted a great piece of code.
    Ok, if you say so.

    He was not asking for us to do it.
    Then why did you?

    Iīve made little to help, one line of code... do you think this is such a problem?
    Which robbed him of the learning experience. Part of programming has to do with struggling through certain concepts. The struggle often teaches a new programmer many different things as different solutions are attempted. And, the lessons learned during the struggle tend to make a bigger impression in our minds than just reading the correct answer.

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Well I obviously appreciate his help, but I had everything right except the output of the smallest integer, so I don't see it as that big of a deal, but I suppose I can see your point, 7stud.

  14. #14
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by 7stud
    Ok, if you say so.
    The code solves the problem.

    Quote Originally Posted by 7stud
    Then why did you?
    It costs me nothing, I like to help.

    Quote Originally Posted by 7stud
    Which robbed him of the learning experience. Part of programming has to do with struggling through certain concepts. The struggle often teaches a new programer many different things as different solutions are attempted. And, the lessons learned during the struggle tend to make a bigger impression in our minds than just reading the correct answer.
    It seems that he is going to learn something:

    Quote Originally Posted by SquirrelRJ
    Once I read these codes a few times it sinks in.
    And also:
    Quote Originally Posted by 7stud
    In any case, I would not recommend your implementation. There is no reason to hard code max and min values.
    Really? And why is that? He learned two good ways of solving this problem. If you hadnīt talk this way, I wouldnīt post the solution.

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    7stud, showing SquirrelRJ how to do it is in no way bad at all. if SquirrelRJ wants to learn, then he will learn. if not, that's one less person that will be competent enough to take your job. You should give them the answer, and if they're serious about programming, they'll look at it, and (maybe with help) figure out exactly what it is that's going on. not helping and just saying 'STFW' or 'RTFM' do not help in any way. people come here to get help with programming, not to be shown how to do a google search.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM