1

I feel like im doing something really silly wrong. I just want the program to tell the user when they are entering non-doubles, and continue to loop back to the cin where you enter a value.

I want the user to input any number. Then essential do this trivial math and repeat. Its working fine in that regard, the problem comes when some unexpected input like a char gets entered. Then the input somehow sends it into a loop where it loops the math problem, instead of just telling the user that they must type a number and looping back to cin type in a new number.

#include <iostream>
#include <cstdlib>

using std::cout; using std::cin; using std::endl;

long double domath(long double i)
{ 
     cout << i << "/" << 2 << "=" << i/2 << endl;    
     cout << i/2 << "*" << 10 << "=" << (i/2)*10 << endl << endl;   
     cout << 5 << "*" << i << "=" << 5*i << "\n\n";      
     return 0;
}

int main()
{

    long double in = 0;

    while(true)
    {
        cin >> in;         
        if (cin.fail()) {             
            in = char(int(in));  
        }           
        domath(in);
    }
    system("pause>nul");
    return 0;
}

2 Answers2

1

You don't clear the cin in case of fail, and it infinitely tries to parse wrong input to double, failing every time. You need to clear the buffer in case of error:

if (cin.fail()) {
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    in = char(int(in));
}

Also, can't understand what you're trying to achieve with

in = char(int(in));

in is a long double variable and will hold the last value you assigned to it, no need to "convert" it to do math.

buld0zzr
  • 962
  • 5
  • 10
  • Why don't I have to clear it with a numerical input? –  Jul 08 '16 at 15:52
  • @x4kepa because it successfully consumes it when parsing – buld0zzr Jul 08 '16 at 15:52
  • Where do you learn stuff like this? Where does the specific knowledge of when to do stuff like this come from? –  Jul 08 '16 at 15:53
  • @x4kepa trial and error mostly, with heavy debugger and google usage – buld0zzr Jul 08 '16 at 15:54
  • @x4kepa it is primarily practice and then the experience – Khalil Khalaf Jul 08 '16 at 15:55
  • @x4kepa did it help? If it did, upvoting and accepting an answer is a stackoverflow way to say thanks – buld0zzr Jul 08 '16 at 16:05
  • By the way I was using `in = char(int(in));` to see if I could convert the char value into a number value so that it might pass right through as a number. –  Jul 08 '16 at 16:11
  • `in` is long double value, it's not a char, and doesn't store any chars from cin. If cin could convert the input into number, it would and store it in `in` variable – buld0zzr Jul 08 '16 at 16:13
  • It says 'numeric_limits' & 'streamsize' undeclared. –  Jul 08 '16 at 16:15
  • @x4kepa add `std` namespace, I've modified the answer – buld0zzr Jul 08 '16 at 16:17
  • 1
    Thanks, that was very helpful, ive got to learn how to use the debugger on Dev, or learn how to not suck at coding.. –  Jul 08 '16 at 16:21
  • _"Why don't I have to clear it with a numerical input?"_ You do. You just never had it set, so you didn't notice. You don't learn that through specific knowledge, but by thinking your solution through. :) – Lightness Races in Orbit Jul 08 '16 at 17:07
0

Couldn't you try doing something like this?

int x;

if(std::cin >> x)
  doSomethingCool(x);
else
  std::cout << "Error, not a valid integer!" << std::endl;

Exit your loop on bad input.

I think this just feels more natural/looks cleaner than clearing the buffer and all the other jazz. Just my opinion.

if (cin >> x) - Why can you use that condition?

edit: Bul's answer is still a good one though.

Community
  • 1
  • 1
HumbleWebDev
  • 555
  • 4
  • 20