Thread: Wrong variable value assigned

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Question 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. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    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. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    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. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    tabstop:
    how would I correct this type of program? should I use getchar() instead of scanf() for the n,a variables as well?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    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. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    If I use that; I get no output at all..

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by drag0n69 View Post
    If I use that; I get no output at all..
    Sorry, should be while (... == 2) not != 2.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    I also tried to print out:
    scanf("&#37;d %d",&n,&a)
    and I got *

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's *? And how do you print out with scanf?

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    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. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What does your code look like now?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to set pointer for environment variable
    By spiky1 in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2006, 05:19 PM
  2. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM