best way to do this?

This is a discussion on best way to do this? within the C++ Programming forums, part of the General Programming Boards category; Just wondering if this is the best / Cleanest way to write this code. Code: #include <iostream> using namespace std; ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    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?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    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.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    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.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    You missed an 'int' before 'AreaCube'.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    lmao how the hell did i miss something as small as that

    thanks alot mate. hugo.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21