Thread: Calculator Syntax Error

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Calculator Syntax Error

    Hello guy, i'm new here so i don't exactly know how thing are run. But anyways i was trying to create a basic calculator but i kept getting the error, "Syntax error at end of input" every time i tried to compile and run it.
    If you guys could take a look and see where my mistake was, it would be greatly appreciated.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    
    
    int Addition(int x, int y);
    int Subtraction(int x, int y);
    int Multiplication(int x, int y);
    int Division(int x, int y);
    
    
    int main( void ) {
        int choice;
        int num1;
        int num2;
        int answer;
        int exit;
        
        int x;
        int y;
        
        int yes = 0;
        int no = 1;
    
    
        Start:
              
        system("CLS");
        system("COLOR 4F");
        
        printf("                              Welcome to Zach's Commandline Equation Calculator!\n");
        printf("\n\nInsert the number of the type of equation you wish to be calculated:\n");
        printf("1- Addition\n");
        printf("2- Subtraction\n");
        printf("3- Multiplication\n");
        printf("4- Division\n");
        printf("5- Exit Calculator");
        
        scanf("%d,\n", &choice, "\n");
        
        switch(choice)
        {
        
        case 1:
             
             printf("\n");
             scanf("%d%d", &num1, &num2);
             
             printf("\n\n%d", addition(num1, num2));
             getchar();
             goto Start;
             break;
             
        case 2:
             
             printf("\n");
             scanf("%d%d", &num1, &num2);
             
             printf("\n\n%d", subtraction(num1, num2));
             getchar();
             goto Start;
             break;
             
        case 3:
             
             printf("\n");
             scanf("%d%d", &num1, &num2);
             
             printf("\n\n%d", multiplication(num1, num2));
             getchar();
             goto Start;
             break;
             
        case 4:
             
             printf("\n");
             scanf("%d%d", &num1, &num2);
             
             printf("\n\n%d", division(num1, num2));
             getchar();
             goto Start;
             break;
             
        case 5:
             
             printf("\nAre you sure you want to exit from this operation?\n");
             printf("\n0 - yes\n1 - no");
             
             scanf("%d", &exit);
             
             if(exit == yes)
             {
               system("EXIT");
               }
             else if (exit == no)
             {
               goto Start;
               }
             else
             {
               goto Start;
               }
               break;
             default:
               {
                     goto Start;
                     break;
                       }
     
      getch();
      return 0;
    }
    Also, any improvements would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you see how the word exit is colored in your code? That's because exit is a keyword in C - you can't use it for the name of a variable.

    You have a nice general program, but then there are the goto's aren't there? >.<

    Two suggestions - 1) learn what your compiler errors really mean, so you can find simple errors in your code and 2) learn to structure your programs using loops and or functions, instead of goto's.

    I'm not a no-goto fanatic, but on a larger program, goto's can be *massive* headaches when you need to extend or debug code.


    And Welcome to the Forum! XD

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Which exit?

    This?
    Code:
    if(exit == yes)
    Or this?
    Code:
    system("EXIT");
    BTW, a friend helped me with the goto's and i replaced them with
    Code:
    main();
    Last edited by zajohnson; 03-02-2012 at 09:54 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zajohnson View Post
    Which exit?
    Quote Originally Posted by Adak View Post
    the name of a variable.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Sorry to keep this thread alive but now get a new error. I fixed the syntax error and input the functions but now every time i go to input the to numbers to see if it works it goes straight back to Start:. BTW The exit command works just fine, just the addition, subtraction, etc. don't.

    Can you tell me what i need to change?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    
    
    int Addition(int x, int y);
    int Subtraction(int a, int b);
    int Multiplication(int c, int d);
    int Division(int e, int f);
    
    
    int main( void ) {
        int choice;
        int num1;
        int num2;
        int answer;
        int leave;
        
        int x;
        int y;
        
        int yes = 0;
        int no = 1;
    
    
        Start:
              
        system("CLS");
        system("COLOR 4A");
        
        printf("                 Welcome to Zach's Commandline Equation Calculator!\n");
        printf("\n\nInsert the number of the type of equation you wish to be calculated:\n");
        printf("1- Addition\n");
        printf("2- Subtraction\n");
        printf("3- Multiplication\n");
        printf("4- Division\n");
        printf("5- Exit Calculator\n\n");
        
        scanf("%d,\n", &choice, "\n");
        
        switch(choice)
        {
        
        case 1:
             
             printf("\nInput the two numbers you wish to add here then press Enter:");
             scanf("%d %d", &num1, &num2);
             int x = num1;
             int y = num2;
             answer = num1 + num2;   
             getchar();
             main();
             break;
             
        case 2:
             
             printf("\nInput the two numbers you wish to subtract here then press Enter:");
             scanf("%d %d", &num1, &num2);
             int a = num1;
             int b = num2;
             answer = num1 - num2;
             getchar();
             main();
             break;
             
        case 3:
             
             printf("\nInput the two numbers you wih to be multiplied here then press Enter:");
             scanf("%d %d", &num1, &num2);
             int c = num1;
             int d = num2;
             answer = num1 * num2;
             getchar();
             main();
             break;
             
        case 4:
             
             printf("\nInput the two numbers you wish to be divided here then press Enter:");
             scanf("%d %d", &num1, &num2);
             int e = num1;
             int f = num2;
             answer = num1 / num2;
             getchar();
             main();
             break;
             
        case 5:
             
             printf("\nAre you sure you want to exit from this operation?\n");
             printf("\n0 - yes\n1 - no\n");
             
             scanf("%d", &leave);
             
             if(leave == yes)
             {
               exit(0);
               }
             else if (leave == no)
             {
               main();
               }
             else
             {
               main();
               }
               break;
             default:
               {
                     main();
                     break;
                       }
    
    
      getchar();
      return 0;
    }
    
    
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are recursively calling main. That's bad. Just use a loop:
    Code:
    do
        get input
        check input
        use input
    while input is not whatever you use to quit
    http://www.cprogramming.com/tutorial/c/lesson3.html


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    • Get rid of the Start: on line 27, since you're no longer using goto.
    • Never call main recursively. Remove all those. Use a loop to repeat your program (example below).
    • You never print the answer.
    • Get rid of all the useless variable declarations inside the switch cases: x, y, a, b, etc. You never use them.
    • Your default case should print invalid choice, so the user knows what the problem was.

    Example:
    Code:
    int done = 0;  // set done to false
    ...
    do {
        print menu
        get choice from user
        switch (choice)
        {
            ...
            case 5:
                ask if they're sure
                if they're sure, set done to true
                break;
        }
    } while (!done);

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Does this apply to every case or just case 5?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Does what apply to every case? Remove the recursive call to main from every case. Remove the unused variables from every case. Print the answer in every case that produced an answer. Case 5 is the only case for quitting, so that's the only case you ask "Are you sure you want to quit?" and the only case where you set done to true.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-30-2011, 11:55 AM
  2. Error "in function 'main' syntax error before 'int' Help Please
    By blackhat11907 in forum C Programming
    Replies: 5
    Last Post: 08-20-2011, 07:05 PM
  3. error C2061: syntax error : identifier
    By maninboots in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2009, 05:40 AM
  4. error C2143: syntax error : missing ')' before ';'
    By steve1_rm in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:06 AM
  5. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM