how do I put in kg to pounds in one case i have one header file and 2 cpp files how can I make it work
Printable View
how do I put in kg to pounds in one case i have one header file and 2 cpp files how can I make it work
like as an option
I guess you'd do something like this.
Code:enum WeightUnits {
LBS_TO_KG,
KG_TO_LBS
};
WeightUnits GetOptionFromUser(void);
switch ( GetOptionFromUser() )
{
case LBS_TO_KG:
ConvertKgToLbs();
break;
case KG_TO_LBS:
ConvertLbsToKg();
break;
};
this is my functions headerthis is my functions cppCode:#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_
class BaseFunction
{
private:
protected:
public:
BaseFunction();
virtual int Menu() = 0;
};
class Functions : public BaseFunction
{
private:
public:
Functions();
void Function1();
void Function2();
void Function3();
void Function4();
void Function5();
virtual int Menu() override;
};
#endif
Code:#include "stdafx.h"
#include "Functions.h"
#include <iostream>
using namespace std;
BaseFunction::BaseFunction()
{
cout << "Constructing base class..." << endl;
}
Functions::Functions()
{
cout << "Constructing derived class..." << endl;
}
void Functions::Function1()
{
cout << "Invoking Function 1..." << endl;
double kg, lb;
cout << "this will maybe convert kilograms to pounds.\n";
cout << "please enter weight in kilograms"
cin >> lb;
lb = kg * 2.2;
cout << "The weight in kilograms is: " << kg << '\n';
}
void Functions::Function2()
{
cout << "Invoking Function 2..." << endl;
}
void Functions::Function3()
{
cout << "Invoking Function 3..." << endl;
}
void Functions::Function4()
{
cout << "Invoking Function 4..." << endl;
}
void Functions::Function5()
{
cout << "Invoking Function 5..." << endl;
}
int Functions::Menu()
{
int opt;
do {
for (int i = 1; i <= 5; i++)
cout << "Function " << i << " [" << i << ']' << endl;
cout << "Quit [0]\nEnter Option: ";
cin >> opt;
} while (opt < 0 || opt > 5);
return opt;
}
this is sampleproject.cppi want 1 to be kilograms to pounds 2 to be celsius to kelvin 3 to be teaspoons to millilitres 4 to be multiplication table and 5 to be a squares table can you helpCode:#include "stdafx.h"
#include <iostream>
#include "Functions.h"
using namespace std;
int main()
{
int opt;
Functions *f = new Functions();
do {
opt = f->Menu();
switch (opt)
{
case 1:
f->Function1();
double kg, lb;
cout << "this will maybe convert kilograms to pounds.\n";
cout << "please enter weight in kilograms"
cin >> lb;
lb = kg * 2.2;
cout << "The weight in kilograms is: " << kg << '\n';
break;
case 2:
f->Function2(); break;
case 3:
f->Function3(); break;
case 4:
f->Function4(); break;
case 5:
f->Function5(); break;
}
} while (opt != 0);
}
Ignoring for a moment all the horrible style, over engineering, and "that's not doing what you think it's doing" problems, the specific big problem is functions.cpp lines 2 through 24, and sampleproject.cpp lines 18 through 22. In both places you are first reading in the pounds, then converting an uninitialized number of kg to pounds, then printing the kilograms. Doing that will not convert kilograms to pounds. A more reasonable set of steps would be to read in kilograms, then convert kilograms to pounds, then print pounds. Also, you don't need to do it in both places.
There are two many other problems for me to list them all, so here's a basic one set you on the right track, hopefully: You should not have names that contain numbers. If you see yourself tempted to do that, instead create an array. Specifically, create an array of 5 things called "function" instead of Function1 through Function5. Make that work, and you'll have fixed several problems in your code.
where should i put the array
Rather than create an array, you should be renaming functions.
Function1, Function2, ... these are meaningless function names that don't tell anyone anything about what they do, which I would argue is a lot more important. Some better names might be:
Code:#ifndef FUNCTIONS_HPP
#define FUNCTIONS_HPP
#include <iostream>
#include <string>
enum Options {NORMAL_EXIT, KG_TO_LBS, CELSIUS_TO_KELVIN, TSP_TO_ML, M_TABLE, S_TABLE};
class Functions
{
public:
static double GetDouble(const std::string& prompt);
static int GetInt(const std::string& prompt);
static Options Menu(void);
static double ConvertCelsiusToKelvin(double c);
static double ConvertKgToLbs(double kg);
static double ConvertTspToMl(double tsp);
static void MulTable(int first, int last);
static void SquareTable(int first, int last, unsigned int step);
};
#endif
thanks!!!
i got this now
Code:#include "stdafx.h"
#include "Functions.h"
#include <iostream>
using namespace std;
{
cout << "Constructing base class..." << endl;
}
Functions::()
{
cout << "Constructing derived class..." << endl;
}
void Functions::()
{
cout << "Invoking Function 1..." << endl;
}
void Functions::()
{
cout << "Invoking Function 2..." << endl;
}
void Functions::()
{
cout << "Invoking Function 3..." << endl;
}
void Functions::()
{
cout << "Invoking Function 4..." << endl;
}
void Functions::()
{
cout << "Invoking Function 5..." << endl;
}
int Functions::()
{
int opt;
do {
for (int i = 1; i <= 5; i++)
cout << "Function " << i << " [" << i << ']' << endl;
cout << "Quit [0]\nEnter Option: ";
cin >> opt;
} while (opt < 0 || opt > 5);
return opt;
}
Code:#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_
#include <iostream>
#include <string>
enum Options { NORMAL_EXIT, KG_TO_LBS, CELSIUS_TO_KELVIN, TSP_TO_ML, M_TABLE, S_TABLE };
class Functions
{
public:
static double GetDouble(const std::string& prompt);
static int GetInt(const std::string& prompt);
static Options Menu(void);
static double ConvertCelsiusToKelvin(double c);
static double ConvertKgToLbs(double kg);
static double ConvertTspToMl(double tsp);
static void MulTable(int first, int last);
static void SquareTable(int first, int last, unsigned int step);
};
#endif
Code:#include "stdafx.h"
#include <iostream>
#include "Functions.h"
using namespace std;
int main()
{
int opt;
Functions *f = new Functions();
do {
opt = f->Menu();
switch (opt)
{
case 1:
f->(); break;
case 2:
f->(); break;
case 3:
f->(); break;
case 4:
f->(); break;
case 5:
f->(); break;
}
} while (opt != 0);
}
I agree that the somewhere in the code, each function should have a better name. But in naming the functions function1 to function5, sonicflare9 properly observed that there is something common about all operations in a menu. This could be placed in an array of menu functions.
I do agree that the list of functions you named should exist too.
One thing in your code I don't see the point of is making them static functions to a class. This is C++ not Java; there's such a thing as free functions. You don't need a class "Functions". OTOH, a class "Function" or better named, "MenuItem" is not a bad idea.
@ sonicflare9
The array would go in main or in a class that's created in main.
Your last code doesn't compile at all. Nor does it attempt to convert kg to pounds anywhere. The code before was (marginally) better.
what should I do to make it work
im kinda new to this thing
Well, to be honest, you only need roughly a handful of C++ lessons to write this program.
- Loops (while, at the bare minimum)
- Functions
- Switch-case
- I/O
Another key problem is that the menu function seems to be doing something that I would expect all menus to do: plainly state the options.
So maybe try drastically simplifying this design?
I'm really not sure what to say. I'm also tempted to call this approach over-engineered for a beginner.Quote:
I agree that the somewhere in the code, each function should have a better name. But in naming the functions function1 to function5, sonicflare9 properly observed that there is something common about all operations in a menu. This could be placed in an array of menu functions.
It feels like an array is getting preferential treatment just because the names were originally enumerated. A static class keeps everything as organized as an array would. The best thing that could be said for an array of function pointers in this program is that it supplants a switch. Personally, I find the switch to be clearer code.
It's not the worst idea, I just don't think it's something people need to learn early on. At least I suspect that sonicflare9 hasn't been at this very long.
maybe would it work if I change the functions in the header one to different names? the class functions publics one? and then maybe i can change the sampleproject and functions cpp