0

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 :)

18
  • 3
    Recursive call of main is wrong (you probably should add some do...while... outer loop), and you should declare it int 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) Commented Sep 11, 2013 at 13:12
  • 1
    main needs int as its return type (why bother with the parameters if you don't use them). Putting no return type is not legal. Using main in 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. Commented Sep 11, 2013 at 13:17
  • 1
    As a first step remove the while loop completely. It has two problems: 1. it is an infinite loop (j will never be anything else than 4) 2. it is illegal to call main() recursively in your program. Commented Sep 11, 2013 at 13:18
  • 1
    If you really don't know about such a simple thing as loops, yet, then you really need to spend more time with a basic C++ tutorial and stick to things which are simpler and that you do know. If you haven't yet put in enough effort to learn the basics, it's not fair to expect people on the internet to spoon feed you. Commented Sep 11, 2013 at 13:34
  • 1
    @Yariz, You need int main {...} where you have main() {...}. The other place where you call main is not valid C++ because main cannot 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 of main, you can always add a loop to do everything again. Commented Sep 11, 2013 at 13:39

3 Answers 3

1

There is a function called isalpha in ctype library, checks whether your variable is an alphabetic letter so you can do using isalpha function.

Sign up to request clarification or add additional context in comments.

Comments

1

Will isdigit or isalpha from standard library help you?

P.S. 1KG contains 1000 grams, so you should divide by 1000, not by 100;

UPDATE: Seems I understood your question... You need cin.clear(); before cin.get() and cin.ignore(). Otherwise the these calls won't do anything, as cin is in an error state.

2 Comments

That's just an example. I'm trying REALLY hard to do it but isn't working :(
main answer was updated. Please, check if it answers your question.
0

I think you can get a as an String, and see if it contains English letter or not, if it contains, again ask for the input ( you can do it in a while loop ). And when a correct input entered, parse it and find what is it's number.

1 Comment

You probably mean a std::string .... the String type is not in standard C++ ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.