C++ program is giving a too many arguments warning

后端 未结 2 898
故里飘歌
故里飘歌 2021-01-29 16:15

I barely know what I\'m doing, I have this code where I try to solve some simple math:

 #include
 #include
 #include

        
相关标签:
2条回答
  • 2021-01-29 16:59

    Corrected code (fixes as pointed out in the comments):

    #include<stdio.h>
    //#include<conio.h> // You don't use anything from these headers (yet?)
    //#include<stdlib.h>
    
    //main()
    int main() // main has to be defiend as an int function
    {
        int n, sum = 0;
        printf("ENTER NUMBER:");
    //  scanf("%i", n);
        scanf("%i", &n); // scanf need the ADDRRESS of variables
        while (n > 0)
        {
            sum += n;
            n--;
        }
    //  printf("\n sum is:", sum);
        printf("\n sum is: %i", sum); // printf needs a format specifier for each variable
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-29 17:18

    The compiler is warning you that you forgot to specify the field for sum in the format string. You probably wanted:

    printf("\n sum is: %d",sum);
    

    As above, it will not print the sum, and the sum value will not be used. Hence the warning.

    0 讨论(0)
提交回复
热议问题