Thread: Basic Calculator Program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    Basic Calculator Program

    I've written this code and for the life of me can't figure out why the compiler won't read it.

    It tells me

    Code:
    Question4.c:7:1: error: expected identifier or '(' before '{' token
    Question4.c:31:1: warning: missing terminating " character
    Question4.c:31:1: warning: missing terminating " character
    (Question4.c is the name of the file).

    To be honest I have looked all over the program, even at the lines I think the compiler is suggesting and can find no issue.

    Can anyone help?


    Code:
    #include<stdio.h>
    {
    int main()
    int num1, num2, sum;
    char operation[1];
    printf("Enter first number:");
    scanf("%d", &num1 );
    printf("Enter operation + or - or * or /: ");
    scanf("%c", &operation);
    printf("Enter second number:");
    scanf("%d", &num2 );
    "if ( operation == '+' )"
    {
    sum = num1 + num2;
    printf("The sum of %d and %d is %d ", num1, num2, sum);
    }
    "if ( operation == '-' )"
    {
    sum = num1 - num2 ;
    printf("Subtracting %d from %d is %d ", num2, num1, sum);
    }
    "if ( operation == '*' )"
    {
    sum = num1 * num2;
    printf("Multiplying %d by %d is %d ", num1, num2, sum);
    "}
    "if ( operation == '/' )"
    {
    sum = num1 / num2 ;
    printf("Dividing %d by %d is %d ", num2, num1, sum);
    }
    return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    "if ( operation == '+' )"
    Why do you keep treating these if statements as strings? Get rid of the quotation marks (around each of them).
    If you understand what you're doing, you're not learning anything.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Yeah, what itsme86 said. Also, there's an extra quote character at the end of one of your conditionals. And there's an opening brace between #include <stdio.h> and int main(). The opening brace goes after the function declaration, like this.

    Code:
    #include <stdio.h>
    
    int main(void) {
        // code
        return 0;
    }
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Interista View Post
    I've written this code and for the life of me can't figure out why the compiler won't read it.

    It tells me

    Code:
    Question4.c:7:1: error: expected identifier or '(' before '{' token
    Question4.c:31:1: warning: missing terminating " character
    Question4.c:31:1: warning: missing terminating " character
    (Question4.c is the name of the file).

    To be honest I have looked all over the program, even at the lines I think the compiler is suggesting and can find no issue.

    Can anyone help?


    Code:
    #include<stdio.h>
    {
    int main()
    int num1, num2, sum;
    char operation[1];
    printf("Enter first number:");
    scanf("%d", &num1 );
    printf("Enter operation + or - or * or /: ");
    scanf("%c", &operation);
    printf("Enter second number:");
    scanf("%d", &num2 );
    "if ( operation == '+' )"
    {
    sum = num1 + num2;
    printf("The sum of %d and %d is %d ", num1, num2, sum);
    }
    "if ( operation == '-' )"
    {
    sum = num1 - num2 ;
    printf("Subtracting %d from %d is %d ", num2, num1, sum);
    }
    "if ( operation == '*' )"
    {
    sum = num1 * num2;
    printf("Multiplying %d by %d is %d ", num1, num2, sum);
    "}
    "if ( operation == '/' )"
    {
    sum = num1 / num2 ;
    printf("Dividing %d by %d is %d ", num2, num1, sum);
    }
    return 0;
    }
    Exchange lines 2 and 3 ... braces go after things, not before.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Thanks all three for your help. I feel like I'm getting somewhere, I just can't crack the final one though.

    It still tells me that there is an expected declaration or statement at the end of input.

    Code:
    #include<stdio.h>int main(void)
    {
    int num1, num2, sum;
    char operation;
    printf("Enter first number:");
    scanf("%d", &num1);
    printf("Enter operation + or - or * or /:");
    scanf("%c", &operation);
    printf("Enter second number:");
    scanf("%d", &num2);
    if ( operation == '+' )
    {
    sum = num1 + num2;
    printf("The sum of %d and %d is %d" ,num1, num2, sum);
    }
    if ( operation == '-' )
    {
    sum = num1 - num2;
    printf("Subtracting %d from %d is %d" ,num1, num2, sum);
    }
    if ( operation == '*' )
    {
    sum = num1 * num2;
    printf("Multiplying %d by %d is %d" ,num1, num2, sum);
    if ( operation == '/' )
    {
    sum = num1 / num2;
    printf("Dividing %d by %d is %d" ,num2, num1, sum);
    }
    return 0;
    }
    Sorry, I know this is probably really simple but I'm a complete novice and I'm just finding my head is being melted. Does this stuff ever get easier?

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    #include<stdio.h>int main(void)
    Look at the example I posted up there again. In C, whitespace is mostly arbitrary, but each preprocessor statement needs its own line.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Interista View Post
    Thanks all three for your help. I feel like I'm getting somewhere, I just can't crack the final one though.

    It still tells me that there is an expected declaration or statement at the end of input.

    Code:
    #include<stdio.h>int main(void)
    {
    int num1, num2, sum;
    char operation;
    printf("Enter first number:");
    scanf("%d", &num1);
    printf("Enter operation + or - or * or /:");
    scanf("%c", &operation);
    printf("Enter second number:");
    scanf("%d", &num2);
    if ( operation == '+' )
    {
    sum = num1 + num2;
    printf("The sum of %d and %d is %d" ,num1, num2, sum);
    }
    if ( operation == '-' )
    {
    sum = num1 - num2;
    printf("Subtracting %d from %d is %d" ,num1, num2, sum);
    }
    if ( operation == '*' )
    {
    sum = num1 * num2;
    printf("Multiplying %d by %d is %d" ,num1, num2, sum);
    if ( operation == '/' )
    {
    sum = num1 / num2;
    printf("Dividing %d by %d is %d" ,num2, num1, sum);
    }
    return 0;
    }
    Sorry, I know this is probably really simple but I'm a complete novice and I'm just finding my head is being melted. Does this stuff ever get easier?
    Well for one thing you have your main function on the same line as a #include... the compiler is probably not seeing it

    For another, you should always have at least one blank line after the final closing brace on a source page... to make sure the page ends in a carriage return.

    And for your own good... learn how to correctly indent your code. Both vertical and horizontal whitespace can improve it's readability by leaps and bounds.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    Lads you're Godsends! As soon as I improved the readability I noticed the problem was a missing {

    Thanks! I just await the day that all this gets easier for me (though I don't think it ever will).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic calculator
    By Khaled in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2011, 08:17 AM
  2. A Basic Calculator.
    By bijan311 in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2009, 01:53 PM
  3. Basic Calculator
    By Surge in forum C Programming
    Replies: 18
    Last Post: 12-02-2006, 10:20 PM
  4. Basic calculator program in C++
    By linkofazeroth in forum C++ Programming
    Replies: 70
    Last Post: 08-28-2005, 04:23 PM
  5. need help with my basic c++ calculator
    By iCouch_Potato in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2004, 06:23 PM