Hey fellas, brand new programmer here! I'm excited to learn and I'm having a blast. Would like some advice from the experienced dudes here. I'm trying to make a program that displays all the multiples of three under 1000 only. I'm close, but something is iffy:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x, i, out;
x = 3;
out = 0;
i = 0;
while (out <= 1000) {
i++;
out= i *= x;
cout << out << endl; }
system ("pause") ;
return EXIT_SUCCESS;
}
What I see in the console is this:
3
12
39
120
363
1092
Press any key to continue......
Obviously I'm on the right page, thing is, they're all multiples of 3, but where's 6, 9, 21, etc...? I think it's my logic more than anything but feel free to critique me about anything.
This is a good time to use a for loop. If you don't know how to use them, then here's a link: http://www.cplusplus.com/doc/tutorial/control/
A hint:
You could do this using only one variable.
Also, avoid using system("pause"), use something else such as cin.get().
You don't need main to accept arguments in this program. Just use int main()
Finally, please use [code][/code] tags.
Edit: See if you can get it down to 9-12 lines. This is about how long the proper solution will be.
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main(int argc, char *argv[])
{
int x, i, out;
x = 3;
out = 0;
i = 0;
while (out <= 1000) {
i++;
out= i *= x;
cout << out << endl; }
system ("pause") ;
return EXIT_SUCCESS;
}
@Zaita:
Why did you just post a solution like that? To you and me it may be exetremely trivial, but to the OP it is something he still needs to learn.
It's probably not a big deal with such a small assignment though, and it should be i < 1000, the OP said numbers UNDER 1000.
Yeah, guess I didn't specify positive integers only. And as far a Zaita's answer goes, it proves it's all in my logic rather than my (small amount of) knowledge of C++. All this makes total sense now, I don't understand why I made something so simple so complicated lol. Basic concept is, if you start at 3 and just keep adding three every loop, all of those numbers each loop will be multiples of 3 DUH!!! haha Thanks everyone for taking my question seriously, it means a lot!
the console you have probably only lets you see so many lines. and the ones above 105 it doesnt show, if you edit the way they print out (maybe 2 per line rather than 1 per line) it should fix it.
you could always scrap the endl at the end and put
1 2 3
cout << i << " ";
that will print out 3 6 9 12 15 etc all on one line, but for class purposes the teacher should be fine with your solution, since based on the information you gave the code is right.
@Browni3141: Typically I'm against posting whole solutions to assignments etc, but this doesn't really qualify as the answer is a basic use of a for-loop. I also wanted to avoid this thread getting polluted with solutions that are not ideal or helpful and over-complicate what has been asked. Danaigo's solution, while still accurate is keeping with the OP's over-complicated approach.
There is no harm in providing a solution when it's a basic use of a single conditional statement or iterative statement.