I have been doing c++ for about 10 days now and I made this little calculator
Sorry, but I think there are alot of spelling mistakes, im from Norway and im only like 14 so sorry :3
Have some error errors (o.O) hope one of you can help me fix it :)!
Anyways:
while(1) {
//code goes in hear
//if you want to skip to the next loop part if you detect an error do this:
continue;
//otherwise just use
break;
//to exit the loop if everything went ok.
}
The other thing you could do is put this line before the code you want to jump back to like this:
1 2 3
int main() {
label_name_here:
... //rest of code
then when you want to go back there, like on an error detection, just do this:
The error seems to be when adding numbers with decimals (i.e. 1.1 +1), something to do with adding of the float type numbers. If I'm correct, removing the cout.precision(100); will fix the problem
I am not getting any debugging errors, but the errors act's strange. Sometimes they make an everlasting loop, and other times they don't output anything. I have nooo idea whats going on (running win7 and using msVisual2010express ass IDE)
And by the main(), thanks for telling me this, loops would be a much smarter solution. But why is calling the main() function so bad? (just wondering)
Calling main() is bad because the C++ standard (the document that specifies everything about C++) says so. §3.6.1/3
The function main shall not be used within a program.
1 2 3 4
if(!(cin>>a||cin>>methode||cin>>b)) {
cout << "Error: You have not used the rigth format"<<endl;
int main();
}
This will try to read a, methode and b again. You probably want to put this where you have line 15-17. If you want it to print the error message when at least one of the inputs failed you could change || to &&. You can chain many >> after each other so a better way is probably to write if(!(cin >> a >> methode >> b)) {.
If you want to be able to read new input after an input operation has failed you will have to clear the error flags by doing cin.clear(); and also get rid of the invalid input. You can use the ignore function to do that.
1 2
// Ignores one character.
cin.ignore();
1 2
// Ignores the whole line.
cin.ignore(numeric_limits<streamsize>::max(), '\n');