I have just started a course on C and wanted to test what I have learned so far. I did so by making a small program that tells you how much you should charge for a shot of a spirit you put in.
The code will ask if you want to do a calculation. It will follow up with a question about the name, price and size of a product and respond with the calculation.
I have just learned how to use a Struct and just worked on Arrays. My next step will be to try and implement arrays to store the Structs you made and have the option to return to previous input. How did I do so far and what could I improve on?
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
//structs
typedef struct
{
string name;
float price;
int size;
} Bottles;
//prototypes
Bottles get_newBottle(void);
//main
int main(void)
{
string newBottles;
//Ask user for input, repeat untill answered correctly.
do
{
do
{
newBottles = get_string("Do you want to calculate a new shot y/n? ");
if (strcmp(newBottles, "y") == 0)
{
//Build struct
Bottles newBottle = get_newBottle();
int shotSize = get_int("What is the size of your pour of %s in ML? ", newBottle.name);
int bevCost = get_int("What percentage beverage cost do you want your drink to be? ");
float shotCost = (newBottle.price / newBottle.size) * shotSize;
float bevPrice = ((shotCost / bevCost) * 100) *1.21;
printf("The cost of your shot of %s is %.2f Euro. \n", newBottle.name, shotCost);
printf("At the cost of %.2f you will have to charge %.2f Euro including 21 percent VAT. \n", shotCost, bevPrice);
}
else if(strcmp(newBottles, "n") == 0)
{
printf("ok. doei!\n");
}
}
while (! (strcmp(newBottles, "y") == 0) && ! (strcmp(newBottles, "n") == 0));
}
while (strcmp(newBottles, "y") == 0);
return 0;
}
//extra functions
Bottles get_newBottle(void)
{
string name = get_string("What is the name of your product? ");
float price = get_float("What is the price of your product? ");
int size = get_int("What is the size in ML of your product? ");
Bottles newName = {name, price, size};
return newName;
}
std::string
is a C++ class. \$\endgroup\$get_string
defined? Probably incs50.h
; please show its contents. \$\endgroup\$string
is the awful CS50 typedef ofchar *
. \$\endgroup\$