Inputs

i am new to C++ and need to finish an assignment. i used the If-Else to define a range of user inputs but can not seem to figure out how to ignore a wrong input and reprint the cout giving chance for another trial.
How do i go about this please
If the condition is simple enough, use do... while.
Otherwise, use for(;;) and break when you're satisfied with the input or continue only when you are dissatisfied and break otherwise, whichever amounts to less code.
#include <iostream>
using namespace std;
#include <string>

int main()
{
int length_side;
int shape;
int work;
string d;
cout<<"AREA AND VOLUME CALCULATOR
cout<<"Is the shape 2dimensional or 3dimensional:\t";
cin>> d;

if ((d =="2d") || (d == "2D") || (d == "2") || (d == "2dimensional"))
{



}
else if ((d == "3d") || (d == "3D") || (d == "3") ||(d == "3dimensional"))
{


}



How do i then reprint cout<<"Is the shape 2dimensional or 3dimensional:\t";
for it to give another trial if another value of d is wrongly put
1
2
3
4
5
6
7
8
9
10
11
12
13
for (;;)
{
  cout<<"Is the shape 2dimensional or 3dimensional:\t";
  cin>> d;
  if (d=="2d" || d=="2D" || d=="2" || d=="2dimensional")
  {
  }
  else if (d=="3d" || d=="3D" || d=="3" || d=="3dimensional")
  {
  }
  else continue;
  break;
}


Or:

1
2
3
4
5
6
7
8
9
10
11
12
13
for (;;)
{
  cout<<"Is the shape 2dimensional or 3dimensional:\t";
  cin>> d;
  if (d=="2d" || d=="2D" || d=="2" || d=="2dimensional")
  {
    break;
  }
  else if (d=="3d" || d=="3D" || d=="3" || d=="3dimensional")
  {
    break;
  }
}


If there are just two different cases you want to handle, I'd favor the second one.
I also took the freedom to remove all these unnecessary braces, they just make the code hard to read.
thank you so so much.....finally happy
Okay. Well. Pseudo-endless loops are fine if they're short. But in the above case these loops are huge and you'll have to go hunting for any continues and breaks first to understand it.

First: you have a semicolon at the end of the following line, which reads as: if the condition is true, do nothing.
if (shape == 1 || shape == 2 || shape == 3 || shape == 4 || shape == 5 || shape == 6 || shape == 7);

Second, you can write that as:
if (shape>=1 && shape<=7)

Third, it's almost always better to avoid unnecessary nested blocks:
1
2
if (shape<1 || shape>7)continue;
//no block necessary for the case that a valid shape number was entered 


Next up is breaking up this spaghetti code into several smaller functions: one just for 2D shapes and one for 3D shapes.
Both of these can call smaller functions that handle the selection and input and do the actual calculations.

As for why the screen "goes crazy"... this prints the line an infinite number of times:
1
2
for(;;)
cout<< "Do u want to get the area or the Perimeter:\n" << "1: Area\n" << "2: Perimeter\n\n" << "Corresponding number:\t";


You probably wanted to create a block here.
Also read the following if necessary:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
Topic archived. No new replies allowed.