Hello Avex,
Looking over your program here are some small tips that may help:
The if statement the displays the help information can just as easily be written as:
1 2 3 4 5 6
|
cout
<< "Usage: calc <X> <operator> [Y] [options] \n\n"
<< "Unary Operators: \n"
<< " ! Computes the factorial of the given operand.\n"
<< " F Displays the first X numbers of the Fibonacci sequence. \n\n"
// rest of output.
|
You do not need all those "std::endl"s the "\n"s will do. Also you do not need all the "std::cout" for each line. It can all be chained together. Leave the "std::endl" for the end of the last line. The "\n" tends to clear the output buffer in most cases, so you do not need all the overhead associated with the "std::endl".
Next I would put that code in a function because there may be one ot two other places that you could call it from.
In your switch/case for cases "F" and "P" it would help to write the case statements as:
1 2 3 4 5 6 7 8 9 10
|
switch (opr)
{
case 'f':
case 'F':
break;
default:
std::cout << "Usage: calc <X> <operator> [Y] [--o] [file name] " << endl;
return 1;
}
|
This way it will not matter if someone would use a lowercase letter or an upper case letter the case statements will catch either and just fall through until it finds a "break" statement.
The "default" is helpful if someone mistypes an operator. Here you could use the "std::cout" statement as I did or call the "help" function to display the instructions. The "return 1;" means that you left the program with a problem. Since (0) zero means that there was no problem any number greater than (0) zero means there is a problem and later you can use that number to help track down where the problem came from.
Now for the bigger problem:
1 2 3 4 5 6 7
|
else
{
int num1 = atoi(argv[1]);
int num2 = atoi(argv[3]);
char opr = argv[2][0];
switch (opr)
|
Line 3 works as long as it is a number that can be converted.
line 5 works to get your operator.
Line 4 does not work. One possibility is the "arg[3]" could be outside the bounds of the array and you would be accessing unknown memory which may or may not be convertible to a number. This will produce a run time error and stop the program. I doubt that you are up to "try/catch" yet, but this is one time where I use it to keep the program from crashing and deal with the problem in the program.
A suggestion would be to keep line 5 before the switch and in the for cases "!" and "F" you know that you will have only "arg[1]" to convert to a number and for the other cases you will have "argv[1]" and "argv3[3]" to convert to numbers.
You collect "argc" when the program starts, but never make use of it and it can be very helpful. At the minimum "argc" could be 3, (program name num opoerator) to a maximum of 6 (program name num operator [num] [switch(--o)] [file name]) or something in between. "argc" can be used to determine how much information there is to collect and use.
In order to write to a file you will need to include the header file "<fstream>" and create a function like "writeToFile()" in order to make use of the switch (--o). Then you will need to decide what variables to send to the function and what to write to the file. Also you will need to decide if the file should be overwritten each time it is opened or appended to.
If your opening lines are your interpretation of the specs, (guidelines, directions0, that is a start, but it is better to post what you were actually given as someone else may see something that you do not.
Here is the code that I have now - I apologize if it's a little messy now. I will work on making it look a bit nicer later on. |
The time to clean up the code is when you write it and not so much after it is done. Clean code is much easier to read and it helps others who have to read it because they do not have to spend as much time trying to figure it out.
Remember that the compiler does not care about white space, blank lines or comments when it compiles the code. The person reading the code does. It looks like you are doing a good job with your comments before the functions.
In a little while I will load up the program and see if there is anything else I can find.
One last note:
Hope that helps,
Andy