Loop

I (a self-taught beginner, please attempt to keep it simple) am currently having problems creating a loop that will allow my user to choose to return to the beginning of the program or quit.
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
#include <iostream>

using namespace std;

int main()
{
    int j;
    do {float a;
    cout << "+----Welcome to Basic Operations----+\n|       Select Your Operation       |\n";
        cout << "|          1. Addition              |\n|          2. Subtraction           |\n";
        cout << "|          3. Multiplication        |\n|          4. Division              |\n";
        cout << "+-----------------------------------+\n\n";
        cout << "                  ";
        cin >> a;
    do{ if (a > 4 || a < 1){cout << "\nInvalid! Enter valid operation number\n\n";
            cout << "                  ";
            cin >> a;}
        if(a==1){cout << "\n+-----Your Selection: Addition------+\n";
            cout << "|      Enter Your First Number      |\n";
            float b, c, sum;
            cout << "                ";
            cin >> b;
            cout << "|      Enter Your Second Number     |\n";
            cout << "                ";
            cin >> c;
            sum = b + c;
            cout << "|             The Sum is            |\n" << "                " << sum << endl;
            cout << "+-----------------------------------+\n";}
        if(a==2){cout << "\n+----Your Selection: Subtraction----+\n";
            cout << "|Enter Your Number to Subtract From |\n";
            float d, e, diff;
            cout << "                ";
            cin >> d;
            cout << "|   Enter Your Number to Subtract   |\n";
            cout << "                ";
            cin >> e;
            diff = d - e;
            cout << "|         The Difference is         |\n" << "                " << diff << endl;
            cout << "+-----------------------------------+\n";}
        if(a==3){cout << "Enter a number to multiply. \n";
            float f, g, prodct;
            cin >> f;
            cout << "Enter a second number. \n";
            cin >> g;
            prodct = f * g;
            cout << "The product is " << prodct << endl;}
        if(a==4){cout << "Enter a number to divide from. \n";
            float h, i, quotent;
            cin >> h;
            cout << "Enter a second number. \n";
            cin >> i;
        do{ if(i==0){cout << "Error. Cannot divide by 0. Re-enter second number. \n";
                cin >> i;}}
                while (i==0);
            quotent = h / i;
            if(i not_eq 0){cout << "The quotient is " << quotent << "." << endl;}}}
            while (a > 4 || a < 1);
        cout << "\n   Continue Using Basic Operations?\n";
        cout << "                (Y/N)\n";
        cout << "                  ";
        cin >> j;}
        while (j=='y' || j=='Y');
    return 0;}

I have not yet finished designing the interface for a couple of the operations, but right now i am hung up on trying to return to the beginning. I believe it is because the j was defined inside do and isn't carried out of the do statement for while. How would I be able to fix this?
Just on a side note you should really work on formatting at some point, not the worst ever but it could look much cleaner with more spaces/better brace placement etc.

while (j=='y' || j=='Y');
j should be a char if you are checking letters, not an int as you have it defined.

if(i not_eq 0) "not_eq" should be !=

Everything else seems ok to me.
Thank you James. I do not know how to clean up my formatting. Since I am teaching myself, I just do it so I know how to read it. If you could clarify on how to clean my formatting (where to create new lines, placement of spaces, etc.), that'd be great. Also, why should i use != instead of not_eq? Do they not do the same thing?
Also, why should i use != instead of not_eq? Do they not do the same thing?

They both do the same thing. The latter is rarely used as far as I can tell.


If you could clarify on how to clean my formatting (where to create new lines, placement of spaces, etc.)...

Here is are some pages you can take a look at.
http://stackoverflow.com/questions/2434213/links-to-official-style-guides
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Style_Conventions#Coding_style_conventions
http://www.cplusplus.com/forum/lounge/69683/
Last edited on
Wow I have never seen "not_eq" used ever, I must be living in a cave (credit to Daleth for bringing this to my attention). Still != is much more commonly used. Doesn't help VS doesn't support these by default to my knowledge.

Formatting is a fairly subjective thing people just learn over time with programming, but I would read as much example code as you can and see how other people are doing it.

In general:
Any kind of check should be on it's own line (if/else if statements). This includes do/while statements as well. Personally I like braces to line up directly and be on lines by themselves but you can find 1000 arguments over this if you search around. I always put new lines after variable declarations and stuff like that.

