Trouble looping a segment of code

I am a novice at c++ and I am having trouble making a loop in my calculator. What I am attempting to do is once I ask for the two numbers they would be asked if these are the two numbers they would like to perform the mathematical operation with. I am not really sure which loop to use, I think i should use the For loop but i keep getting errors in my syntax.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

  cout << "Welcome to my caculator! Please enter the first number you wish to caculate!" << endl;
    int a;
    cin >> a;
    //// This ask's the user for the first number

    cout << "This is the number you entered! " << a << endl;
    //// This displays the first number you entered!

    cout << "Enter the second number you wish to enter!" << endl;
    int b;
    cin >> b;
    ///// This ask's the user for the second number

    cout << "This is the second number you entered! " << b << endl;
    ///// This displays the second number you entered!


I am pretty sure this is the section you have to loop. Any help would be much appreciated, thank you!

P.S. Something that was also bothering me in my calculator was that you can't get decimals or go negative in the calculator. Is this because c++ isn't arithmetical?
You want to use the do-while loop. You do your operations and such, ask the questions etc. If they answer yes to the question - if these are the two numbers they would like to perform the mathematical operation with - You do the mathematical operation. If they answer no, you want to loop around everything so it starts over and they can give you two new numbers.

There are 3 kinds of loops. For-loops, while loops and do-while loops.

I would recommend looking up info about each of them on the googles, that's the best way to learn, and there is tons of tutorials on this website included. I also love watching youtube videos, I'll recommend two playlists for you -

https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83
https://www.youtube.com/playlist?list=PL2DD6A625AD033D36

There's videos of pretty much everything here, including all the loops.

Get started and try it out yourself, if it gets sticky please post your updated code and we will help out :) Goodluck!

Edit: Don't forget that you will also need an if-statement to ask if they want to do the mathematical operation, watch the videos on that too :D
Last edited on
Thank you very much, I finally added the feature and also thanks for the playlist, I'll be sure to check them out. If your interested here is all the code for the calculator, in a separate post ill ask about the arithmetical problem.

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
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
using namespace std;

///// The four first functions you see right now are the mathematical operations
///// variables a and b are used to store the variables for the two numbers you would like to peform the operation with
//// The variable m and n are used for getting return values from a function that performs the operation
//// The variable i is used for which operation they want to do
//// The variable k is used to make sure that the user has inputted the right numbers

int subtraction (int a, int b)
{
  int m;
  m=a-b;
  return m;
}

int addition (int a, int b)
{
  int m;
  m=a+b;
  return m;
}

int mulitplication (int a, int b)
{
  int m;
  m=a*b;
  return m;
}

int division (int a, int b)
{
    int m;
    m=a/b;
    return m;
}

int main()
{

    cout << "Welcome to my calculator!" << endl;
    int k = 10;
    int a;
    int b;

    do {
    cout << "Please enter the first number you wish to caculate!" << endl;
    cin >> a;
    //// This ask's the user for the first number

    cout << "This is the number you entered! " << a << endl;
    //// This displays the first number you entered!

    cout << "Enter the second number you wish to enter!" << endl;
    cin >> b;
    ///// This ask's the user for the second number

    cout << "This is the second number you entered! " << b << endl;
    ///// This displays the second number you entered!

    cout << "Are these the numbers you would like to perform the operation with? If Yes Enter a number higher than 10 unless enter a number less than 10" << endl;
    cin >> k;
    } while (k < 10);

    cout << "What would you like to do with these numbers?" << endl;
    cout << "For Addition please enter 1!" << endl;
    cout << "For Subtraction please enter 2!" << endl;
    cout << "For Mulitplication please enter 3!" << endl;
    cout << "For Division please enter 4!" << endl;
    int i;
    cin >> i;
    ///// Prompts the user as to which mathematical operation they want to do

    if (i==1) {
    int n;
    n = addition (a,b);
    cout << "The answer is " << n;
    }
    //// This performs the addition operation

    if (i==2) {
    int n;
    n = subtraction (a,b);
    cout << "The answer is " << n;
    }
    //// This performs the subtraction operation

    if (i==3) {
    int n;
    n = mulitplication (a,b);
    cout << "The answer is " << n;
    }
    //// This performs the multiplcation operation

    if (i==4) {
    int n;
    n = division (a,b);
    cout << "The answer is " << n;
    }
    //// This performs the division operation


}
Topic archived. No new replies allowed.