New Posts  All Forums:Forum Nav:

# More help with C++

I am following this tutorial here: http://www.learncpp.com/

I am attempting to not use global variables, and pass the variables info through the functions.

This is the main file. It takes the user input. (main.cpp)
Code:
``````#include "stdafx.h"
#include <iostream>

void math();

int main()
{
using namespace std;
cout << "How many numbers of Fibonacci do you want?" << endl;
int input;
cin >> input; //Input on how many numbers are wanted

if(input > 1476)
{
while (input > 1476) //repeat until good number is input
{
cout << "The maximum number is 1467, please choose a lower number" << endl;
cin >> input;
}
}
math(input); //start math process

cin.clear();
cin.ignore(255, '\
');
cin.get();

return 0;

}``````
This is math.cpp. It does the calculations.
Code:
``````#include "stdafx.h"
#include <iostream>

void math(amount)
{
int amount;

using namespace std;

// OX: Stands for "Old x". It is needed so I can calculate the new "y" After I change the "x"
double x(0), y(1), z(2), ox(0);

int Counter(1);

cout << "1:1" << endl; //Must be put here because the first "1" is not added

while ( Counter < amount );
{
Counter=Counter+1;

cout << z << ":" << x+y << endl; // Displays The Fib. Number
ox=x;   // Change "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.

}
}``````
I get the following errors:
Math.cpp:

>c:\\users\\justin\\documents\\visual studio 2010\\projects\\fi\\fi\\math.cpp(5): error C2065: 'amount' : undeclared identifier
1>c:\\users\\justin\\documents\\visual studio 2010\\projects\\fi\\fi\\math.cpp(6): error C2448: 'math' : function-style initializer appears to be a function definition

Main.cpp:
1>c:\\users\\justin\\documents\\visual studio 2010\\projects\\fi\\fi\\main.cpp(21): error C2660: 'math' : function does not take 1 arguments

I colored the error lines.

What could be the problem? Thanks
Edited by Jtvd78 - 3/2/11 at 2:35pm
Well I am not sure this is it, but here are some issues in terms of good code.

cout << "How many numbers of Fibonacci do you want?" << endl;
cin >> amount; //Input on how many numbers are wanted

if(amount > 1476)
{
do //repeat until good number is input
{
cout << "The maximum number is 1467, please choose a lower number" << endl;
cin >> amount;

} while (amount > 1476);

math(); //start math process
}

All of the above can be reduced to this.

while( amount > 1476 || amount < 0){
cout << "How many numbers of Fibonacci do you want?" << endl;
cin >> amount;
if( amount > 1476 || amount < 0){
cout << "The maximum number is 1467, please choose a lower number" << endl;
}

The reason I made it an integer is because depending on your compiler, it might be complaining about you comparing an integer to a double.

I am also pretty certain you can simply post

integer amount; //Global V

#endif

in one of your .c files and include it with the other.
Edited by joemaniaci - 3/1/11 at 7:18pm
 I Love Cheese (13 items)
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt
CaseMouse
Rosewill THOR Logitech Trackball
 I Love Cheese (13 items)
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt
CaseMouse
Rosewill THOR Logitech Trackball
Bump! New problem
You.. almost.. passed amount into the function, now you are declaring a new uninitialized amount variable.
Edited by mykah89 - 3/2/11 at 2:42pm
 My System (13 items)
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07
Mouse
CM Sentinel
 My System (13 items)
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07
Mouse
CM Sentinel
Quote:
 Originally Posted by mykah89 You.. almost.. passed amount into the function, now you are declaring a new uninitialized amount variable.
when I delete "int amount;", nothing changes
You need to declare the type of amount in the function implementation:

Code:
``````void math(int amount){
// do sum math (catch my pun?!)
}``````
 Jesus PC v5.3 (13 items)
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs
PowerCaseMouse
Thermaltake 750W RocketPwn G500
 Jesus PC v5.3 (13 items)
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs
PowerCaseMouse
Thermaltake 750W RocketPwn G500
Quote:
 Originally Posted by FearMeansControl You need to declare the type of amount in the function implementation: Code: ``````void math(int amount){ // do sum math (catch my pun?!) }``````
I love you. The problem in main is still there tho.
oops! Its a little strange the way this is set up to be honest..

You would usually see a header file called math.h which would declare the math function, but its defined in your main file.. In any event, you should declare the type there as well:

Code:
``void math(int amount);``
 Jesus PC v5.3 (13 items)
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs
PowerCaseMouse
Thermaltake 750W RocketPwn G500
 Jesus PC v5.3 (13 items)
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs
PowerCaseMouse
Thermaltake 750W RocketPwn G500
Hmm...
Now, it just shows 1:1,
It seems like amount is being set to a number lover then 1

EDITroblem with the loop.

THanks
Edited by Jtvd78 - 3/2/11 at 3:04pm
Glad to help, first bit of C++ thinking I've done in a few months
 Jesus PC v5.3 (13 items)
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs
PowerCaseMouse
Thermaltake 750W RocketPwn G500
 Jesus PC v5.3 (13 items)
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs
PowerCaseMouse
Thermaltake 750W RocketPwn G500
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Application Programming