Do { While Loop Problem - Note: Begginer Programmer

This is a discussion on Do { While Loop Problem - Note: Begginer Programmer within the C++ Programming forums, part of the General Programming Boards category; - I have written a basic calculator program and it came to the point where I wanted to create a ...

  1. #1
    Unregistered
    Guest

    Question Do { While Loop Problem - Note: Begginer Programmer

    - I have written a basic calculator program and it came to the point where I wanted to create a loop. I put the loop before the menu area and the while statement right after the cases. When I compile I am getting one error - "Parse Error Before }." Can anyone help?

    #include <iostream.h>
    #include <ctype.h>
    #include <stdlib.h>

    float c;

    //function declarators//
    float add(float, float);
    float sub(float, float);
    float mul(float, float);
    float div(float, float);

    //function definitions//
    //Here is says the following
    //'c' undeclared (first use this function)
    float add(float a, float b) {

    c = a + b;
    cout << "Sum = >" << c;
    return(c);
    }

    float sub(float a, float b) {

    c = a - b;
    cout << "Difference = >" << c;
    return(c);
    }

    float mul(float a, float b) {

    c = a * b;
    cout << "Product = >" << c;
    return(c);
    }

    float div(float a, float b) {

    c = a / b;
    cout << "Qoutient = >" << c;
    return(c);
    }
    main() {
    float a, b; //global variables//
    char choice;

    cout << "\t Simple Calculator - Ver. 1.0 02-03 EDT." << endl;
    cout << "\t Author: -[ProgramingNoob03@yahoo.com]-" << endl << endl;
    do {
    cout << " Select Option [1 - 5]" << endl;
    cout << " [1] Addition" << endl;
    cout << " [2] Subtraction" << endl;
    cout << " [3] Multiply" << endl;
    cout << " [4] Divide" << endl;
    cout << " [5] Exit" << endl;
    cout << " Enter Selection Num. >";

    cin >> choice;

    cout << endl;

    //switch statements//
    switch (choice) {

    case '1' : {
    system ("PAUSE");
    cout << endl;
    cout << " 2 Digits, Enter ->";
    cin >> a >> b; cout << endl;
    cout << add(a, b);
    break; }

    case '2' : {
    system ("PAUSE");
    cout << endl;
    cout << " 2 Digits, Enter ->";
    cin >> a >> b; cout << endl;
    cout << sub(a, b);
    break; }


    case '3' : {
    system ("PAUSE");
    cout << endl;
    cout << " 2 Digits, Enter ->";
    cin >> a >> b; cout << endl;
    cout << mul(a, b);
    break; }

    case '4' : {
    system ("PAUSE");
    cout << endl;
    cout << " 2 Digits, Enter ->";
    cin >> a >> b; cout << endl;
    cout << div(a, b);
    break; }

    case '5' : {
    cout << endl;
    cout << " Selection 5: (Exit);" << endl;
    cout << " Simple Calculator Ver 1.0" << endl;
    cout << " Developed By: -[ProgramingNoob03@yahoo.com]-" << endl;
    system ("PAUSE");
    exit(0);
    break; }

    default : { cout << "Invalid Selection\n"; main(); }
    } while ((choice != '0') && (choice != '6'));
    return(0);
    }}

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Repost with code (IE surround the code with [ c o d e ] and [ / c o d e ] without the spaces) tags so the indentation is preserved. Likely, you are missing a } somewhere.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    I am the one who posted this thread - please ignore the comment lines in the code that say:
    //Here is says that
    //'c' is undeclared
    I believe these comment lines are found in the beggining right before the function definitions. - Just ignore those comments I had fixed that problem - I just want to know what is wrong with the do/while loop TY!!

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    here are the preserved indentations.
    Code:
     
    #include <iostream.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    float c;
    
             //function declarators//
             float add(float, float);
             float sub(float, float);
             float mul(float, float);
             float div(float, float);
    
        //function definitions//
        //Here is says the following
        //'c' undeclared (first use this function)
        float add(float a, float b) {
    
            c = a + b;
            cout << "Sum = >" << c;
            return(c);
                  }
    
         float sub(float a, float b) {
    
            c = a - b;
            cout << "Difference = >" << c;
            return(c);
                  }
    
         float mul(float a, float b) {
    
            c = a * b;
            cout << "Product = >" << c;
            return(c);
                   }
    
         float div(float a, float b) {
    
            c = a / b;
            cout << "Qoutient = >" << c;
            return(c);
                   }
    main() {
    float a, b; //global variables//
    char choice;
    
    cout << "\t Simple Calculator - Ver. 1.0 02-03 EDT." << endl;
    cout << "\t Author: -[ProgramingNoob03@yahoo.com]-" << endl << endl;
    do {
    cout << " Select Option [1 - 5]" << endl;
    cout << " [1] Addition" << endl;
    cout << " [2] Subtraction" << endl;
    cout << " [3] Multiply" << endl;
    cout << " [4] Divide" << endl;
    cout << " [5] Exit" << endl;
    cout << "  Enter Selection Num. >";
    
         cin >> choice;
    
             cout << endl;
    
    //switch statements//
    switch (choice) {
    
           case '1' : {
                system ("PAUSE");
                cout << endl;
                cout << " 2 Digits, Enter ->";
                     cin >> a >> b; cout << endl;
                     cout << add(a, b);
                     break; }
    
            case '2' : {
                system ("PAUSE");
                cout << endl;
                cout << " 2 Digits, Enter ->";
                     cin >> a >> b; cout << endl;
                     cout << sub(a, b);
                     break; }
    
    
            case '3' : {
                system ("PAUSE");
                cout << endl;
                cout << " 2 Digits, Enter ->";
                     cin >> a >> b; cout << endl;
                     cout << mul(a, b);
                     break; }
    
             case '4' : {
                system ("PAUSE");
                cout << endl;
                cout << " 2 Digits, Enter ->";
                     cin >> a >> b; cout << endl;
                     cout << div(a, b);
                     break; }
    
             case '5' : {
                 cout << endl;
                 cout << " Selection 5: (Exit);" << endl;
                 cout << " Simple Calculator Ver 1.0" << endl;
                 cout << " Developed By: -[ProgramingNoob03@yahoo.com]-" << endl;
                 system ("PAUSE");
                 exit(0);
                 break; }
    
             default : { cout << "Invalid Selection\n"; main(); }
    } while ((choice != '0') && (choice != '6'));
    return(0);
    }}

  5. #5
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    hmm

    default : { cout << "Invalid Selection\n"; main(); }

    you can't call main from main ... there are other errors too..
    the "}" closing switch() is misplaced..
    the last few lines should look like this:

    Code:
             default : { cout << "Invalid Selection\n"; main(); }
    	}
    } while ((choice != '0') && (choice != '6'));
    return(0);
    }
    That should fix almost everything.

    I dont know how to fix the main() though
    Try putting the menu in a function and call that function from main.
    Last edited by moonwalker; 08-10-2002 at 10:53 PM.

  6. #6
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ok

    //this should work

    #include <iostream.h>
    #include <ctype.h>
    #include <stdlib.h>

    struct calc
    {

    char choice ;

    } obj ;


    float c;

    float add ( float , float ) ;

    float sub ( float , float ) ;

    float mul ( float , float ) ;

    float div ( float , float ) ;




    float add ( float a , float b )
    {
    c = a + b ;

    cout << "Sum = >" << c ;

    return 0;
    }

    float sub ( float a , float b )
    {

    c = a - b;

    cout << "Difference = >" << c ;

    return 0 ;

    }

    float mul ( float a , float b )
    {

    c = a * b;

    cout << "Product = >" << c ;

    return 0 ;

    }


    float div ( float a , float b )

    {

    c = a / b;

    cout << "Qoutient = >" << c ;

    return 0 ;

    }


    int f() ;

    int main()

    {

    //float a , b ; //global variables// these aren't called global
    // variable. these are called local variables
    //no need!!

    cout << "\t Simple Calculator - Ver. 1.0 02-03 EDT." << endl ;

    cout << "\t Author: -[ProgramingNoob03@yahoo.com]-" << endl ;

    do

    { f() ; } //i've called a function which should hopefully increase the
    //readability of the code.

    while ( obj.choice != 0 && obj.choice != 6 );

    //your mistake was hiding in "while"
    // you are trying to access the "choice" which is out of scope for do-while
    //whatever you do , you cant run this code without bug.i've modified this
    // code a lil bit which should work fine.

    return 0;
    }


    int f()
    {

    float a , b ;

    cout << " \nSelect Option [1 - 5] " << endl ;

    cout << " [1] Addition" << endl;

    cout << " [2] Subtraction" << endl;

    cout << " [3] Multiply" << endl;

    cout << " [4] Divide" << endl;

    cout << " [5] Exit" << endl;

    cout << " Enter Selection Num. >";

    cin >> obj.choice;

    cout<<endl;

    switch (obj.choice) {

    case '1' : {

    system ( "PAUSE" ) ;//i dont understand why you've called this
    // function!!!oh gawd!!
    cout << endl ;

    cout << " 2 Digits, Enter -> " ;

    cin >> a >> b ;

    cout << endl ;

    add( a , b ) ;

    break;

    }

    case '2' :{

    system ( " PAUSE " ) ;

    cout << endl ;

    cout << " 2 Digits, Enter ->" ;

    cin >> a >> b ;

    cout << endl ;

    cout << sub( a , b ) ;

    break ;

    }


    case '3' : {

    system ( " PAUSE " ) ;

    cout << endl ;

    cout << " 2 Digits, Enter ->" ;

    cin >> a >> b ;

    cout << endl ;

    cout << mul ( a, b ) ;

    break ;

    }

    case '4' : {

    system ("PAUSE") ;

    cout << endl ;

    cout << " 2 Digits, Enter ->" ;

    cin >> a >> b ;

    cout << endl ;

    cout << div ( a , b );

    break ;

    }

    case '5' :{

    cout << endl ;

    cout << " Selection 5: (Exit);" << endl ;

    cout << " Simple Calculator Ver 1.0" << endl ;

    cout << " Developed By: -[ProgramingNoob03@yahoo.com]-" << endl ;

    system ( "PAUSE" ) ;

    exit (0) ;

    break ;

    }

    default : {

    cout << "Invalid Selection\n" ;
    break ;

    }


    }


    return 0;
    }
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    Thank you very much for the help - all though I haven't reached to point of using pointers yet, I am sorry for this - and I will remove the system("PAUSE") function as soon as possible. TY for the help!!!

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    4
    What does the { f(); } function do - I don't see a prototype for it, so I assume it is included in one of the headers. What do you mean by increasing the readability of the code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 07:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 11:47 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. Replies: 12
    Last Post: 06-08-2005, 12:23 PM
  5. Problem accessing 3d array in loop...
    By weirdbeardmt in forum C Programming
    Replies: 2
    Last Post: 06-16-2004, 12:43 PM

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