# Wrong variable value assigned

This is a discussion on Wrong variable value assigned within the C Programming forums, part of the General Programming Boards category; Hello: I am trying to make an application which takes input of sets of 2 numbers and does a mathematical ...

1. ## Wrong variable value assigned

Hello:
I am trying to make an application which takes input of sets of 2 numbers and does a mathematical equation with them and displays the answer for each set.
However; I have been testing and testing this code:
Code:
```/* CSC1140@ 07611201 74kzYkXl 10523 */
#include <stdio.h>

int main()
{
int c,n,a;
while(c = getchar() != EOF)
{
int sum=0;
scanf("%d",&n);
scanf("%d",&a);
int i;
for(i=1; i<=n; i++)
{
int calc=0,x=0,final_a = a;
for(x=1;x<i;x++)
{
final_a = final_a * a;
}
calc = i * final_a;
sum = sum + calc;
}
printf("N:%d  A:%d\n",n,a);
printf("%d\n",sum);
}
}```
I am using the input as
Code:
```3 3
4 4```
The output displays the numbers as it is supposed to; but gives the wrong answers. So I debugged my code and printed out N and A for each set and found out that the "a" variable is not being correctly assigned.
The "a" value seems to be assigned 4 in the first set (a is supposed to be 3).
the n is correctly assigned.
Maybe it is a really simple mistake I just cant see... any help would be appreciated!

2. I think your problem is thus:

Code:
```     for(i=1; i<=n; i++)
{
int calc=0,x=0,final_a = a;
for(x=1;x<i;x++)
{
final_a = final_a * a;
}
calc = i * final_a;
sum = sum + calc;
}```
You are initializing EACH time during the for loop!

3. As to the input issue, c grabs a character and throws it away. So c gets the first 3, n gets the second one, and a has to take the next 4.

4. Code:
`while(c = getchar() != EOF)`
This likely doesn't do what you want: it will assign either a 0 or a 1 to c due to precedence rules. Instead, you want:
Code:
`while( (c = getchar()) != EOF )`

5. Thanks for the help!
So after reading all the replies I got something like this:
Code:
```#include <stdio.h>

int main()
{
int c,n,a,calc,sum=0,x,i,final_a;
while((c = getchar()) != EOF)
{
sum = 0;
scanf("&#37;d",&n);
scanf("%d",&a);
final_a = a;
calc = 0;
for(i=1; i<=n; i++)
{
for(x=1;x<i;x++)
{
final_a = final_a * a;
}
calc = i * final_a;
sum = sum + calc;
}
printf("N:%d  A:%d\n",n,a);
printf("%d\n",sum);
}
}```
So in the future; should I always declare ALL variables(int) before any loops? And only set them to 0 when I need to reset them?

Thanks

6. tabstop:
how would I correct this type of program? should I use getchar() instead of scanf() for the n,a variables as well?

7. Well, you need to consider the scope of the variable and "do the right thing" - it's not necessarily right to put them all at the beginning of the code. Intializing variables where you declare the variable is definitely a good idea, but sometimes it doesn't work that way.

[Actually, I don't think tabstops comment is correct - the variables are just temporaries within the loop anyways].

Your code is still eating a char from the input and not using it for numeric input.

--
Mats

8. So how would I correct the problem?
Should I click the input first for n,a first? But then I couldn't loop the input and I wouldn't know where to stop...
Any help would be greatly appreciated!

9. One way would be to use the return value from scanf(), something like this:
Code:
`while (scanf("%d%d", &n, &a) != 2)  ...`
scanf() returns how many values it successfully read through the call, and you are requesting two values (in your code, you use two calls to scanf, but it's easier when doing this checking to just read two on one line).

--
Mats

10. If I use that; I get no output at all..

11. Originally Posted by drag0n69
If I use that; I get no output at all..
Sorry, should be while (... == 2) not != 2.

--
Mats

12. I also tried to print out:
scanf("&#37;d %d",&n,&a)
and I got *

13. What's *? And how do you print out with scanf?

14. Ahh I see. That works! Thanks so much.
Now my values are screwed up though. Like when I try to ech out x,i,final_a or any variable; they are all really high messed up values; maybe memory addresses?

15. What does your code look like now?

--
Mats

Page 1 of 2 12 Last