Title probably sounds confusing so first I'll show you my code, I made this simple program to get two input values and multiply them, and another thing, but that's not important, It works correctly:
#include <iostream>
using namespace std;
main()
{
int a,b,c,d,e;
char j = 4;
cout << "Welcome to Momentum Calculator\n\n";
cout << "------------------------------\n";
cout << "Please Enter Mass in KG (if the mass in in grams, put \"9999\" and hit enter): \n\n";
cin >> a;
if (a==9999) {
cout << "\nPlease Enter Mass in grams: \n\n";
cin >> d;
}
else {
d = 0;
}
cout << "\nPlease Enter Velocity \n\n";
cin >> e;
if (d == 0)
{
c = (a*e);
}
else {
c = (e*d)/100;
}
cout << "\nMomentum = " << c;
cin.get();
cin.ignore();
while (j == 4)
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
main();
}
}
Now as you can see, my variable is an int (integer) and my problem is If I enter an English letter (a-z) or anything that is not a number will cause it to repeat my program unlimited times at an unlimited speed. I want a string/char to see if my var "a" is a letter or anything but don't know how to. I can do it, however, I want user to input only one time in "a" and mine makes him to enter again. Please Help :)
mainis wrong (you probably should add somedo...while... outer loop), and you should declare itint main(int argc, char**argv).... Also, compile with all warnings and debugging information (e.g.g++ -Wall -g) and learn to use the debugger (e.g.gdb)mainneedsintas its return type (why bother with the parameters if you don't use them). Putting no return type is not legal. Usingmainin the program is also not legal. In addition to-Wall, I suggest-Wextra -pedantic-errors. I also suggest declaring variables as close to first use as you can.main()recursively in your program.int main {...}where you havemain() {...}. The other place where you callmainis not valid C++ becausemaincannot be used in a program. The compiler should stop you. If it doesn't, you need a higher warning level or a newer compiler. If you need to loop all ofmain, you can always add a loop to do everything again.