Labels are just overs simplified loops and are very useful if programming in very low level languages like any assembly language (MIPS). However that doesn't mean we can't make use of them in c++.
Like this:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void Welcome()
{
cout << "\nWelcome to Vinyl Scratch's MathFinder!\n";
cout << "It's my first C++ program, so I wanted to \nsee "<<
"if you guys liked it, and if I did well.\n\n\n";
}
int main()
{
unsigned long Width, Height, Base, Width2, Length2;
Welcome();
enum mathType {Area, Volume, Perimeter};
mathType Math;
int x;
start://label
cout << "What math type would you like?(0-2) Area is 0, Volume is 1, and Perimeter is 2.\r\n";
cin >> x;
Math = mathType(x);
if (Math == Area)
{
if (!cin.good())
{
cout << "Invalid Character, please input an integer.\r\n";
return 1;
}
cout << "Enter Width then Length:" << "\r\n";
cin >> Width;
cin >> Height;
cout << "Area is: " << Width * Height << endl;
}
else if (Math == Volume)
{
if (!cin.good())
{
cout << "Invalid Character, please input an integer.\r\n";
return 1;
}
cout << "Enter Width, then Height, then Base:\r\n";
cin >> Width;
cin >> Height;
cin >> Base;
cout << "Volume is: " << Width * Height * Base << endl;
}
else if (Math == Perimeter)
{
if (!cin.good())
{
cout << "Invalid Character, please input an integer.\r\n";
return 1;
}
cout << "Enter Width, then Length.\r\n";
cin >> Width;
cin >> Height;
Width2 = Width * 2;
Length2 = Height * 2;
cout << "Perimeter is: " << Width2 + Length2 << endl;
}
else
goto start; //go to label
return 0;
}
OR THIS:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
enum mathType {Area, Volume, Perimeter};
void Welcome();//
void Operation(mathType );//function headers
int main()
{
Welcome();
mathType Math;
int x;
DoSomeMath://label
cout << "What math type would you like?(0-2) \n"<<
"Area is 0, Volume is 1, and Perimeter is 2.\n";
cin >> x;
if (x < 0 || x > 2)
goto DoSomeMath;
Math = mathType(x);
Operation(Math);
return 0;
}
void Welcome()
{
cout << "\nWelcome to Vinyl Scratch's MathFinder!\n";
cout << "It's my first C++ program, so I wanted to \nsee "<<
"if you guys liked it, and if I did well.\n\n\n";
}
void Operation(mathType MATH)
{
unsigned long Width, Height, Base, Width2, Length2;
if (MATH == Area)
{
if (!cin.good())
{
cout << "Invalid Character, please input an integer.\n";
return;
}
cout << "Enter Width then Length:" << "\n";
cin >> Width;
cin >> Height;
cout << "Area is: " << Width * Height << endl;
}
else if (MATH == Volume)
{
if (!cin.good())
{
cout << "Invalid Character, please input an integer.\r\n";
return;
}
cout << "Enter Width, then Height, then Base:\r\n";
cin >> Width;
cin >> Height;
cin >> Base;
cout << "Volume is: " << Width * Height * Base << endl;
}
else if (MATH == Perimeter)
{
if (!cin.good())
{
cout << "Invalid Character, please input an integer.\r\n";
return;
}
cout << "Enter Width, then Length.\r\n";
cin >> Width;
cin >> Height;
Width2 = Width * 2;
Length2 = Height * 2;
cout << "Perimeter is: " << Width2 + Length2 << endl;
}
}
Someone did message me though and said that using labels isn't such a swell idea and I see why now. Labels just aren't as solid as using for/while loops. So if you want, you can go ahead and implements some while loops.
gl
while loop:
#include <iostream>
#include <sstream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::stringstream;
enum mathType {Area, Volume, Perimeter};
mathType Getx ();
void Welcome();
void Operation(mathType );
int main()
{
Welcome();
Operation(Getx());
return 0;
}
mathType Getx()
{
int x;
string input = "";
while (1)
{
cout << "What math type would you like?(0-2) \n"<<
"Area is 0, Volume is 1, and Perimeter is 2.\n";
getline(cin, input);
stringstream CatchBad(input);
if ((CatchBad >> x))
{
if (x < 0 || x > 2)
continue;
else
break;
}
else
cout << "\nInvalid Character, please input an integer.\n";
}
return mathType(x);
}
void Welcome()
{
cout << "\nWelcome to Vinyl Scratch's MathFinder!\n";
cout << "It's my first C++ program, so I wanted to \nsee "<<
"if you guys liked it, and if I did well.\n\n\n";
}
void Operation(mathType MATH)
{
unsigned long Width, Height, Base, Width2, Length2;
if (MATH == Area)
{
cout << "Enter Width then Length:" << "\n";
cin >> Width;
cin >> Height;
cout << "Area is: " << Width * Height << endl;
}
else if (MATH == Volume)
{
cout << "Enter Width, then Height, then Base:\n";
cin >> Width;
cin >> Height;
cin >> Base;
cout << "Volume is: " << Width * Height * Base << endl;
}
else if (MATH == Perimeter)
{
cout << "Enter Width, then Length.\n";
cin >> Width;
cin >> Height;
Width2 = Width * 2;
Length2 = Height * 2;
cout << "Perimeter is: " << Width2 + Length2 << endl;
}
}