0

my program is a calculator that currently only does addition and subtraction, but when i input a large number it starts flashing and scrolling. it works fine for small numbers. the program isn't long so here it is. a youtube video of the problem https://youtu.be/Fa03WtgXoek

 #include <iostream>

 int GVFU()
{
std::cout <<"enter number";
int a;
std::cin >> a;
return a;
}

int add()
{
int x = GVFU();
int y = GVFU();
int z = x + y;
std::cout <<z <<std::endl;
return 0;
}

int subtract()
{
int x = GVFU();
int y = GVFU();
int z = x - y;
std::cout <<z << std::endl;
return 0;
}

int main()
{
for ( ; ; )
{

std::cout << "enter 1 for addition and 2 for subtraction";
int c;
std::cin >> c;
if (c==1)
{
add();
}
if (c==2)
{
    subtract();
}
std::cout << "press 1 to end";
int e;
std::cin >>e;
if (e==1)
{
    return 0;
}
}
}
Avalanche
  • 15
  • 4
  • 1
    Your input reading functions are failing and you're not checking for errors anywhere. – melpomene Aug 12 '17 at 15:08
  • You are probably accepting a char in place of integer thus entering an endless loop. More info on the subject [in this question](https://stackoverflow.com/questions/18400620/cin-for-an-int-inputing-a-char-causes-loop-that-is-supposed-to-check-input-to-go). – Ron Aug 12 '17 at 15:11

4 Answers4

1

If you try to read a value from cin and the value read doesn't match the expected format, it causes the stream to fail and all future read operations will instantly return without reading anything.

Independently, in C++ integer values for the int type have a minimum and maximum possible value that depends on what compiler and system you're using. If you exceed that value when entering a number, cin will consider it a failed read.

Putting this together, once you enter a value that's too large, the program will keep running through the main loop in your program, prompting for a value, instantly returning without actually getting user input, then calculating garbage values.

To fix this, you'll need to either (1) just hope the user doesn't type in anything unexpected or (2) get user input more robustly. There are a number of good explanations about how to do option (2) here on Stack Overflow, and now that you know what the root cause of the issue is you can hopefully get the code fixed and working!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1

Use

std::cout << std::numeric_limits<int>::max() << std::endl;

and include #include <limits> and you will find out max int value on your machine.

int on your system is likely a 32-bit signed two's complement number, which means the max value it can represent is 2,147,483,647. If you add bigger number stream will fail and all next read operations will return without reading anything.

Use unsigned long long which will allow you to insert bigger numbers.

kocica
  • 6,412
  • 2
  • 14
  • 35
  • But wouldn't unsigned long long still run into the same issue if the user types in a really really huge number? – templatetypedef Aug 12 '17 at 15:11
  • Of course that the same thing will happen, i just tried to give him advice if he need to insert bigger numbers :-) – kocica Aug 12 '17 at 15:12
0

You are taking your inputs as " int " and value range for int is between -2,147,483,648 to 2,147,483,647. Which means that if you exceed this value 2,147,483,647 it can not be stored as an integer(int) type. You should probably use Long data type for such large numbers.

-1

You can add a following check in your code if the user input more than int limit

int GVFU()
{
    std::cout <<"enter number";
    int a;
    std::cin >> a;
    if(cin.fail())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input " << endl;

    }
    return a;
}

I would also add exit if invalid number

#include <iostream>
#include <cstdlib>

using namespace std;

int GVFU()
{
    std::cout <<"enter number";
    int a;
    std::cin >> a;
    if(cin.fail())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input " << endl;
        std::exit(EXIT_FAILURE);
    }
    return a;
}

Note: You could also more info instead of just "Invalid input " Output like size or limit info

enter 1 for addition and 2 for subtraction1
enter number4535245242
Invalid input 
Program ended with exit code: 1
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52