#include<iostream>
#include<conio.h>
#include<time.h>
#include<windows.h>
#include<math.h>
#include <string.h>
#include <sstream>
using namespace std;
int main()
{ int result = 1;
float checker; //Holds the full array
char char_holder;
int loop = 0;
float checker2 = 0; //gets the letter
float checker3 = 0; //Holds the previous array
int counter;
int x = 0;
char name = 'A';
double Dresult;
char Cresult[10] = {'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'};
do{
printf("\n Enter value for %c ", name);
cin>>Cresult;
for (x = 0; Cresult[x] != '\0' ; x++) //Cresult[x] != Null Terminator
{
if (x != 0)
{checker3 = checker;}
char_holder = (int)Cresult[x];
checker2 = atof(&char_holder);
if (x == 0) //if its the first one start adding onto it
{ checker3 = checker2;
checker = checker2;
}
else
{checker2 /= 10;
checker = (checker + checker2)*10;
Sleep(1000);
if(checker > 2147483647)
{ cout<<"Overflow error....2\n";
Sleep(2000);
result = 0;
Dresult = 0;
for (x = 0 ; Cresult[x] != '\0' ; x++) //Cresult[x] != Null Terminator
{
Cresult[x]= '\0';}
break;
}
else if(checker < checker3)
{ cout<<"Overflow error....1";
Sleep(2000);
Dresult = 0;
result = 0;
break;
}
//else { checker3 = checker;}
}
cout<<"Checker: "<<checker<<"\t";
cout<<"Checker2: "<<checker2<<"\t";
cout<<"Checker3: "<<checker3<<"\t"<<'\n';
Sleep(10);
}
if (Dresult != 0)
{
Dresult = atoi (Cresult);
}
cout<<"\n\n";
cout<<"Cresult: "<<Cresult<<'\n';
cout<<"Dresult: "<<Dresult<<'\n';;
Sleep(1000);
for (x = 0 ; Cresult[x] != 0 ; x++)
{
Cresult[x]= '\0';}
cout<<Cresult;
checker = 0;
checker2 = 0;
checker3 =0;
Dresult = 0;
Sleep(1000);
}
while(loop == 0);
return 0;
}
Attention: This is something I was messing around with for hours, I don't expect it to look picture perfect, right now I just want the idea working before i clean it up.
This is an isolated part of code from a bigger calculator I'm working on. My teacher enjoys breaking our programs in any way possible, so I'm trying to show him up by making it unable to be broken. In my original project I have it setup to use a process similar to this in order to recieve the number inputted by the user. In the original project I use isdigit to check if theyre inputting characters where theyre inputting numbers so thats why i have the input orginally saved as a string in order to do so. In order to check for overflow i have it set up so that it manually adds on each number until it reaches overflow (overflows an int) then it breaks out and resets. If you input 21 it will output 21, if you input 999999999999, itll overflow and work properly, but once i start having 999999999999999999999999999(enough to fill up 2 - 3 lines of my console. It out puts 1.#INF without adding up the numbers, outputs overflow(which is good) it resets everything(which is good), but once it reaches the end of the program it crashes. I have no idea how to stop it from doing so besides it just being unecessary for me to input so much. A friend of mine doesnt have the fix for characters, so if you input fsdfsdf21, it wil break, but for his if you spam 9's it outputs 1.#INF, but continues to run (He accepts the input as an INT). I don't know if anyone will know what the problem is =/.