### 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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include 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://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.
 ``12345678910111213141516171819202122232425262728293031`` ``````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?
 ``12345678910`` ``````{ 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?
 ``12345678910`` ``````{ 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?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122`` ``````#include 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.