Just started programming in C, and so far, it has been great...

This is a discussion on Just started programming in C, and so far, it has been great... within the C Programming forums, part of the General Programming Boards category; However, I am running into a syntax error. Code: #include <stdio.h> int main (void) { int num1; int num2; int ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Just started programming in C, and so far, it has been great...

    However, I am running into a syntax error.

    Code:
    #include <stdio.h>
    
    int main (void)
    
    
    {
        int num1;
        int num2;
        int sum;
        int product;
        
        sum = num1 + num2;
        product = (num1 * num2);
        
        
        printf("Enter your numbers!\n");
        
        scanf("%d", &num1);
        scanf("%d", &num2);
        
        
        printf("Your sum is: %d", sum);
        
        if (num1 > num2)
        {
                 printf("\nThe first number is greater than the second one!\n");
                 }
                 
                 if (num1 < num2) 
                 {
                          printf("\nThe first number is less than the second number!");
                          }
                          
                          char choice;
                          
                          printf("Would you like to multiply the two numbers?");
                          scanf("Yes");
                          scanf("No");
                          
                          if (choice == "Yes") {
                          printf("The product of the two numbers is... \n%d", product);
                          }
                          
                          
                          
                          
                          
    }
    It says that there is a "comparison between point and integer" on the line with the bold text.

    Can you correct my issue? I am making a basic calculator.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    To me it says that this a warning.We will fix it,but first consider that we use to end main by the line return 0;,which says that the program terminated successfully.
    Now the basic problem i think is that you calculate sum and product before even taken the numbers from the user.You should first take the input(check it maybe if you want) and then do the calculations.

    scanf("YEs"); and scnaf("NO") has no reason of living.You should create a string(an array of char maybe) that will be used to hold the input of the user.As you wrote scanf about the integers try to write it again for a char.

    As for the comparison between pointer and integer,well the char choice is not exactly what you need.You need something to store a word(a string),not something to store just a characters,as char choice does.You need an array of char(an array of char maybe)
    Last edited by std10093; 08-07-2012 at 07:26 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    You have defined choice to be a single character. That means you can't compare this single character to the string constant "Yes".
    Also this snippet is incorrect:
    Code:
    scanf("Yes");
                          scanf("No");
    I really don't know what exactly you are trying to do with these two lines, but I suggest you look up the documentation for scanf().

    Jim

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Another thing i would state is that comparison between string is done with strcmp and not with the operator ==

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    Another thing i would state is that comparison between string is done with strcmp and not with the operator ==
    True but strcmp() requires two strings, not a string and a character. The OP needs to decide if he wants the user to enter a single character or a string then use the proper method.

    Jim

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by jimblumberg View Post
    True but strcmp() requires two strings, not a string and a character. The OP needs to decide if he wants the user to enter a single character or a string then use the proper method.

    Jim
    I would say that by the "YES" and "NO" we see he/she wanted to compare to strings,but ok we could wait

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Nigeria
    Posts
    15
    Am also a learner like you pal, hope i can offer a lil assistance.
    I checked it and i think the pointer issue is present cos of this:
    Code:
    if(choice=="Yes")
    On more thing, as earlier said, you calculated sum and product too earlier, you should atleast get the inputs of the person before calculating. Theres a possibility that the results of the calculation wont be correct!
    In your code, this is in the wrong position.
    Code:
    char choice;
    it should be done at the beginning.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Are you guys kidding me? Even if choice was an array instead of a single char you still wouldn't compare its "value" to "Yes" or "No" using ==. The OP's problem is that he has no idea how scanf works as indicated by Jim and that he doesn't understand that string comparison involves using library functions not ==.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    No we do not kidding you .We stated what you said about the string comparison above

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just started programming. Help please.
    By Ra.one in forum C Programming
    Replies: 1
    Last Post: 10-28-2011, 02:05 PM
  2. Getting started with programming
    By Nathan in forum C++ Programming
    Replies: 21
    Last Post: 11-27-2006, 02:51 PM
  3. How did you get started in programming?
    By ZooTrigger1191 in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 07-18-2003, 12:26 PM
  4. programming was great until c came along
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-04-2002, 07:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21