Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
hi people I don't know if this is the right place, but I have a homeork assignment to list all the multiples of a number up to 10000 and seperate them with a comma except the last number should NOT have a comma after it. I have come pretty close to doing this but the last 2 numbers are messing up I don't know what I am doing wrong.
here is the code





#include <iostream>
using namespace std;
int main () {

int number;

cout << "enter a number" << endl;

cin >> number;
int count = 1;

while ( number < 10000 )

{
cout << number ;
cout << ",";
cout << " ";
number = number + number / count;
count++;

if (( number / count ) > 10000 - number)

cout << number;
}

return 0;
}
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
I ran your code and it looks alright.

Using 5 as your number... The last 5 numbers looked like this

9980, 9985, 9990, 9995, 10000


If it still looks odd in your output...

Try this code

Code:
int number, multiple = 0;

cout << "enter a number" << endl;

cin >> number;

while ( multiple < 10000 )
	
{
	cout << (multiple += number);
	
	if ( multiple < 10000)
		cout << ", ";
}
return 0;
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Inside your while loop, you need this logic:

1) print the current multiple
2) bump the multiple for the next loop
3) If the new multiple is < 10000, print the comma/blank

Right now, your logic assumes it is OK to print the ", ". If the number entered is 9000, the comma/blank prints out.

Todd
 

cruzrojas

macrumors member
Mar 26, 2007
67
0
USA
#include <iostream>
using namespace std;
int main () {

int number;

cout << "enter a number" << endl;

cin >> number;
int count = 1;

while ( number < 10000 )

{
cout << number ;
cout << ",";
cout << " ";
number = number + number / count;
count++;

if (( number / count ) > 10000 - number)

cout << number;
}

return 0;
}

The way you are adding to number and to count on each iteration makes this code more complicated than it should be. Start by noting that you used number/count, but this ratio will always be N (the user input). Substitute that into your if statement and you will see that it is equivalent to

if(N(count+1)>10000)
cout << number;

this should make your error pretty clear. This if statements says that if the next multiple of N is bigger than 10,000 the current multiple (count*N) will be printed. But then you go into the while loop and print it again.
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
I
If it still looks odd in your output...

Try this code

Code:
int number, multiple = 0;

cout << "enter a number" << endl;

cin >> number;

while ( multiple < 10000 )
	
{
	cout << (multiple += number);
	
	if ( multiple < 10000)
		cout << ", ";
}
return 0;

This code goes to the multiple pas 10000 like if i put in 99 i get 10098 as my last number not 9999 but I actually do not fully understand this code, multiple is never incremented so wouldn't it always stay at 0?....and how does multiple interact with number to produce multiples?
 

ebel3003

macrumors 6502a
Jun 20, 2007
630
0
"The Google"
It's been a little while since I've done any sort of C++ coding, and I know it may be a bit sloppy, but here's a bit of code I wrote up which seems to work how you would like:

Code:
#include <iostream>

using namespace std;

int main () {
	int number;
	int i;
	
	cout << "Please enter a number: ";
	cin >> number;
	
	cout << "\nThe multiples of this number are: ";
	
	for (i = 0; i * number < 10000 || i * number == 10000; i++) {
		cout << number * i << ", ";
	}
}

Let me know if you would like me to explain any of it, but it should be simple enough.

I've taken several courses on the language, so should you need any help let me know and I'll do my best to give you a hand.
 

chris200x9

macrumors 6502a
Original poster
Jun 3, 2006
906
0
It's been a little while since I've done any sort of C++ coding, and I know it may be a bit sloppy, but here's a bit of code I wrote up which seems to work how you would like:

Code:
#include <iostream>

using namespace std;

int main () {
	int number;
	int i;
	
	cout << "Please enter a number: ";
	cin >> number;
	
	cout << "\nThe multiples of this number are: ";
	
	for (i = 0; i * number < 10000 || i * number == 10000; i++) {
		cout << number * i << ", ";
	}
}

Let me know if you would like me to explain any of it, but it should be simple enough.

I've taken several courses on the language, so should you need any help let me know and I'll do my best to give you a hand.

thanx! yea no explaination necassary i got it
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
This code goes to the multiple pas 10000 like if i put in 99 i get 10098 as my last number not 9999 but I actually do not fully understand this code, multiple is never incremented so wouldn't it always stay at 0?....and how does multiple interact with number to produce multiples?

Oops, I suppose I didn't test it enough.

The line

Code:
cout << (multiple += number);

is where multiple interacts with number.

This is equivalent of saying

Code:
multiple = multiple + number;
cout << multiple;
 

madmoose

macrumors newbie
Oct 5, 2007
5
0
I prefer to do the separator-logic first. I find it a lot easier to read.

Code:
#include <iostream>

int main()
{
    int number;

    std::cout << "Enter a number:" << std::endl;
    std::cin >> number;
        
    if (number < 1)
        std::cerr << "Number must be positive!" << std::endl;

    for (int i = 1; i*number < 10000; ++i)
    {
        if (i > 1)
            std::cout << ", ";

        std::cout << i*number;
    }

    std::cout << std::endl;
}
 

ebel3003

macrumors 6502a
Jun 20, 2007
630
0
"The Google"
I don't particularly like working without std namespace with such a small and simple program, all of that extra code and chance of syntax error for nothing.

Also, I believe your code has one flaw: if the ending number equals 10,000, it will not be sent out to the console. I don't have a compiler handy to test it though.

Mine isn't perfect either, though. I didn't check for a negative number.
 

madmoose

macrumors newbie
Oct 5, 2007
5
0
Also, I believe your code has one flaw: if the ending number equals 10,000, it will not be sent out to the console. I don't have a compiler handy to test it though.

The specification said "up to" not "up to and including" ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.