0

I am very new to programming and was wondering about the scope of variables in for loops.
I was attempting to make something that asked the user to enter a number that would represent the amount of numbers to be added together. So if they entered 3 it would add three numbers together.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
  int nTarget;
  cout <<"Enter the amount of numbers you wish to add: ";
  cin >> nTarget;
  while (nTarget < 0)
    {
        cout <<"Negative number detected, please enter a positive number: ";
        cin >> nTarget;
    }
    for(int nAccum = 0, nNext, nCounter = 0; nCounter < nTarget; nCounter++)
    {
        cout <<"Enter the number to be added: ";
        cin >> nNext;
        nAccum = (nAccum + nNext)
    }
  cout <<"The total is " << nAccum << endl;

    system("PAUSE");
    return 0;
}

I'm sorry if the code is hard to read and sloppy I was just messing around. My problem is that it gives me an error saying that "name lookup if 'nAccum' changed for ISO 'for' scoping."
Does this mean that I cannot access that variable outside of that for loop? Is there a way I could change it so that it would allow me to?
And let's say that the original code did work and it did retrieve the value of nAccum, would it even hold the accumulated value or is its value completely erased once the for loop ends?
Sorry about these really newbie questions but I wasn't able to find an answer elsewhere, and thanks to whoever decides to answer.

1
  • if you're just checking if the input is negative i would think an if-else statement should be sufficient. looping it is overkill Commented Jul 7, 2013 at 4:46

4 Answers 4

1

when you declare any variable inside the loop, its scope is only inside the loop. for example:

    for(int i=0; i<3; i++) {
   i=i+2;
}
cout<<i; // here it will give you an error because i is destroyed. outside the loop it doesn't exist.

you are doing same mistake when you cout nAccum outside the loop

Sign up to request clarification or add additional context in comments.

Comments

0

nAccum should be scoped to the function, not to the loop. Define it (and initialize it) at the top of your function, the same place as nTarget.

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
  int nTarget;
  int nAccum = 0;
  cout <<"Enter the amount of numbers you wish to add: ";
  cin >> nTarget;
  while (nTarget < 0)
    {
        cout <<"Negative number detected, please enter a positive number: ";
        cin >> nTarget;
    }
    for(int nNext, nCounter = 0; nCounter < nTarget; nCounter++)
    {
        cout <<"Enter the number to be added: ";
        cin >> nNext;
        nAccum = (nAccum + nNext)
    }
  cout <<"The total is " << nAccum << endl;

    system("PAUSE");
    return 0;
}

Comments

0

If you want to access nAccum outside the for loop, just declare it outside, e.g.

int nAccum = 0;
for(int nNext, nCounter = 0; nCounter < nTarget; nCounter++)
{
    cout << "Enter the number to be added: ";
    cin >> nNext;
    nAccum = (nAccum + nNext)
}
cout << "The total is " << nAccum << endl;

2 Comments

Thanks it didn't occur to me to do this, I feel really stupid.
The typical way to say thanks here is to accept / upvote the stuff that was helpful :)
0

Variable nAccum if declared outside the for loop will still preserve the value assigned to it in the for loop.

int nTarget, nAccum, nNext, nCounter;
cout <<"Enter the amount of numbers you wish to add: ";
cin >> nTarget;
while (nTarget < 0)
{
    cout <<"Negative number detected, please enter a positive number: ";
    cin >> nTarget;
}
for(nAccum = 0, nNext, nCounter = 0; nCounter < nTarget; nCounter++)
{
    cout <<"Enter the number to be added: ";
    cin >> nNext;
    nAccum = (nAccum + nNext)
}
cout <<"The total is " << nAccum << endl;

system("PAUSE");
return 0;

Comments

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.