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 ;)

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <ctime>
#include <cstdlib>
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.