New Posts  All Forums:Forum Nav:

Very basic C++ Help

post #1 of 10
Thread Starter 
I have decided to learn C++. I didn't know what to make first, so I made a simple Fibonacci Number generator. It works up to about the 40th number, and then it starts giving me crazy numbers. I don't Know if its because the numbers are too large, or what, but What is wrong?

Code:
#include <iostream>

int main()
{
using namespace std;

int x, y, z;
x=0; //First Second Number
y=1; //Second Starting Number
z=2; //Number on side

int ox; // Stands for "Old x". It is needed so I can calculate the new "y" After I change the "x"


int Counter, Amount;
cout << "How many numbers of Fibonacci do you want?" << endl;
cin >> Amount; //Input on how many numbers are wanted
Counter=1; // how many times gone through


cout << "1:1" << endl; //Must be put here because the first one is not added
do
{
Counter=Counter+1;

cout << z << ":" << x+y << endl; // Displays HTe Fib. Number
ox=x; // MAkes "Old X" to "X"
x=y; // Changes X to Y. Used for next fib. Number
y=ox+y; // Finds the new Y. It uses OX, because the X variable is already Changed.

z=z+1; //  This is for the counting number next to the fib. Number.
}
while ( Counter < Amount );

return 0;
}
You may not understand this, so I put in some comments. Hope they explain it. So why do the numbers mess up?


Thanks
post #2 of 10
Thread Starter 
Here is a picture of the problem
post #3 of 10
The maximum value an int can hold in C++ is 65,535. Not sure if that's your issue or not.
post #4 of 10
Thread Starter 
Quote:
Originally Posted by hermitmaster View Post
The maximum value an int can hold in C++ is 65,535. Not sure if that's your issue or not.
What should I use then if not an int?
post #5 of 10
Maybe try long int
post #6 of 10
Quote:
Originally Posted by Jtvd78 View Post
What should I use then if not an int?
Sorry, int is 2,147,483,647. 65,535 is a short. Use an unsigned long.
post #7 of 10
You would need to use something like long long instead of int. Yeah it looks retarded but it allows for up to 9,223,372,036,854,775,807.
post #8 of 10
Quote:
Originally Posted by Jtvd78 View Post
What should I use then if not an int?
The biggest I can think of would be the long int or the long double.. You might want to put some code for the program to stop after reaching the limits of the variable data types.
You'll find here a table with the limits and such: http://www.cplusplus.com/doc/tutorial/variables/
Hurricane
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q6600 @ 3.2GHz [G0] Gigabyte EP43-DS3L ATI Radeon HD 4650 1GB (Gigabyte) 4GB 2x2GB Kingston HyperX 800MHz 6-6-6-18 
Hard DriveOptical DriveOSMonitor
1TB WD Caviar Black, 2x 320GB WD Caviar Blue ASUS DVD-RW Lightscribe Windows 7 Ultimate x32 BenQ T2200HD Full HD 21.5" 
KeyboardPowerCaseMouse
A4Tech Office 8K Wireless keyboard Cooler Master GX-650W Cooler Master Storm Scout A4Tech Office 8K Wireless mouse 
  hide details  
Reply
Hurricane
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q6600 @ 3.2GHz [G0] Gigabyte EP43-DS3L ATI Radeon HD 4650 1GB (Gigabyte) 4GB 2x2GB Kingston HyperX 800MHz 6-6-6-18 
Hard DriveOptical DriveOSMonitor
1TB WD Caviar Black, 2x 320GB WD Caviar Blue ASUS DVD-RW Lightscribe Windows 7 Ultimate x32 BenQ T2200HD Full HD 21.5" 
KeyboardPowerCaseMouse
A4Tech Office 8K Wireless keyboard Cooler Master GX-650W Cooler Master Storm Scout A4Tech Office 8K Wireless mouse 
  hide details  
Reply
post #9 of 10
Thread Starter 
Thanks guys. The long long wasn't long enough, but when I used the double, it calculated it correctly, but it was scientific notation.
post #10 of 10
Pretty much what everyone said. A signed integer has a range of -2,147,483,648 to 2,147,483,647 using 4 bytes. 1 byte has 8 bits, meaning it is a 32 bit number. But since it is signed, the left most bit is the sign flag, leaving 31 bits for the number. On an unsigned but, the left most bit is not used as the sign flag, leaving 32 bits for the number. With an unsigned int, the range is 0 to 2^32 or 4294967296.

Long long int is 8 bytes, meaning 64 bits. A signed long long would have a range of –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 or [2^63 - (2^63)-1] while an unsigned long long has a range from 0 to 2^64.

EDIT: Whoops, looks like I took too long. Seems like you figured it out. kbye!
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming