function - C++ Infinite Loop causing a stack overflow error? -


okay, i've been working on calculator. trying tell difference between valid integer , character. easy workaround 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 enter in character goes infinite loop saying: symbolhere:number 1:number 2:

ff(); calling in calculation function.

i wondering how fix , prevent stack overflow/ infinite loop? pastebin link: http://pastebin.com/gxn2ujaq

edit: ok, there number of things code.

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

will absolutely nothing, program increment fast undetectable human mind. recommend removing entirely.

if (snum >= 0 || fnum >= 0)                 {     cout << ("error: invalid number!") << endl;     wait = 0;     while (wait <= 5)     {        wait++;     }     system("cls");     calc();     } 

why sending error message if these numbers valid? unless adding negative numbers, should have different range.

your function calls never resolve main, instead call each other (ff , calc) infinity, program has many flaws , bad programming practices. drop whichever tutorial/book have , try finding more date list(sorry being harsh, has said).

c++ primer plus

cplusplus.com

thenewboston(recommended)


you have input fail entering char int, need make sure catch thrown cin , clear 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 include <limits> in file.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -