-1

I am attempting to build a form/gpa calculator for class, but when entering the amount of classes using scanf, the output is 6,487,576 regardless of what I enter.

int main()
{
    int opt;
    int c;




    printf("*******************************\n");
    printf("** Fanshawe Grade Calculator **\n");
    printf("*******************************\n");

    printf("Please Choose an Option:\n");
    printf("[1] Enter Your Marks\n");
    printf("[2] Quit\n");

    scanf("%d", &opt);

    switch(opt) {
      case 1 :
         printf("********************************************************\n");
         printf("** Enter Your Marks For Your Courses (Up to 10 Only): **\n");
         printf("********************************************************\n");

         scanf("%i", &c);
         printf("You Have Entered %i Classes!\n", &c);

         /*int i;
         for(i=1;i=c;i++) {
            printf("Enter Your Mark for Class #%i\n", &i);
         }*/

         break;

      case 2 :
         printf("GoodBye!");
         exit(0);
         break;
   }
    return 0;
}

Help please!

E Yansen
  • 1
  • 1

3 Answers3

1

When you use %i with scanf(), it can allow you to input hexadecimal and octal numbers as well (this isn't an issue as explained here). However, you shouldn't use & while using printf() as it'll display the memory location of the variable instead of the value stored in it.

Try this:

case 1 :
         printf("********************************************************\n");
         printf("** Enter Your Marks For Your Courses (Up to 10 Only): **\n");
         printf("********************************************************\n");

         scanf("%d", &c);
         printf("You Have Entered %d Classes!\n", c);
         break;
1

printf("You Have Entered %i Classes!\n", &c) outputs the address of the variable c (&c takes the address), i.e. it outputs the number of the memory cell. Since you want to pass the value of the variable you should not use the operator of taking address of a variable &.

printf("You Have Entered %i Classes!\n", c);
273K
  • 29,503
  • 10
  • 41
  • 64
1

There are two problems here. First, you need to include the following headers:

#include <stdio.h>    // declares functions like printf and scanf
#include <stdlib.h>   // declares functions like exit

If you don't include these headers, then the compiler does not know how to execute them or their exact formats. When you attempt to compile, the warnings will not be helpful. There are a few good websites for this; just search something like "c exit()" in Google and it will tell you which headers you need in the future. When I compiled this code, I used gcc main.c -Wall which forces all warnings to be displayed.

Secondly, you use the address of c in both cases. When you go to print the value of c, you are actually printing the address of c, not the value. scanf() takes the address(es) of the variables to store values in, but printf() takes the variables themselves. The code should look more like this:

scanf("%i", &c);
printf("You Have Entered %i Classes!\n", c);

It is also always a good idea to initialize your variables. This way, you know when something is assigned improperly, or not assigned at all. I forgot to assign my variables for a while, and when compiled on another machine, it didn't work. This is because different machines will initialize variables differently. Just change the code to the following:

int opt = 0;
int c = 0;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278