code w/o using goto

I admit that I am not a very skilled programmer. I have not been doing C++ very long. I know that "goto" use is frowned upon. I would like to know if anyone can help me make this code do the same thing but without relying on "goto".
Please let me know, thanks!


#include <iostream>
using namespace std;

class rect
{
private:
double length, width, height;
public:
rect(double = 0, double = 0, double = 0);
void showVal();
void calArea();
void calVol();
};

rect::rect(double l, double w, double h)
{
length = l;
width = w;
height = h;
}

void rect::showVal()
{
cout << "These are the dimension you entered" << endl
<< "Length: " << length << "\nWidth: " << width
<< "\nHeight: " << height << endl << endl;
}

void rect::calVol()
{
cout << (length*width*height) << endl;
}

void rect::calArea()
{
cout << (length*width) << endl;
}

int main()
{
int l, w, h;
char answer;

start:
cout << "Enter the dimensions" << endl
<< "Length: ";
cin >> l;
cout << "Width: ";
cin >> w;
cout << "Height: ";
cin >> h;
cout << endl;
cout << endl;

rect rect(l, w, h);

rect.showVal();

tryagain:
cout << "Are these the correct dimensions (Y/N)?";
cin >> answer;
cout << endl;

if(answer == 'N' || answer == 'n')
goto start;
else if(answer == 'Y' || answer == 'y')
goto cont;
else
cout << "Invalid command entered, please try again\n\n"; goto tryagain;

cont:
cout << "The Area is: ";
rect.calArea();

cout << "\nThe Volume is: ";
rect.calVol();
cout << endl;

retry:
cout << "Would you like to calculate another (Y/N)?";
cin >> answer;

cout << endl;

if(answer == 'Y' || answer == 'y')
goto start;
else if(answer == 'N' || answer == 'n')
goto end;
else
cout << "Invalid command entered, please try again\n\n";
goto retry;

end:
system("pause");
return 0;
}
Last edited on
Hey, check the following code...Its working without using goto.... Test it. If there is any error, do follow up.


#include <iostream>
using namespace std;

class rect
{
private:
double length, width, height;
public:
rect(double = 0, double = 0, double = 0);
void showVal();
void calArea();
void calVol();
};

rect::rect(double l, double w, double h)
{
length = l;
width = w;
height = h;
}

void rect::showVal()
{
cout << "These are the dimension you entered" << endl
<< "Length: " << length << "\nWidth: " << width
<< "\nHeight: " << height << endl << endl;
}

void rect::calVol()
{
cout << (length*width*height) << endl;
}

void rect::calArea()
{
cout << (length*width) << endl;
}

int main()
{
int l, w, h;
char answer;

do
{
cout << "Enter the dimensions" << endl
<< "Length: ";
cin >> l;
cout << "Width: ";
cin >> w;
cout << "Height: ";
cin >> h;
cout << endl;
cout << endl;

rect rect(l, w, h);

rect.showVal();

while(1)
{
cout << "Are these the correct dimensions (Y/N)?";
cin >> answer;
cout << endl;
if (answer!='N' && answer != 'n' && answer != 'Y' && answer != 'y')
cout << "Invalid command entered, please try again\n\n";
else
break;
}

if(answer == 'N' || answer == 'n')
{
answer = 'Y';
continue;
}
else
{
cout << "The Area is: ";
rect.calArea();
cout << "\nThe Volume is: ";
rect.calVol();
cout << endl;
}

while(1)
{
cout << "Would you like to calculate another (Y/N)?";
cin >> answer;
cout << endl;
if (answer!='N' && answer != 'n' && answer != 'Y' && answer != 'y')
cout << "Invalid command entered, please try again\n\n";
else
break;
}

}while(answer == 'Y' || answer == 'y');

system("pause");
return 0;
}
@gtasmy

Hmm.

You shouldn't use goto statements. They're bad programming practice and the been deprecated in C++.
Last edited on
@Code Assassin
Maybe you should read the question?
... if anyone can help me make this code do the same thing but without relying on "goto".
In BASIC, goto is no problem. Although it still means spaghetti programming which is why its bad practice. But there is nothing wrong with it. IN BASIC.

HOWEVER, I didn't even know C++ could use the goto command.. LMAO

Shows how much I use it :P
@Caligulaminus

Ha. Must've slipped my mind. :)
Topic archived. No new replies allowed.