1

Okay, so I've been working on my calculator. I am currently trying to get it to tell the difference between a valid integer and a character. As an easy workaround I did:

int calc()
{
cout << "Number 1:"; cin >> fnum;
cout << "Number 2:"; cin >> snum;
    if (snum <= -1000 || fnum <= -1000)
    {
        cout << ("Error: Invalid Number!") << endl;
        calc();
    }
    else
    {
        ff();
    }
return 0;
}

And whenever I enter in a character it goes into an infinite loop saying: SymbolHere:Number 1:Number 2:

ff(); is calling in the calculation function.

I was wondering how to fix this and prevent a stack overflow/ infinite loop? Pastebin Link: http://pastebin.com/GxN2uJAQ

Chris Altig
  • 680
  • 3
  • 8
  • 22

1 Answers1

0

EDIT: ok, there are a number of things with this code.

wait = 0;
while (wait <= 5)
{
   wait++;
}

will do absolutely nothing, your program will increment so fast this is undetectable to the human mind. I would recommend removing this entirely.

if (snum >= 0 || fnum >= 0)
                {
    cout << ("Error: Invalid Number!") << endl;
    wait = 0;
    while (wait <= 5)
    {
       wait++;
    }
    system("CLS");
    calc();
    }

why are you sending an error message if these numbers are valid? Unless you are only adding negative numbers, this should have a different range.

your function calls also never resolve back into main, instead they call each other (ff and calc) infinity, the program honestly has too many flaws and bad programming practices. Drop whichever tutorial/book you have and try finding a more up to date list(sorry for being harsh, but it has to be said).

C++ Primer Plus

CPlusPlus.com

TheNewBoston(Recommended)


You probably have an input fail by entering say a char for an int, you need to make sure to catch anything thrown by cin and clear the state:

std::string err = "error!";
try {
    std::cin >> x;
    if(!cin)
        throw err;
    //....
}
catch(std::string& ee)
{
    std::cout << ee << std::endl;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
}

make sure to include <limits> in your file.

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
  • The whole point of that part of the code was to check if it was a number or not. Because let's say... LOL is neither >= -1000 because it is not a number so it should fail the test. So it does. But when it goes back it continues the loop without even asking the user's input. I attempted to counter this by setting both variables to zero before an input was requested. But it still went into a loop. (I am also very new to C++ hence why I made a simple calculator) If you need to see all of the code you can find it here: http://pastebin.com/GxN2uJAQ – Chris Altig May 21 '13 at 03:59
  • You need to post your loop then, not just it's function. – Syntactic Fructose May 21 '13 at 04:07
  • 1
    @user2396111 I added an edit in. It's not pretty, but hopefully you'll take my advice. – Syntactic Fructose May 21 '13 at 04:25
  • The wait function was an experiment. Most of what I learned I am teaching myself. I read a few chapters of a book and decided to give it a shot. – Chris Altig May 21 '13 at 04:35
  • Okay, so I had a friend go over it and he cleared ALOT of things up. Just by moving things to different places and ending the main(); function early! – Chris Altig May 21 '13 at 05:02