help2???

This is a discussion on help2??? within the C++ Programming forums, part of the General Programming Boards category; im guess i try to start over somewhat im trying to use user data until the user inputs -1 if ...

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

    help2???

    im guess i try to start over somewhat
    im trying to use user data until the user inputs -1

    if the user inputed say 4 5 -1

    my output would look something like

    a counter the number of integers used ie 2
    the sum ie 9
    the largest number ie 5
    the smallest number ie 4
    the product ie 20
    the avg ie 4.5

    -1 should not be used to calculate any of the data below

    i could use some help
    teach myself c++ in 24 hours isnt seeming to help

    Code:
    #include <iostream> /*for cin and cout*/
    
    
    int main () /*function main*/
    {
    	int x=0,x2=0,x3=0,sum=0,number=0,product=0,largest=0,smallest=0,flag=0; /*declaration of variables*/
    	float avg;
    
    //if (flag != -1) //here's the start of the loop	
    //	std::cout << "Please enter an interger" << std::endl; /*input data*/
    //	std::cin >> flag;
    do {
    
    	std::cout << "Show this again? ( -1 for no ): ";
    	std::cin  >> flag;
    
    } while ( flag != -1 );	
     
      
         sum = x + x2 +x3; /*total score*/
         avg = (sum)/2; /*average score*/
    
        std::cout << "The number of the integers is" << number << 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;
    
    	//here's where user decides to stop or continue
    	std::cout << "enter -1 to stop, anything to do it all over again" << std::endl;
    	std::cin >> flag;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    int num;
    int sum = 0;;
    float avg;
    int count = 0;
    int largest;
    int smallest;
     
    while(1) //never ending loop
    start loop body
    cout enter an integer enter -1 to stop
    cin >> num
    if num == -1
    	 break; //get out of the loop
    else
    start else body
    	 sum += num
    	 count++;
    	 avg = sum/count;
    	 if count == 1
    	 smallest = largest = num
    	 else
    	 start else body
    	 if num < smallest
    		 smallest = num
    	 if num > largest
    		 largest = num
    	 end else body
    end else body
    end loop body
    Last edited by elad; 09-16-2004 at 10:48 AM.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Elad's pseudo code is a good way to go, probably more appropriate than this method. However...

    You could also use a container to hold the numbers. The problem is that you don't know how many numbers because it depends on when the user stops. You might want to use an array. I would actually use a list from the STL. The syntax might be more daunting, but it is easier to get right and handles memory management on its own.

    Here is an example that uses a list. It asks the user for integers that it adds to the list, then it iterates through the list and outputs the numbers. You can change the code inside the for loop to do whatever you want - get the sum, get the minimum, get the product, etc.
    Code:
    #include <iostream>
    #include <list>
    
    int main()
    {
        int input;
        std::list<int> numberList;
    
        do
        {
            std::cout << "Input an integer (-1 to quit): ";
            std::cin >> input;
    
            if (input !=-1)
                numberList.push_back(input);
    
        } while (input != -1);
    
        std::list<int>::const_iterator iterNumber = numberList.begin();
        std::list<int>::const_iterator listEnd = numberList.end();
        for (; iterNumber != listEnd; ++iterNumber)
        {
            int currentNumber = *iterNumber;
            std::cout << currentNumber << std::endl;
        }
    }
    Last edited by jlou; 09-16-2004 at 11:15 AM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I agree with jlou. If you want to keep track of all the individual integers used, then you need to use a container. An array can be used if you know how many integers is the maximum you want to allow. You can create your own list if you don't know about the containers in the Standard Template Library (STL). You can use a vector or a list from STL if you know about them.

    These examples allow you do collect an initially undetermined number of integer inputs in a sequential fashion. If you want to group them 2 by 2 or in some other manor, then there are variants (on a similar theme) that can be used. You do have to be very explicit in what you are trying to do to get the code to do it for you, so make sure you understand the goal before you start to write the code, unless you like working on all the variations.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    Code:
    #include <iostream> /*for cin and cout*/
    
    int main() /*function main*/
    {
    int num;
    int sum = 0;
    float avg;
    int count = 0;
    int largest;
    int smallest;
     
    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 = sum;
    			else;								//end body;
    	else;								//end body;
    }										//end loop body;
    return 0;
    }
    all my else staements cant find a matching if?????

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if the body of a loop (for, while, do/while) or logic statement (it/else if/else) contains more than one line, then you need to use { to indicate the start of the body and } to indicate the end of the body. Therefore, substitute the correct curly brace at the correct spots in the pseudocode. I find it difficult to maintain my indenting pattern when posting to the board. If you are using a single line body I would recommend indenting the line (I use two spaces but it came out as a tab on the board). I see that didn't carry over from the edit window to the board. Sorry.


    Don't use simicolons after the conditional clause of a loop or logic

    if (num == -1); //drop this semicolon
    break; //indent this line

    [Edit] alsotry to work through any code samples you may be given for silly mistakes, like the one (at least) in the original pseudocode I posted (and subsequently edited). It should be

    if(largest < num)
    largest = num; //not sum---typo in my original post.
    Last edited by elad; 09-16-2004 at 12:20 PM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    29
    thanks for all your help im down to one error but have to go to work
    this is diffently been a learning expierence

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning reference from class
    By l2u in forum C++ Programming
    Replies: 17
    Last Post: 12-16-2007, 10:21 AM
  2. External error
    By redsoxj in forum C Programming
    Replies: 4
    Last Post: 03-29-2002, 07:51 AM

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