| | #1 |
| LOL I AM A GIRL GUISE | So I'm learning C++, my first language I'm learning. Now, I'm working with this code: #include <iostream> using namespace std; int main() { int y; cout<<"Please enter a number: "; cin>> y; cin.ignore(); cout<<"You entered: "<< y <<"\n"; cin.get(); } It runs fine, but if I randomly type in a number with a large amount of value places, the number the program tells me I typed in is incorrect. The number it gives me usually starts with 21473, but then will go on for a couple of more value places. I don't understand how it could get this wrong, and if someone can explain, it'd be appreciated.
__________________ |
| | |
| | #2 |
| The World Rests On Me Join Date: May 2007 Location: Question is... where are YOU?! I'll tell you where... sittin on my shoulders >.> III SEEEE YOOOOU! Age: 20
Posts: 1,852
Rep Power: 3 ![]() | lol... C ++ yay.... >.> the next class I have to look forward to lol.... I already graduated from one thing and got my degree... next thing... great, well at least that wont be untill the fall :-p |
| | |
| | #3 |
| LOL I AM A GIRL GUISE | ...how does that help me at all?
__________________ |
| | |
| | #4 |
| Keyblade Novice | How many places are you using in your random large numbers? Since your data type is int, there is a limit to how large a value it can hold. In general, this value is 2,147,483,647 so if you're going larger than that, it won't work correctly. Here's a little more technical info if you're curious: Your default int type is called a signed integer. It can range from -2,147,483,648 to 2,147,483,647 (signed means it can be positive or negative). You can also have an unsigned integer which has a range from 0 to 4,294,967,295. I haven't done much C++ recently but if I remember correctly, to make an unsigned int, just add the modifier unsigned in front of the variable declaration (i.e. unsigned int y;). I hope this helps. Let me know if you need anything else. I've got quite a bit of experience in both C++ and Java - I just need to refresh my memory a bit. |
| | |
![]() |
| Thread Tools | |
| Display Modes | |
| |