Strange breakline appears in my program?

Jun 3, 2013 at 4:05pm
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.


I've solved the problem and it works but if the input is a large number, instead of no breaklines in every string printed, there is. Yes, it's ok to ignore it but I dont want to let this be one of my biggest problem which I can't figure out in the future. So anyone knows why?

here id the code

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
#include <iostream>

using namespace std;

int main()
{
    unsigned short int tryAgain;
    double sum = 0;
    unsigned long int userInput;
    do
    {
        do
        {
            cout << "Enter the number: ";
            cin >> userInput;
        }while(userInput < 1);
        for(int i = 1; i < userInput; i++)
        {
            if(i%3 == 0)
            {
                sum += i;
                cout << i << " is multiple of 3. It's now added to variable sum which is now " << sum << endl;
            };
            if(i%5 == 0)
            {
                sum += i;
                cout << i << " is multiple of 5. It's now added to variable sum which is now " << sum << endl;
            };
        };
        cout << "The sum of its multiple is: " << sum << endl;
        do
        {
            cout << "Wanna try again?" << endl;
            cout << "1 - Yes" << endl << "2 - No" << endl;
            cin >> tryAgain;
        }while(tryAgain != 1 && tryAgain != 2);
        if(tryAgain == 1) sum = 0;
    }while(tryAgain == 1);
};
Last edited on Jun 3, 2013 at 4:12pm
Jun 3, 2013 at 4:08pm
try to enter something like 500 and string will be printed as expected but if the program receives an input like 30000, there is something wrong.
Jun 3, 2013 at 4:16pm
jasonrobertz357 wrote:
there is something wrong.
What do you mean? What makes you think something's wrong? Does it stop breathing? Does it explode? Does it install a virus? You're not giving us much to go on here.

Also, you have a lot of semicolons after closing braces, like on lines 23, 28, 29, and 39 - what are those for? They do not do what you expect them to, and you're not supposed to have them.
Last edited on Jun 3, 2013 at 4:17pm
Jun 3, 2013 at 4:17pm
this is what I said

Yes, it's ok to ignore it but I dont want to let this be one of my biggest problem which I can't figure out in the future. So anyone knows why?


which also means that I wont let this simple problem becomes a big problem.
Last edited on Jun 3, 2013 at 4:19pm
Jun 3, 2013 at 4:23pm
Could you explain what this means?
jasonrobertz357 wrote:
if the input is a large number, instead of no breaklines in every string printed, there is.
What do you mean by "breaklines in every string printed"? What is a "breakline"?
Jun 3, 2013 at 4:31pm
I cant explain, but this example will help:

strings without breakline
Hello World!Hello World!

strings with 1 breakline
1
2
Hello World!
Hello World!


strings with 2 breakline
1
2
3
Hello World!

Hello World!


Sorry for that, before I came into programming, I'm doing stuffs with html and it's called breakline ^_^
Jun 3, 2013 at 4:36pm
oh by the way, if you noticed that the problem is to know what is the multiple of 3 or 5 below 1000 and the code that i posted above, it's just a version of mine which you can find out the multiples of the number the user entered, not just 1000
Jun 3, 2013 at 5:03pm
At a guess, your output lines are exceeding the width of the console you're running in. Not a whole lot you can do about that but shorten them.

Multiples of both 3 and 5 (15, 30, 45, etc.) are added to the sum twice. That probably isn't what you intended.
Jun 3, 2013 at 5:05pm
Ah, the term you are looking for is "line break".

I don't see any line breaks:
http://ideone.com/qN5Ccy

If you're running the program in the console, note that when the text is long and reaches the edge of the screen, it inserts a line break for you (you have no choice). Then when your program puts its own line break, it looks like it put two - don't worry, this is just the console, your program is fine.
Jun 3, 2013 at 5:15pm
@cire and LB

crystal clear ^_^

That probably isn't what you intended.


nope, I do. But if the problem doesnt, its ok, Im just practicing so it doesnt matter if its "3 or 5 or 3 and 5" or "3 or 5".

by the way thanks again for helping me
Jun 3, 2013 at 5:18pm
nope, I do.


Alrighty. I thought I remembered someone directing you to the Project Euler page previously and this looked like an attempt to solve the first problem. For the purposes of that problem, doing so is incorrect.
Topic archived. No new replies allowed.