Hello, I am having issues with my Data Structures lab and I was wondering if you guys could help me.
Here is the lab:
Write a program that asks for two numbers as arguments, multiplies them, and prints out the product and the two numbers. Call your executable, multiply; it operates like, for example,
multiply 3.14 57
If a wrong number of arguments or wrong types are supplied, it should display a message informing the user how to use the program correctly.
Your program must be made up of three modules, namely, main.cpp ( for handling the arguments ), product.cpp ( for calculating the product ), and print.cpp ( for printing out the product ). Check if each of the modules can be compiled properly by the command,
g++ -c program_name.cpp
Finally, write a Makefile that contains all the compilation processes to generate the executable multiply. The Makefile should have a clean instruction so that when one executes,
make clean
the object files will be deleted.
End lab
Here is what I know and would like to find out
Know:
How to write a program that accepts command line arguments, multiplies them and shows the result
I have to break up the program into 3 objects and link them using the compiler
Would like to find out:
How would i go about splitting the program?(Without using a header file)
I understand the concept of the Makefile, but don't understand the writing of one
Here is the code i wrote that is working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if (argc <= 1)
{
if (argv[0])
{
cout << "You did not provide an argument\n";
}
exit(1);
}
double multiple = 1.0;
for(int i = 1; i < argc; i++)
{
stringstream convert(argv[i]);
double currentArg = 0.0;
//should the conversion fail
if (!(convert >> currentArg))
{
currentArg = 0;
cout << "Please enter only numerical values and I will show the product\n";
exit(1);
}
multiple = currentArg * multiple;
}
cout << "Your multiple is " << multiple << "\n";
return 0;
}
| |
I tested it with multiple values and tried throwing some curveballs and it worked out fine.
Thank you!