View Full Version : c++ help
chris200x9
Oct 1, 2007, 09:22 PM
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
Oct 1, 2007, 10:40 PM
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
int number, multiple = 0;
cout << "enter a number" << endl;
cin >> number;
while ( multiple < 10000 )
{
cout << (multiple += number);
if ( multiple < 10000)
cout << ", ";
}
return 0;
toddburch
Oct 1, 2007, 10:56 PM
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
toddburch
Oct 1, 2007, 10:58 PM
I ran your code and it looks alright.
It's not alright. Enter 210 and see what happens.
Todd
Spike099
Oct 1, 2007, 11:03 PM
It's not alright. Enter 210 and see what happens.
Todd
Ahh right.
cruzrojas
Oct 1, 2007, 11:27 PM
#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
Oct 3, 2007, 06:22 PM
I
If it still looks odd in your output...
Try this 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
Oct 3, 2007, 07:31 PM
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:
#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
Oct 3, 2007, 07:37 PM
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:
#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
Oct 3, 2007, 09:50 PM
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
cout << (multiple += number);
is where multiple interacts with number.
This is equivalent of saying
multiple = multiple + number;
cout << multiple;
madmoose
Oct 5, 2007, 10:27 AM
I prefer to do the separator-logic first. I find it a lot easier to read.
#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
Oct 5, 2007, 10:08 PM
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.
callidus
Oct 6, 2007, 01:16 PM
.
madmoose
Oct 6, 2007, 01:25 PM
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" ;)
ebel3003
Oct 6, 2007, 06:27 PM
The specification said "up to" not "up to and including" ;)
Touche :)
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.