The Issue
I was encountering issues within VSCode that I could not figure out how to fix, most of which stemmed from attempts to compile code and run programs.
I kept getting bash errors, or error codes telling me that running such-and-such program was impossible because I was trying to execute a directory, not a file, and so forth.
I tried to remedy the situation and figure out what was going on by copying the code from my new calculator program into an old program from CS50, then running the code there without changing the directory or the file-name—somehow, that produced the same errors.
So I simply deleted all of the files I had that came from old labs, problem sets, test programs, etc. I wanted a fresh start—and it worked.
Ye Olde Silly Calculator Programme
Let’s go through this calculator program.
First, I defined char choice with a set ‘index’ (if that’s the right word) of 20 so as not to multiply googlepluxes, megabazillatrillions, and so forth.
char choice[20];
Then, a couple of simple welcome messages in the form of printf statements.
printf("Hello, Friend! It's your favorite calculator boddy, Mr. Calculator!\n");
printf("Let's crunch some digits.\n");
Then, I defined first_number and second_number (yes, they could have been num1/num2 but I wanted to be verbose and explicit) and assigned them as double values so that they would have enough memory to be used twice.
double first_number;
printf("Enter your first number: ");
scanf("%lf", &first_number);
printf("You entered: %.2lf\n", first_number);
double second_number;
printf("Great! Now, enter your second number: ");
scanf("%lf", &second_number);
printf("You entered: %.2lf\n", second_number);
Then, some more silly & encouraging printf statements to keep the user moving along.
printf("Nice work. Now, what mathematical operation would you like me to perform?\n");
printf("Your available options are: add, subtract, multiply, and divide.\n");
The user then types add, subtract, etc. I chose not to have a menu that functioned with a single character input, like ‘a’ == add, ‘b’ == subtract, etc., because I wanted a little bit more explicit type of input from the user, and I wanted a different type of code. I determined this after conversing what ChatGPT about what a skeleton of this part of the program—the menu—would look like, code-wise.
Then, I used a scanf function to record the user’s input as a choice. This will be more important later on.
scanf("%s", choice);
Following this, I assign another variable called result as a double to store the value of the equation—depending on the user’s choice—before printing it back to them in the final external stage of the program.
The next part is pretty self-explanatory. It provides a function with four ‘chunks’ that determine what the user’s choice was—add, multiply, subtract, or divide. Then the code determines what type of string via strcmp is necessary to use, performs the required action, and prints out the result of their two inputted numbers and the type of equation they asked for—one of the options in the menu.
if (strcmp(choice, "add") == 0)
{
result = first_number + second_number;
printf("The result of %.2lf + %.2lf is %2.2lf\n", first_number, second_number, result);
}
else if (strcmp(choice, "subtract") == 0)
{
result = first_number - second_number;
printf("The result of %.2lf - %.2lf is %.2lf\n", first_number, second_number, result);
}
else if (strcmp(choice, "multiply") == 0)
{
result = first_number * second_number;
printf("The result of %.2lf * %.2lf is %.2lf\n", first_number, second_number, result);
}
else if (strcmp(choice, "divide") == 0)
{
if (second_number != 0)
{
result = first_number / second_number;
printf("The result of %.2lf / %.2lf is %.2lf\n", first_number, second_number, result);
}
else
{
printf("Error: Division by zero is not allowed.\n");
}
}
else
{
printf("Invalid choice.\n");
}
return 0;
}
The only difference here, as you might have already noticed, is the division chunk (function?) has an extra ‘else’ clause (aspect? micro-function? Somebody help.) that bans dividing by 0 for obvious reasons.
Then it returns 0; if all is well and good at the end, and that’s my overly-complicated, verbose calculator that I tried to create with as little help as possible (I needed a lot of help).
Thanks for reading.
Judson