c++ help

Discussion in 'Mac Programming' started by chris200x9, Oct 1, 2007.

  1. chris200x9 macrumors 6502a

    Joined:
    Jun 3, 2006
    #1
    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;
    }
     
  2. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #2
    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;
    
     
  3. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    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
     
  4. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    It's not alright. Enter 210 and see what happens.

    Todd
     
  5. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #5
    Ahh right.
     
  6. cruzrojas macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #6
    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.
     
  7. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #7
    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?
     
  8. ebel3003 macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #8
    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.
     
  9. chris200x9 thread starter macrumors 6502a

    Joined:
    Jun 3, 2006
    #9
    thanx! yea no explaination necassary i got it
     
  10. Spike099 macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #10
    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;
     
  11. madmoose macrumors newbie

    Joined:
    Oct 5, 2007
    #11
    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;
    }
    
     
  12. ebel3003 macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #12
    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.
     
  13. shawnwich macrumors 6502

    shawnwich

    Joined:
    Oct 4, 2007
    Location:
    Houston, TX
  14. madmoose macrumors newbie

    Joined:
    Oct 5, 2007
    #14
    The specification said "up to" not "up to and including" ;)
     
  15. ebel3003 macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #15
    Touche :)
     

Share This Page