# Do { While Loop Problem - Note: Begginer Programmer

• 08-10-2002
Unregistered
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 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;
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);
}}
• 08-10-2002
SilentStrike
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.
• 08-10-2002
ProgrammingNoob
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!!
• 08-10-2002
ProgrammingNoob
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); }}```
• 08-10-2002
moonwalker
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.
• 08-11-2002
newbie_grg
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

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;
}
• 08-11-2002
ProgrammingNoob
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!!!
• 08-11-2002
ProgrammingNoob
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?