You are using a version without Ads of this website. Please, consider donating:

### understanding loops

Hello everybody!

I am really really new to C++ and programming in general. I'm just turning 2 weeks into it!
I'm going to be honest here. The questions I'll be posting are part of my assignment for an online class I'm taking. I'm not asking for a direct solution so I can simply copy - paste. I'm looking for tips on how to do my assignment and how to understand what's going on with my program.
So, my assignment has 2 tasks. I will post just the first one for now. I'm still not sure if to even try my second one. I've spent over a week trying to figure out the 1st one on my own and it really didn't work out at all. And the 1st task is like kindergarten compared to the 2nd.

So I have to write a program that:
1. Asks the user to type in a number higher than 0 (let's call it A) and on a different line to type in a number higher than A (let's call it B).
2. The program then has to take each number from A to B and apply a series of operations to each of them repetitively until each number we started from becomes 0.
3. The program has to count how many times it is necessary to repeat the operations to get to 0.
4. The program has to have as output ONLY the number it started from, an arrow (->) and the number of times it had to go through the loop.

For example:
A user chooses A to be 5 and B, 7.
The program will take the numbers from 5 to 7, so 5, 6, 7 and apply:
a. if the number is divisible by 3, it will add 4 to the number;
b. if the number is divisible by 4, it will divide the number by 2;
c. if it's neither divisible by 3, nor 4, it will subtract 1;
Lets take 5. It is not divisible by either 3 or 4 so it falls under the c, therefore we will subtract 1. So 5 - 1 = 4. Now 4 is divisible by 4 so it falls under b; therefore we will divide 4 by 2, getting 2. 2 falls under c so we subtract 1, reaching 1. And 1 falls under c, we subtract 1 and tada! 0.

So 5 goes through 4, 2, 1, 0, requiring a number of 4 repetitions of the if statements. The only thing the program has to show the user is 5 -> 4. 5 being the number we started from and 4 the number of repetitions. So the end result has to look like:

5 -> 4
6 -> 10
7 -> 11

Now for my questions:
1. How do I make the program take every number from A to B and apply the if statements loop to each of them?
I don't know if I should even post the code I tried. It's wrong on so many levels. And now even my if statements are giving me errors. It tells me 'else without a previous if'. But I do have an 'if' before the else.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include using namespace std; int main() { int start; do { cout << "Please type a number higher than 0: "; cin >> start; } while(start <= 0); int stop; do { cout << "Please type a number higher than " << start << ": " << endl; cin >> stop; } while(stop <= start); int number, count = 0; do{ for (; number != 0; ++count) { while(number != 0){ if (number % 3 == 0){ number = number + 4; }; else if (number % 4 == 0){ number = number / 2; }; else{ number = number - 1; }; }; }; cout << number << '->' << count << endl; } while (number >= start and number <= stop); return 0; } ``````

2. I have managed to apply the operations to a single number... too much talk, I will just show you:
 ``12345678910111213141516171819202122232425262728293031`` `````` #include using namespace std; int main() { int number, count = 0; do { cout << "Please type a number higher than 0: "; cin >> number; } while(number <= 0); for (; number != 0; ++count) { while(number != 0){ if (number % 3 == 0){ number = number + 4; } else if (number % 4 == 0){ number = number / 2; } else{ number = number - 1; }; }; }; cout << number << '->' << count << endl; return 0; } ``````

For some reason now I get a 'multi-character character constant' error message for the last 'cout' command. But the first time I typed it it work but the result was, taking the above example of 5:
0 -> 4
5 -> 4

I understand why. Because in my code i have the same variable named 'number'. But I have no clue how to tell it to take the initial number.

I hope you'll be kind enough to guide me. I feel completely lost in the dark. Should I really be capable of solving this after just 3 weeks of C++? I've only learned about variables, expressions, if statements and loops so far. And this assignment if for the 'Loops' chapter.
Question 1 :
 ``123456789101112131415`` ``````while(number != 0) { if (number % 3 == 0) { number = number + 4; }; else if (number % 4 == 0) { number = number / 2; }; else { number = number - 1; }; };``````

 It tells me 'else without a previous if'. But I do have an 'if' before the else.

The problem is that, you have extra semi-colons (;) after curly braces. That's redundant. You'll only need to remove them :

 ``123456789101112131415`` ``````while(number != 0) { if (number % 3 == 0) { number = number + 4; } else if (number % 4 == 0) { number = number / 2; } else { number = number - 1; } }``````
Last edited on
Oh, thanks! I can't believe I missed that! Rookie mistake, huh?!

Thanks a lot!
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: