# Thread: Problems building a "command-line" calculator

1. ## Problems building a "command-line" calculator

I'm having an error as you can see in the image I attatched. I also attached the only c file of this program in which the error resides.

2. How many times do you think this for loop will run.
Assume i does not equal a. Or, let a be equal to 4.
Hint: Do you know what "==" means?
Code:
for(i=0; i == a; i++)
If hint is not enough read http://www.cprogramming.com/tutorial/c/lesson3.html

Tim S.

3. Stop and think what would happen if someone put in -1 ....

4. Oh thank you, I discovered my error. I should have put:
Code:
for(i=0; i < a; i++)
{
scanf("%d", &l);
int b = l + b;
}

5. I solved that error, but when i reach the answer, it still gives me 0!!

6. Originally Posted by mmario10
Code:
int b = l + b;
Have a look at this line. It creates a new variable named b, which happens to be initialized using the value of b at another scope. The one created inside the loop will not exist outside it.

Using a variable named 'l' is also an extremely bad idea, only justified by laziness. It is quite difficult to distinguish an 'l' (lowercase L) from a '|' (bitwise or). It can, depending on screen characteristics, be difficult to distinguish from other characters as well.

7. Fixed the error, thanks. And changed the 'l' variable to 'r'. Intead of doing:
Code:
int b = r + b;
I did:
Code:
b = r + b;