0

Looking for some insight on how to calculate a sum of user inputted numbers within a for statement and print it after the for loop has been completed.

So far I have this code:

//this code will sort 2 numbers then print them in ascending order and 
before exiting, add them all together 
// then average them

#include <iostream>
using namespace std;

int main(int,char**) {

int n, m, z, sort1, sort2;

for (n=0;n<3;n++){
  cout << " enter two integers (n n): ";
  cin >> m;
  cin >> z;

    if (m>z){
        sort1 = z;
        sort2 = m;
    }
    else{
        sort1 = m;
        sort2 = z;
    }
     cout << sort1 << sort2 << endl;
  }
int sum = m+z;
int sum2 = m+z+sum;
float sum3= m+z+sum2;
cout << " sum of all numbers entered: " << sum << endl;
cout << " average of the numberes entered: " << sum3 /6 << endl;
}

So I know that the sum function i have is incorrect, it only evaluates the last m+z entered by the user and not the others. If i put the sum function in the loop, once it breaks, it dumps all information within the loop rendering the sum value obsolete. Wondering if there's another way to achieve the sum function within the loop but only print once outside the loop.

Are there any other loops that don't delete information within the loop that you can extract outside?

1
  • Loops are scoped, which means that code outside the scope cannot access variables declared inside the scope. What you probably want to do is have one variable declared outside the loop, like int sum = 0, and then update it accordingly within the loop. Commented Oct 24, 2017 at 1:14

2 Answers 2

1

All loops in C++ are scoped, that means that any variables declared within a scope are not accessible outside (of the scope) nor will they persist to the next iteration.

int sum = 0; // declare sum outside of loop
for(int n = 0; 0 < 3; n++)
{
   int m, z; // These will be reset every iteration of the for loop
   cout << " enter two integers (n n): ";
   cin >> m;
   cin >> z;

   /*
       Sort and print goes here...
   */

   sum += m + z;
}
std::cout << "The sum: " << sum <<std::endl; 
Sign up to request clarification or add additional context in comments.

1 Comment

the compound assignment was what I was looking for, thanks! @Chris Mc
1
#include<iostream>
using namespace std;

int main()
{

    int total = 0, i, j, sort1, sort2;

    //this For-Loop will loop three times, every time getting two new 
    //integer from the user

    for (int c = 0; c < 3; c++) {
        cout << "Enter two integers ( n n ): ";
        cin >> i;
        cin >> j;

   //This will compare if first number is bigger than the second one. If it 
   //is, then second number is the smallest
        if (i > j) {
            sort1 = j;
            sort2 = i;
        }
        else {
            sort1 = i;
            sort2 = j;
        }

        cout << "The numbers are: " << sort1 << " and " << sort2 << endl;

   //This statement will add into the variable total, the sum of both numbers entered before doing another loop around
        total += i + j;
    }

    cout << "The sum of all integers entered is: " << total << endl;

    system("pause");

    return 0;
}

2 Comments

@Stefan What do you want me to explain? If you read the question posted, i provided a solution in which the user can input two integers up to three times in a row and the program will sort the numbers entered in ascending order and it will have a total variable adding all the integers entered and at the end output the sum of all the integers.
@Stefan You are right. I should've added some comments and more explanation. Will update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.