larger numbers wont print out correctly

Sep 11, 2018 at 2:01am
So i have a problem where large numbers wont print out correctly. I tried debugging and the number is said to be 657901841 it is gotten from adding all numbers together in a for loop. However when i tried to output the result it would output as -2147483648. I will only post part of the code where it is having the problem because its too long.

1
2
3
4
5
6
7
8
9
10
  for (int i = 0; i < 8; i++) {      //Adds total together

	
		TimeTotal += NTimeTotal[i];
		StepTotal += NStepTotal[i];
	}
   
     cout << StepTotal << endl;

	

So when i tried to print out the number it would give it as a negative stated above. It is really weird because when i played around it a few days ago, it didnt have this problem at all. I dont even remember changing the code around. But now, its surprisingly giving me this error. Any fix for this? I have tried using fixed , setprecision etcc.. No luck ;(. Thanks for any help!
Sep 11, 2018 at 2:39am
Hi,

The value is probably too large for an int, try changing the type of StepTotal to long int

Read about the basic types here:
https://en.cppreference.com/w/cpp/language/types
Sep 11, 2018 at 2:48am
i have tried that still the same ;(


EDIT: i tried unsigned long int and it fixes the negative number problem but wrong console output compared to the debugger version
Last edited on Sep 11, 2018 at 2:55am
Sep 11, 2018 at 2:54am
Was StepTotal initialised properly? If it wasn't, it could be some large random value. Also show the code where the values are put into the array.

In short make a small compilable program that demonstrates the problem.

Does your program run? If so consider using a debugger.
Sep 11, 2018 at 3:13am
I think i fixed it! thanks for the help!
Topic archived. No new replies allowed.