0

This program is supposed to get the multiples of whatever number the user inputs (x) until it reaches (y). All three loops function correctly but when used together we get get the first two loops but the third loop doesn't output. Also I would want to output each loop's output in a new line. My question is what can I do to make my outputs come out in three separate lines and why isn't my third loop outputting anything.

#include <iostream> #include <string> #include <cstdlib> using namespace std; bool error(const string & msg); int main(){ unsigned x, y; cout << "Give me a number you want the multiples of and give me the max limit: "; cin >> x >> y || error("Input Failure: "); if (x == 0) error("Input can't be 0"); unsigned increment = x; x = 0; while( x < y) { cout << x << ", "; x += increment; if (x > y) break; } cout << endl; // This was the other problem. I kept putting it inside the loop for(int x; x < y; x += increment){ cout << x << ", "; if (x > y) break; } cout << endl; // This was the other problem. I kept putting it inside the loop x = 0; // This is what originally was wrong do{ cout << x << ", "; x += increment; if ( x > y){ break; } }while (x < y); } bool error(const string & msg){ cout <<"Fatal error: " << msg << endl; exit(EXIT_FAILURE); } 
6
  • What's your question? Commented Oct 18, 2015 at 3:49
  • What is the output you're getting? Are there any runtime errors? Commented Oct 18, 2015 at 3:52
  • When I input 5 and 40 i get 0, 5, 10, 15, 20, 25, 30, 35, 0, 5, 10, 15, 20, 25, 30, 35, 40 and I am not receiving any errors. It is strange since when I run the third loop alone I get 0, 5, 10, 15, 20, 25, 30, 35 Commented Oct 18, 2015 at 3:56
  • 1
    the first loop, you set x =0, in the second loop, you defined new x (int x) so the its value will be 0, in the third one, it will continue from where x ended i n the first one. so the third loop will start and print x then finish immediatly. the solution is to set x = 0 before starting the third loop. Commented Oct 18, 2015 at 3:58
  • @OSAMAORABI You comment and nbermudzs helped a lot. I realized I defined x in both the first loops and like you said the third loop x carried on from the second loop I'll edit my code but I put x = 0 before the do loop and it worked fine Commented Oct 18, 2015 at 4:06

3 Answers 3

1

Well, it isn't strange. When you run the third loop alone the variable x is not being modified by the other two loops. In fact, your third loop it is being executed. The first loop prints 0,5, ..., 35, then the second loop prints 0, 5, ..., 35 (note the < in the condition for the for loop) and the 40 is printed by the third loop, then right after it prints it the condition on the while is false since 40 == 40 and the loop ends.

Sign up to request clarification or add additional context in comments.

4 Comments

I see what you're saying it is indeed being executed and I saw that in the first two loops I had x = 0. In the third loop x carried on from the second loop. I put x = 0 before the do loop and it executed perfectly now I just need to know how to make them have their on individual lines. But thank you!
No problem! What do you mean by individual lines? you need the output of each loop in one line? if that's the case just do cout << endl before the start of the second and third loop.
HAHA ughhh I spent at least an hour trying to figure it out. I kept putting it inside of the loop and I would get individual lines for 5 10 15 in a vertical line. Thank you so much I won't ever forget that
hahaha, glad I could help. Please mark either OSABA's or my answer as accepted so no other people unnecessarily try to help :-)
0
 unsigned increment = x; x = 0; while( x < y) { cout << x << ", "; x += increment; if (x > y) break; } for(x = 0; x < y; x += increment){ cout << x << ", "; if (x > y) break; } x=0; do{ cout << x << ", "; x += increment; if ( x > y){ break; } }while (x < y); 

1 Comment

Thank you! I solved it before with your comment but I appreciate the code as well
0

The for loop will not output because you redefine x and do not give it a new value. I am not sure why you use three loops that practically do the same thing unless you are just curious about how they work. For example you could do something like this to output the same thing with different loops:

#include <iostream> using namespace std; int main() { unsigned counter, increment, limit; cout << "Give me a number you want to increment by of and give me the max limit: "; cin >> increment >> limit; //Check to see if the input stream failed if(cin.fail()) { cout << "Fatal Error: Input Failure" << endl; return -1; } if(increment == 0 || limit == 0) { cout << "Input Cannot Be 0" << endl; return -1; } if(limit <= increment) { cout << "Limit <= Increment" << endl; return -1; } cout << "First Loop" << endl; counter = 0; //Set counter to 0 for 1st loop while(counter <= limit) { cout << counter << ", "; counter += increment; } cout << endl; cout << "Second Loop" << endl; //for loop resets counter for 2nd loop for(counter = 0; counter <= limit; counter += increment) { cout << counter << ", "; } cout << endl; cout << "Third Loop" << endl; //Reset counter again for 3rd loop counter = 0; do{ cout << counter << ", "; counter += increment; }while (counter <= limit); return 0; } 

2 Comments

Wow I was late to the game :P
Haha yeah late to the game I had to use all three loops for a homework assignment but I'll take a look at your code just to get inside of other peoples brains and see how they figured it out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.