Thread: I've ran into a problem

  1. #1
    Mike Jones
    Guest

    Question I've ran into a problem

    Ok firstly, here is the assignment again, once again I'm not asking for an answer or any code, just what I'm dong wrong, hints, etc.

    **************************************************
    You are to write a program that evaluates simple arithmetic expressions which are composed by positive numbers (real or integers) and the four basic arithmetic operators: +, -, * and /. The program will have to adhere to the following rules:

    The expressions will be evaluated left to right (all operators will have the same precedence).
    Blank spaces are ignored.
    The expression is terminated by the end of the line, at which point you will display the answer.
    The prompt is indicated with >.
    You have to guard against illegal characters entered.
    The program can be terminated only by pressing <CTRL-C>.
    Ex:

    > 2 + 3 * 4/6
    3.333333
    > 2/4+3
    3.500000
    > 2!/4+3
    *** Illegal character !
    >

    Notes and Hints:
    Since you want to loop indefinitely (until you enter <CTRL-C>), you have to set up an infinite loop:
    do
    {

    /* Body of loop */

    } while( 1 );

    To test for end-of-line, you have to verify if the character read is equal to '\n'.

    If you've encountered an error, before redisplaying the prompt, you need to clean the input line. This can be accomplished with the following code:
    while( (c = getchar()) != '\n' ) ;

    You want to do this ONLY IF THERE WAS AN ILLEGAL CHARACTER

    **************************************************
    And now here is my code thus far:
    **************************************************
    /* This programevaluates simple arithmetic expressions which
    are composed by positive numbers (real or integers) and
    the four basic arithmetic operators: +, -, * and /. */

    #include <stdio.h>

    int main()
    {
    float Answer, integer1;
    char c;

    printf("Please Enter the Mathematical Expression you want to
    be evaluated:\n" );
    scanf("%d", &Answer);
    c = getchar();

    do
    {

    switch ( c ) { /* switch nested in while */

    case ' ': /* Read over white space */
    scanf("%d",&integer1);
    Answer = Answer;
    break;

    case '+': /* Operand was + */
    scanf("%d",&integer1);
    Answer = Answer + integer1;
    break;

    case '-': /* Operand was - */
    scanf("%d",&integer1);
    Answer = Answer - integer1;
    break;

    case '/': /* Operand was */
    scanf("%d",&integer1);
    Answer = Answer / integer1;
    break;

    case '*': /* Operand was */
    scanf("%d",&integer1);
    Answer = Answer * integer1;
    break;

    default: /* Operand was an invalid character */
    while( (c = getchar()) != '\n' );
    printf("*** Illegal character!\n");
    break;


    } while(1);

    printf("%d",Answer);

    return 0;

    }

    *************************************************
    Now the Major problem I'm getting: On compiling I'm getting an error that says "fatal error C1004: unexpected end of file found". I think its bcasue I have an indefinite loop, but according to the assingnment arent I supposed to have an indefinite loop?


    Thanx in advance, I really really appreciate any help.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    "fatal error C1004: unexpected end of file found".
    Your missing a '}'

    Add a closing brace to your switch.
    And use CODE TAGS makes it easier for us to read!!!!
    Last edited by C_Coder; 03-27-2002 at 04:10 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM