I did have a look through previous threads about validation, but most of the posts went straight over my head. I'm making a CLI calc app in Linux, and it works fine, but there are a couple of queries:
1. My current program uses GOTOs to return to the options dialogue, so the user can enter another option to use a different operation. I have heard plenty about GOTOs being bad practice, but what I don't clearly know is why? All I know is they are decried on every corner of the globe. If I shouldn't use GOTOs, I assume a loop of some sort would do the job? I am not sure which to use or how to break out of it should the user wish to close the program.
2. I have no idea how to validate the input so that letters are rejected. I have been told a for loop of some kind is the way to do it, but I am still stumped as to how. I was also told that getting the program to check the ASCII values of the characters/digits passed through and only accept a certain range, but again, I am clueless as to how exactly.
If I could have some help on these issues, it would be much appreciated.
My program so far:
Code:
#include <iostream>
using namespace std;
int main(void)
{
float option,operation,num1,num2;
cout << "Welcome to Bal Jacques' calculator program. Select the operation you want from the list below, "
"by typing in the corresponding number:\n";
cout << "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. About\n6. Quit\n";
Start:
cout << "Option: ";
cin >> option;
/*int iCount = 0
for(iCount = 0; iCount < len(option); iCount++;)
{
}*/
if (option == 1)
{
cout << "This operation will add two numbers.\n";
cout << "Number 1: ";
cin >> num1;
cout << "Number 2: ";
cin >> num2;
operation = num1 + num2;
cout << "The answer is " << operation << "\n";
goto Start;
}
else if (option == 2)
{
cout << "This operation will subtract two numbers.\n";
cout << "Number 1: ";
cin >> num1;
cout << "Number 2: ";
cin >> num2;
operation = num1 - num2;
cout << "The answer is " << operation << "\n";
goto Start;
}
else if (option == 3)
{
cout << "This operation will multiply two numbers.\n";
cout << "Number 1: ";
cin >> num1;
cout << "Number 2: ";
cin >> num2;
operation = num1 * num2;
cout << "The answer is " << operation << "\n";
goto Start;
}
else if (option == 4)
{
cout << "This operation will divide two numbers.\n";
cout << "Number 1: ";
cin >> num1;
cout << "Number 2: ";
cin >> num2;
if (num2 == 0)
{
cout << "You cannot divide by zero.\n";
}
else
{
operation = num1 / num2;
cout << "The answer is " << operation << "\n";
}
goto Start;
}
else if (option == 5)
{
cout << "Calculator App version 1.6.1 by Bal Jacques.\n";
goto Start;
}
else if (option == 6)
{
cout << "Thank you for using this program, Goodbye.\n";
cin.ignore();
cin.get();
}
else
{
cout << "Enter a number from 1 to 6.\n";
goto Start;
}
}