Hey everyone, I am a beginner learning C++ and recently made a project by myself without having to look up anything on the Internet. It really sucks compared to everything else I see in here, but I am proud of it and am looking to improve. Please post any suggestions you have to improve/simplify my code and point out any blatant mistakes I may have made.
It's called "Troll Calc" and it's basically a in-console calculator that won't give you an answer if it's an easy problem. I haven't added the "troll" part to the subtraction and division classes yet but I'll be getting to it soon.
[url]http://pastebin.com/aqT4DUvb[/url]
Thanks in advance! :D
Edit: I was playing around with it and realized that when you type in anything but an integer for the two numbers, it keeps repeating itself. Anyone know how I can fix this?
I'm not a C++ expert but I believe you can do something like this.
You need to check whether the input was valid or not, that's the !(cin >> number)
[code]
cout << "Enter an Integer: ";
int number = 0;
while(!(cin >> number))
{
cin.sync(); //You might want to google cin.sync and .clear, not 100% sure if this is correct.
cin.clear(); //I think this clears the input and error flags so you don't get the infinite loop.
cout << "Bad input, Enter an INTEGER: ";
}
[/code]
[editline]2nd February 2014[/editline]
Basically if the cast to an integer(in this case the number variable) fails, it will run the while loop, clear the errors and try again.
[CODE]#include <iostream>
#include <limits>
using namespace std;
int main()
{
int x = 0;
while ((cout << "Enter your first number: ") && !(cin >> x))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Yo mama did not teach you what an INTEGER is?\n";
}
cout << x << '\n';
return 0;
}[/CODE]
[url]http://en.cppreference.com/w/cpp/io/basic_ios/clear[/url]
[url]http://en.cppreference.com/w/cpp/io/basic_istream/ignore[/url]
Thanks guys! I appreciate the help.
Sorry, you need to Log In to post a reply to this thread.