### help

write a program that uses a witch statement to create a menu for a calculating machine. the program will demonstrate the option that you take and ask you to enter a value and then perform the operation. display a running total before you perform the next task. the calculating machine only supports adding,subtracting and multiplying . you will have the program quite when you enter a pound sign '#'. when you enter a 'Q' call a function that displays the message " tahnk you for using our calculator!"
I added some extra stuff here and there to make a challenge for myself too. But look through it and see if you can get a hang of it.

Feel free to ask a question if you want to understand something in the code I've done ;)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include #include #include using namespace std; long sum = 0; char option; int addition(int num1, int num2){ return num1 + num2; } int substraction(int num1, int num2){ return num1 - num2; } int multiplying(int num1, int num2){ return num1*num2; } void timer(unsigned short i){ clock_t delay = i*CLOCKS_PER_SEC; clock_t start = clock(); while(clock() - start < delay); } void menu(){ int num1, num2; do{ system("cls"); cout << "WELCOME TO THE MENU!\n\nEnter 'A' for addition.\nEnter 'S' for substraction.\nEnter 'M' for multiplying.\nEnter '#' to quit.\nEnter 'Q' for a thankyou message.\n"; cout << "\nOption: "; cin.get(option).get(); if( ! (option == 'A' || option == 'S' || option == 'M' || option == '#' || option == 'Q') ){ system("cls"); cout.put('\a'); cout << "You didn't enter a valid option!\nProgram terminating.\n"; exit(EXIT_FAILURE); } else if(option == '#'){ system("cls"); cout << "Quiting program...\n"; exit(EXIT_FAILURE); } else if(option == 'Q'){ system("cls"); cout << "Thankyou for using our calculator!\n\n"; timer(5); } }while(option == 'Q'); cout << endl; cout << "Enter number 1: "; (cin >> num1).get(); cout << "Enter number 2: "; (cin >> num2).get(); switch(option){ case 'A': cout << endl << num1 << " added to " << num2 << " = " << addition(num1,num2); sum += addition(num1,num2); break; case 'S': cout << endl << num1 << " substracted by " << num2 << " = " << substraction(num1,num2); sum += substraction(num1,num2); break; case 'M': cout << endl << num1 << " multiplyed with " << num2 << " = " << multiplying(num1,num2); sum += multiplying(num1,num2); } cout << "\nThe current total = " << sum << endl << endl; system("pause"); } main(){ system("title Calculator"); while(option != '#') menu(); } ``````
ok where you have the
}while(option =='Q') {
i added a loop and in the beginning a i added a void and he told me i couldnt do that
Have you looked into functions and loops in c++?

I used the do-while loop in the void function. It's a chained loop.

Everything I put between do and while will be executed atleast once.

Example:

do{

cout << "Hello, world!" << endl;

}while( 1 < 2);

This is an infinite loop because there is no update expression and 1 < 2 is a true expression

Anyways, read the book: "C++ Primer 5th edition". It helped me ALOT, and answered all the questions I had. I am currently still reading it, I am at page 300.
But beware, it's a 1200 page book. Still, I highly recommend reading it if you want to know the syntax of C++ thoroughly.
Last edited on
yeah i do
Topic archived. No new replies allowed.