help 3 ???

This is a discussion on help 3 ??? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> /*for cin and cout*/ int main() /*function main*/ { int num = 0; float sum= 0; float ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    29

    help 3 ???

    Code:
    #include <iostream> /*for cin and cout*/
    
    int main() /*function main*/
    {
    int num = 0;
    float sum= 0;
    float avg= 0;
    int count = 0;
    int largest = 0;
    int smallest= 0;
    int product = 0;
    
    while (1)				//never ending loop//start loop body
    {										
    	std::cout <<"Please enter an integer enter (-1 to stop)";
    	std::cin >> num;
    	   
    	if (num == -1)
    {		break;							//get out of the loop
    }    else;
    		sum += num;						//  start else body
    		count++;
    		avg = (sum)/count;
    		
    		if (count == 1)
    {		smallest = largest = num;
    }		else;
    										//	 start else body;
    			if (num < smallest)
    	{	 	smallest = num;
    			
    				if (num > largest)
    				largest = num;
    			else;								//end body;
    }	else;								//end body;
    }										//end loop body;
       	
    	product = (smallest * largest); //product = num * num??
    		
    	std::cout << "The number of the integers is " << count << std::endl;
    	std::cout << "The sum of the integers is " << sum << std::endl;
    	std::cout << "The average of the integers is " << avg << std::endl;
    	std::cout << "The product of the integers is " << product << std::endl;
    	std::cout << "The largest integers is " << largest << std::endl;
    	std::cout << "The smallest integers is " << smallest << std::endl;
    
    	return 0;
    }

    this board has been a tremendous amount of help and I have a couple of ???

    1) If i enter three intergers into the program say 5 4 2 -1
    everything work except my product out put it ends up with 10 instead of say 40, does that statement have to be in the while loop
    somewhere???

    2) smallest = largest = num; I'm a little confused about this statement if i enter say 5 4 2 is it ending up assigning 2 to num after the program runs???

    many thanks to all

  2. #2
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    because it is the product of largest (5) and smallest (2)
    5x2=10

    Code:
    product = (smallest * largest);
    if you want the product of all numbers: then do smthing like this:
    Code:
    product = 1;
    Code:
    sum += num;
    product *= num;
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    cool!! still confused about what

    Code:
    if (count == 1)
    	smallest = largest = num;

    does???

  4. #4
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    that assigns the very first input number to smallest and largest

    ie: u entered 5 as ur first num
    then largest = num, and smallest = largest
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    what concerns me is....

    Code:
    while(1)
    As far as i have seen in my c+ career.. there has been some sort of condition that will at some point, terminate the loop.

    example:

    Code:
    while (counter < 5)
    or something like

    Code:
    while (choice != 'Q')
    I would like to see some sort of variable in your while() loop.. by having only a constant.. i am not sure if your loop will ever terminate.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,208
    His loop will terminate with this code:
    Code:
    if (num == -1)
    {
        break;    //get out of the loop
    }
    break will, as it says, break a loop or switch, so he has a condition where the loop terminates.

Popular pages Recent additions subscribe to a feed

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