New Posts  All Forums:Forum Nav:

More help with C++

post #1 of 10
Thread Starter 
I created a thread about a problem I had before, and I ave another again. I'll just post them in this thread.

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
post #2 of 10
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.

int amount = -1; //in your Head.h
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

#ifndef HEAD_D
#define ADD_H

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 
  hide details  
Reply
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 
  hide details  
Reply
post #3 of 10
Thread Starter 
Bump! New problem
post #4 of 10
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 
  hide details  
Reply
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 
  hide details  
Reply
post #5 of 10
Thread Starter 
Quote:
Originally Posted by mykah89 View Post
You.. almost.. passed amount into the function, now you are declaring a new uninitialized amount variable.
when I delete "int amount;", nothing changes
post #6 of 10
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 
  hide details  
Reply
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 
  hide details  
Reply
post #7 of 10
Thread Starter 
Quote:
Originally Posted by FearMeansControl View Post
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.
post #8 of 10
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 
  hide details  
Reply
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 
  hide details  
Reply
post #9 of 10
Thread Starter 
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
post #10 of 10
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 
  hide details  
Reply
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 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming