I am currently working on a program that takes one measurement of a circle and then gives the rest although i came across a problem while trying to make it loop back if you input an invalid number and my compiler keeps saying i have an error with were my while is placed ( I highlighted the important area in red!) thanks in advance you guys are the best!

``````#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;

double circumfrence;
double area;
double diameter;
int circumfrencefunction();
int areafunction();
int diameterfunction();

int main()
{
int choice;
do{
cout << "I can find measurments on a circle!" << endl;
cout << "which measurment do you have for me?" << endl;
cout << "1.) radius" << endl;
cout << "2.) circumfrence" << endl;
cout << "3.) area" << endl;
cout << "4.) diameter" << endl;
cin >> choice;
if (choice == 1){
}
else{
if (choice == 2){
circumfrencefunction();
}
else{
if (choice == 3){
areafunction();
}
else{
if (choice == 4){
diameterfunction();
}
else{
system("cls");
}while (choice != 1, 2, 3, 4);
}
}
}
}
return 0;
}``````

:icon_confused:

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Fbody

Yeah, you can't do that with the while loop.

``while(choice != A && choice != B && choice != C ... etc)``

OR you can write it as

``````while( choice < 1 || choice > 4)
// basically read as, while choice is less than 1 or greater than 4, do this again
// Meaning, not 1 or 2 or 3 or 4``````

Edited by Saith: n/a

Your code's current formatting leaves something to be desired, it's ineffective and inconsistent, so you've made a common rookie mistake. You've mis-matched your braces. Here is your code reformatted for better readability:

``````#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;

double circumfrence;
double area;
double diameter;
int circumfrencefunction();
int areafunction();
int diameterfunction();

int main() {                              //begin main()
int choice;
do {                                    //begin do-loop
cout << "I can find measurments on a circle!" << endl;
cout << "which measurment do you have for me?" << endl;
cout << "1.) radius" << endl;
cout << "2.) circumfrence" << endl;
cout << "3.) area" << endl;
cout << "4.) diameter" << endl;
cin >> choice;
if (choice == 1) {                    //begin if 1
} else {
if (choice == 2) {                  //begin if 2
circumfrencefunction();
} else {
if (choice == 3) {                //begin if 3
areafunction();
} else {
if (choice == 4) {              //begin if 4
diameterfunction();
} else {
system("cls");
}while (choice != 1, 2, 3, 4);  //end if 4, do-loop does not end here
}                                 //end if 3
}                                   //end if 2
}                                     //end if 1
}                                       //do-loop actually ends here...
return 0;
}                                         //end main()``````

Notice which brace your while is after. What block does that brace close? It closes the else that you open on Line 36, which in turn closes the if you opened on Line 34, not your do that you open on Line 17. That block ends with the brace on Line 42.

Edited by Fbody: n/a

thanks guys I probably never would have figured that out myself thanks!

Next time you have a series of if statements like this you should consider replacing them with a series that uses the else-if construct. This will keep the nesting from getting too deep and out of control and help keep you from losing track of exactly how deep you currently are.

Here's your code with an "else if" construct instead (reformatted only, syntax error not corrected):

``````#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;

double circumfrence;
double area;
double diameter;
int circumfrencefunction();
int areafunction();
int diameterfunction();

int main() {                              //begin main()
int choice;
do {                                    //begin do-loop
cout << "I can find measurments on a circle!" << endl;
cout << "which measurment do you have for me?" << endl;
cout << "1.) radius" << endl;
cout << "2.) circumfrence" << endl;
cout << "3.) area" << endl;
cout << "4.) diameter" << endl;
cin >> choice;
if (choice == 1) {                    //begin if 1
} else if (choice == 2) {             //end if 1, begin if 2
circumfrencefunction();
} else if (choice == 3) {             //end if 2, begin if 3
areafunction();
} else if (choice == 4) {             //end if 3, begin if 4
diameterfunction();
} else {
system("cls");
}                                     //end if 4
} while (choice != 1, 2, 3, 4);         //end do-loop
return 0;
}                                         //end main()``````

See how much simpler that is? The level of nesting has been cut down from 6 to 4, but the code still behaves the same.

Edited by Fbody: n/a