-
best way to do this?
Just wondering if this is the best / Cleanest way to write this code.
Code:
#include <iostream>
using namespace std;
int Multiplier(int AmountToMultiply);
int main()
{
int input;
int Result = 0;
cout << "Enter Cat or Dogs age: ";
cin >> input;
Result = Multiplier(input);
cout << "\nYour animal is: " << Result << endl;
system("PAUSE");
return 0;
}
int Multiplier(int Amount)
{
return Amount * 7;
}
also, if i was to make a amount.h file which only contains
Code:
int Multiplier(int Amount)
{
return Amount * 7;
}
and remove that part from the main code and add #include "Amount.h" would it still work?
-
Yes, but it's a lot of work for something so tiny, and it definitely doesn't need its own header file:
Code:
#include <iostream>
int main () {
int dog_age = 0;
std::cout << "Enter dog's age: ";
std::cin >> dog_age;
std::cout << "Your animal is " << dog_age * 7 << "\n";
return 0;
}
If you have a lot of Dogs or something you can make it a class so that you can build an array, and have the constructor compute the age.
-
> and remove that part from the main code and add #include "Amount.h" would it still work?
It would, but it's not the best way.
Code:
#include <iostream>
#include "animal.h"
using namespace std;
int main()
{
int input;
int Result = 0;
cout << "Enter Cat or Dogs age: ";
cin >> input;
Result = Multiplier(input);
cout << "\nYour animal is: " << Result << endl;
system("PAUSE");
return 0;
}
// this is animal.h
int Multiplier(int Amount);
// This is animal.cpp
#include "animal.h"
int Multiplier(int Amount)
{
return Amount * 7;
}
The interface goes into a header file, and the implementation goes into a source file.
main.cpp and animal.cpp would be source files in your project.
-
lol yeh i know its big and no need for a header but im learning.
i was just testing out a function for it. but would it work by doing it with the header i said? im not going to, just want to know as im getting used to using functions and headers.
cheers. Hugo.
-
heres another thing that will not work...
its of a book im readin and it wont work. the error is below it.
sorry to keep asking.
Code:
// demonstrates the use of default parameters
#include <iostream>
using namespace std;
int AreaCube(int Length, int Width = 25, int Height = 1);
int main()
{
int Length = 100;
int Width = 50;
int Height = 2;
int Area;
Area = AreaCube(Length, Width, Height);
cout << "First area equals: " << Area << "\n";
Area = AreaCube(Length, Width);
cout << "Second area equals: " << Area << "\n";
Area = AreaCube(Length);
cout << "Third area equals: " << Area << "\n";
system("PAUSE");
return 0;
}
AreaCube(int Length, int Width, int Height)
{
return (Length * Width * Height);
}
Code:
------ Build started: Project: area thing, Configuration: Debug Win32 ------
Compiling...
area thing.cpp
.\area thing.cpp(30) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Build log was saved at "file://c:\Documents and Settings\Reece.YOUR-E0367A1424\My Documents\Visual Studio 2005\Projects\area thing\area thing\Debug\BuildLog.htm"
area thing - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
hugo.
-
You missed an 'int' before 'AreaCube'.
-
lmao how the hell did i miss something as small as that
thanks alot mate. hugo.