| | |
Counter changing to a negative number
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 14
Reputation:
Solved Threads: 0
Hi All,
I'm having a bit of trouble with a simulation I am writing in C++. Any help to solve the problem would be appreciated.
The simuation needs to run 5500000000 times.
When it gets to 2100000000, the counter I am using changes to -210000000. So as you can see, it will never finsh and do what I want.
I presume this is to do with how I have declared the variables, but am unsure of how to solve this.
Selected code below
Hope you can help.
Cheers
I'm having a bit of trouble with a simulation I am writing in C++. Any help to solve the problem would be appreciated.
The simuation needs to run 5500000000 times.
When it gets to 2100000000, the counter I am using changes to -210000000. So as you can see, it will never finsh and do what I want.
I presume this is to do with how I have declared the variables, but am unsure of how to solve this.
Selected code below
C++ Syntax (Toggle Plain Text)
int clock = 0; // Start the simulation while(clock < simulationlength) { //arrival process on the corporate links if (clock % 4 == 0) {
Hope you can help.
Cheers
use unsigned long intead of int. But first check for the maximum value for your compiler in the file limits.h. The maximum value for an unsigned long on my compiler is 4,294,967,295 -- slightly smaller than the value you want. You may have to go with a 64-bit integer, longlong on some compilers and __int64 on others.
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Solved Threads: 2
Yeah, integers wrap around themselves. For example if you had a signed (takes positive and negative numbers, i think the min is -32,000 something and max is 32,000 something), then when you get to that maximum number and add 1 it will wrap around and start from its min.
If you have an unsigned variable (only positives), the maximum number it can take is double the normal. If you use unsigned long int, you will be able to use upto a value in the millions
If you have an unsigned variable (only positives), the maximum number it can take is double the normal. If you use unsigned long int, you will be able to use upto a value in the millions
![]() |
Similar Threads
- Insertion Sort and Number Occurence (C++)
- Need help displaying when user enters negative number? (C++)
- hvving problems displaying after user inters negative number? (C++)
Other Threads in the C++ Forum
- Previous Thread: Insertion Sort Problem
- Next Thread: Program Involving Overloading
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