Just as an example I would write the first part of your code like this, but change it to whatever feels right to you/stays easy to read.
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
int main()
{
    char j;

    do 
    {
	cout << "+----Welcome to Basic Operations----+\n|       Select Your Operation       |\n";
        cout << "|          1. Addition              |\n|          2. Subtraction           |\n";
        cout << "|          3. Multiplication        |\n|          4. Division              |\n";
        cout << "+-----------------------------------+\n\n";
        cout << "                  ";

	float a;
        cin >> a;

        do
	{
		if (a > 4 || a < 1)
		{
			cout << "\nInvalid! Enter valid operation number\n\n";
			cout << "                  ";
			cin >> a;
		}

		if(a==1)
		{
			cout << "\n+-----Your Selection: Addition------+\n";
			cout << "|      Enter Your First Number      |\n";
			float b, c, sum;
			// etc
		}
Last edited on
I have thought about doing that, but by doing that with my code, closer the the end it will be extremely far over due to the indentations.
http://www.cplusplus.com/reference/iomanip/?kw=iomanip

http://www.cplusplus.com/reference/iomanip/setw/
http://www.cplusplus.com/reference/iomanip/setfill/

These are very useful when you've got to print a long line of the same character. Using these in your code will reduce the length of your code lines, too.
Which do you think is easier to read?
1
2
3
4
5
6
7
8
9
10
{
    char j;

    do
    {
        cout << "+----Welcome to Basic Operations----+\n|       Select Your Operation       |\n"
             << "|          1. Addition              |\n|          2. Subtraction           |\n"
             << "|          3. Multiplication        |\n|          4. Division              |\n"
             << "+-----------------------------------+\n\n"
             << "                  ";

Or would you rather see cout in front of each line?
1
2
3
4
5
6
7
8
9
10
{
    char j;

    do
    {
        cout << "+----Welcome to Basic Operations----+\n|       Select Your Operation       |\n";
        cout << "|          1. Addition              |\n|          2. Subtraction           |\n";
        cout << "|          3. Multiplication        |\n|          4. Division              |\n";
        cout << "+-----------------------------------+\n\n";
        cout << "                  ";
Last edited on
I went ahead and finished up the code with a new formatting. Does this read better?
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>

using namespace std;

int main()
{
    char j;

    do
    {
        cout << "+----Welcome to Basic Operations----+\n|       Select Your Operation       |\n"
             << "|          1. Addition              |\n|          2. Subtraction           |\n"
             << "|          3. Multiplication        |\n|          4. Division              |\n"
             << "+-----------------------------------+\n\n"
             << "                  ";

            float a;
            cin >> a;

            do
            {
                if (a > 4 || a < 1)
                {
                    cout << "\nInvalid! Enter valid operation number\n\n"
                         << "                  ";
                    cin >> a;
                }

                if(a==1)
                {
                    cout << "\n+-----Your Selection: Addition------+\n"
                         << "|      Enter Your Number to Add     |\n";
                    float b, c, sum;
                    cout << "                ";
                    cin >> b;
                    cout << "|      Enter Your Second Number     |\n"
                         << "                ";
                    cin >> c;
                    sum = b + c;
                    cout << "|             The Sum is            |\n" << "                " << sum << endl
                         << "+-----------------------------------+\n";
                }

                if(a==2)
                {
                    cout << "\n+----Your Selection: Subtraction----+\n"
                         << "|Enter Your Number to Subtract From |\n";
                    float d, e, diff;
                    cout << "                ";
                    cin >> d;
                    cout << "|   Enter Your Number to Subtract   |\n"
                         << "                ";
                    cin >> e;
                    diff = d - e;
                    cout << "|         The Difference is         |\n" << "                " << diff << endl
                         << "+-----------------------------------+\n";
                }

                if(a==3)
                {
                    cout << "\n+--Your Selection: Multiplication--+\n"
                         << "|   Enter Your Number To Multiply  |\n";
                    float f, g, prodct;
                    cout << "                ";
                    cin >> f;
                    cout << "|      Enter Your Second Number     |\n"
                         << "                ";
                    cin >> g;
                    prodct = f * g;
                    cout << "|           The Product is          |\n" << "                " << prodct << endl
                         << "+-----------------------------------+\n";
                }

                if(a==4)
                {
                    cout << "\n+------Your Selection: Division-----+\n"
                         << "|  Enter Your Number to Divide From |\n";
                    float h, i, quotent;
                    cout << "                ";
                    cin >> h;
                    cout << "|      Enter Your Second Number     |\n"
                         << "                ";
                    cin >> i;

                    do
                    {
                        if(i==0)
                        {
                            cout << "|               Error               |"
                                 << "|         Cannot Divide by 0        |"
                                 << "|       Re-Enter Second Number      |\n";
                            cin >> i;
                        }

                    }

                    while (i==0);

                    quotent = h / i;
                    if(i != 0)
                    {
                        cout << "|          The Quotient is          |\n" << "                " << quotent << endl
                             << "+-----------------------------------+\n";
                    }

                }

            }

            while (a > 4 || a < 1);

        cout << "\n   Continue Using Basic Operations?\n"
             << "                (Y/N)\n"
             << "                  ";
        cin >> j;
        cout << "\n";
        }

    while (j=='y' || j=='Y');

    return 0;
}
Last edited on
Definitely better than it was, I would still include more new lines in your choices between the cout/cin statements etc but that's all just opinion anyway.

if you wanted another thing to work on you could put each choice into a separate function and just call them from main making it more organized. (Assuming you have started on functions, if you haven't you can start now!)
I have not yet started functions. I am unsure what they even do yet. Today will most likely be my first day looking at them and if I have any questions, I will come back to this forum topic to ask you.
After looking at functions, I don't understand how you think it will help organize my program. It will just make my main look much nicer, but I will have just added lines of code to place my already working program into functions that are not needed. I do not see any gain from using functions in this program other than as practice.
I don't understand how you think it will help organize my program

Several reasons for using functions:

1) Anything you do more than once in your code, should be in a function. Consider your prompting for two numbers. You do that in four separate places in your code. Why not call one function that asks the user to enter two numbers? That way, you write the code once rather than 4 times. You only need to debug the function once, rather than four separate times.
A trivial example, but you should get the idea.

2) Code isolation. By creating four separate functions as James2250 suggested, you isolate your logic for each operation. Much less chance of one operation interferring with a different operation.

3) Organization. By creating a function called "add_two_numbers", it's very easy for someone else to follow your program. It's immediately clear what the function does by it's name. Sure, you know what your program does at this point, but as it grows, you don't want to keep cramming everything into main.

As you advance in C++, use of functions will become mandatory, such as with classes.
Last edited on
After you have explained it now, I see the point of it. I had just thought of using it for the organization and not just for having them enter numbers. I feel blind as a bat to have not thought of it that way. Thank you for the explanation.
Topic archived. No new replies allowed.