Thread: Help writing calculator program (newbie)

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Help writing calculator program (newbie)

    Hi, I've recently begun to learn how to program in C, I wrote a calculator program that worked so I decided to update it, but my updated source has errors that I don't know how to fix. My situation is this, my compiler tells me that I have to fix Error A, after I fix Error A, it tells me I have to fix Error B, when I fix Error B, it tells me I have to fix Error A again. Very confusing. Anyhow this is the source I have written.


    -=begin=-

    #include <stdio.h>
    #include <string.h>

    main()

    {
    int number1, number2, choice5;

    printf("Welcome to Calculator 0.2, written by Danny Two, this is an update to an already crappy program, Hope you enjoy!\n\n");
    printf("[1] Addition\n");
    printf("[2] Subtraction\n");
    printf("[3] Multiplication\n");
    printf("[4] Division\n");

    scanf("%d", &choice5);

    if {
    (choice5 == 1);
    printf("You have chosen Addition, Please enter the two numbers you wish to add\n");
    scanf("%d", &number1);
    scanf("%d", &number2);
    printf("Sum is %d\n", number1 + number2);
    }

    if {
    (choice5 == 2);
    printf("You have chosen Subtraction, Please enter the two numbers you wish to subtract\n");
    scanf("%d", &number1);
    scanf("%d", &number2);
    printf("Sum is %d\n", number1 - number2);
    }

    if {
    (choice5 == 3);
    printf("You have chosen Multiplication, Please enter the two numbers you wish to multiply\n");
    scanf("%d", &number1);
    scanf("%d", &number2);
    printf("Sum is %d\n", number1 * number2);
    }

    if {
    (choice5 == 4);
    printf("You have chosen Division, Please enter the two numbers you wish to divide\n");
    scanf("%d", &number1);
    scanf("%d", &number2);
    printf("Sum is %d\n", number1 / number2);
    }

    return 0;

    }

    -=end=-

    The error messages I am getting for that is...


    calc02.cpp
    C:\c++ programs\Calculator\Calculator 02\calc02.cpp(20) : error C2059: syntax error : '{'
    C:\c++ programs\Calculator\Calculator 02\calc02.cpp(20) : error C2143: syntax error : missing ';' before '{'
    C:\c++ programs\Calculator\Calculator 02\calc02.cpp(28) : error C2059: syntax error : '{'
    C:\c++ programs\Calculator\Calculator 02\calc02.cpp(28) : error C2143: syntax error : missing ';' before '{'
    C:\c++ programs\Calculator\Calculator 02\calc02.cpp(36) : error C2059: syntax error : '{'
    C:\c++ programs\Calculator\Calculator 02\calc02.cpp(36) : error C2143: syntax error : missing ';' before '{'
    C:\c++ programs\Calculator\Calculator 02\calc02.cpp(44) : error C2059: syntax error : '{'
    C:\c++ programs\Calculator\Calculator 02\calc02.cpp(44) : error C2143: syntax error : missing ';' before '{'
    Error executing cl.exe.


    When I add a ";" before the "{"s all the syntax errors about the "{" and the missing ";" will be gone, but instead it adds

    error C2059: syntax error : ';'

    to the equation and I dunno what to do after that.

    Any help greatly appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The structure of your if statement is wrong. Here, look at this:
    Code:
    if (something == 1)
    {
        printf ("Something is 1\n");
    }
    note the position of the brackets, and semi-colons. Compare it to your version, and try again.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    2
    Thanks, Hammer! It worked. Thanks for helping out a n00b heh. I'll never make that mistake again.

    Once again thanks.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Thanks, Hammer!
    No problem
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  2. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  3. Newbie - How do I know if I'm writing C code?
    By dfinner in forum C Programming
    Replies: 4
    Last Post: 07-31-2002, 04:17 PM
  4. newbie needs help writing prgrm
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 06-18-2002, 12:38 PM