Resolved Using sentinel loops?

Discussion in 'Mac Programming' started by gmcguinn, Feb 9, 2011.

  1. gmcguinn, Feb 9, 2011
    Last edited: Feb 9, 2011

    gmcguinn macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #1
    Code:
    [COLOR="DarkOrange"]#include[/COLOR] <iostream>
    [COLOR="DarkOrange"]#include[/COLOR] <string>
    
    [COLOR="DarkOrange"]using namespace[/COLOR] std;
    
    [COLOR="DarkOrange"]int[/COLOR] main ()
    {										
    	[COLOR="DarkOrange"]int[/COLOR] count, SENTINEL;											
    	[COLOR="DarkOrange"]double[/COLOR] sum, min, max, average, temp;
    	[COLOR="DarkOrange"]string[/COLOR] station;
    	
    	count=0;
    	sum=0;
    	SENTINEL=-999;
    																	
    
    		cin >> station;
    		cin >> temp;												 
    		
    		max=temp;
    		min=temp;
    	
    	[COLOR="DarkOrange"]while[/COLOR] (temp != -999)
    	{															
    		count=count+1;												
    		sum=sum+temp;
    		
    		[COLOR="DarkOrange"]if[/COLOR] (temp>max)									
    		{
    			max=temp;									          	 
    		}
    		
    		[COLOR="DarkOrange"]if[/COLOR] (temp<min)									
    		{
    			min=temp;												
    		}
    		
    		cin >> temp;												
    	}
    	
    	
    	average=sum/count;
    	
    	cout << station << endl;
    	cout << [COLOR="red"]"Average is : "[/COLOR] << average << endl;
    	cout << [COLOR="red"]"Sum is : "[/COLOR]	<< sum << endl;
    	cout << [COLOR="red"]"Max is : "[/COLOR] << max << endl;
    	cout << [COLOR="red"]"Min is : "[/COLOR] << min << endl;
    	
    	
    	[COLOR="DarkOrange"]return[/COLOR] 0;
    }
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    Strip off the location name outside the loop before your first "cin >> temp;" Have you learned about strings or arrays of char yet? (EDIT: clearly since you include <string>)

    NOTE: You don't seem to be doing the min/max stuff yet.

    Check out this thread for something similar in C http://forums.macrumors.com/showthread.php?t=1092350

    B
     
  3. gmcguinn thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #3
    Yea thanks.
    After an hour or so I figured it out.
    Thanks again!
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
  5. gmcguinn thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #5
  6. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    For future reference, don't remove the content of your original post if you solved the problem. Just add the thread prefix and leave it alone. This helps future readers understand the complete issue if they had the same problem as well.
     
  7. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    I hadn't noticed that! What I meant was add a new post to the end of the thread with the fixed code and change the thread prefix. As kainjow says this gives other readers a chance to see where you were stuck and how you got around it.

    B
     

Share This Page