Help with simple code.

Hello, working through Bjarne Stroustrup: Programming, Principles and Practice using C++. One of the problems in chapter 6 is to simply open a program he has already made and run it to see what it looks like.
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<conio.h>
using namespace std;
int add(int a, int b)
{
    cout<<"Please enter a number"<<endl;
    cin>>a;
    cout<<"Please enter another number to add\n";
    cin>>b;
    int c = a + b;
    cout<<"Output: "<<a<<" + " <<b<< " = "<<c;
    return c;
}
int sub(int a, int b)
{
    cout<<"Please enter a number"<<endl;
    cin>>a;
    cout<<"Please enter another number to subtract\n";
    cin>>b;
    int c = a - b;
    cout<<"Output: "<<a<<" - " <<b<< " = "<<c;
    return c;
}
int mul(int a, int b)
{
    cout<<"Please enter a number"<<endl;
    cin>>a;
    cout<<"Please enter another number to multiply\n";
    cin>>b;
    int c = a * b;
    cout<<"Output: "<<a<<" * " <<b<< " = "<<c;
    return c;
}
int devide(int a, int b)
{
    cout<<"Please enter a number"<<endl;
    cin>>a;
    cout<<"Please enter another number to devide\n";
    cin>>b;
    int c = a / b;
    cout<<"Output: "<<a<<" / " <<b<< " = "<<c;
    return c;
}
int input(int option,int a,int b)
{
    while(1)
    {
cout<<endl;
cout<<"-----------------------------------------------------\n"<<endl;
    cout<<"Please select the option you want to perform"<<endl;
    cout<<"1. Addition"<<endl<<"2. Subtraction"<<endl<<"3. Multiplication\n"<<"4. Devision\n"<<"5. Exit Calculator\n";
    cout<<"\nPlease enter your option:";
    cin >>option;
    switch(option){
case 1:
    cout<<"You have selected Addition\n";
    add(a,b);
    break;
case 2:
    cout<<"You have selected Subtraction\n";
    sub(a,b);
    break;
case 3:
    cout<<"You have selected Multiplication\n";
    mul(a,b);
    break;
case 4:
    cout<<"You have selected Devision\n";
    devide(a,b);
    break;
case 5:
    cout<<"Exiting...";
    break;
    default:
    cout<<"Please select a valid option";
}
if (option == 5)
{
    break;
}
    }
}
int main()
{
cout<<"Caculator";
int option,a,b;
input(option,a,b);
getch();
return 0;
}


Error: Unhandled exception at 0x74efc41f in hello_world.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0021fc48..

I've looked through all the code and it looks like it should work to me, and since the following pages aren't about fixing the errors in the code, and it tells me to run it and not try and get it to run, I'm thinking it's supposed to run fine, suggestions? Thanks in advance.
Last edited on
I have not found such program in this book.
It refers you to a file he had already made calculator00.cpp. Page #202
I have a Russian edition of the book. I looked through the chapter #6 but there is another code of the calculater. For example section 6.4 named Grammar (or grammaes) descibes the calculator grammar. It is strange that the autor could place such code where parameters of the functions are not used.
Yeah, I don't know what to do, I've been learning for 2 weeks now and I'm afraid I wont learn the stuff I need if I don't follow along.
Your function input returns nothing though it is declared as returning an object of type int.
Hi GMPoison, i'm also a beginner in c++, it's my first programming language.

I would like to recommend you to go true the new boston tutorials, step by step, you will learn a lot. http://www.youtube.com/course?list=ECAE85DE8440AA6B83

Once you get a general idea around all the c++ stuff, reading that book will be alot easier and you will learn faster.

It's just a suggestion, but i have learned a lot from those tutorials and once i've started reading books i fully understooded the content of it.
Topic archived. No new replies allowed.