need help with my loops project

This is a discussion on need help with my loops project within the C++ Programming forums, part of the General Programming Boards category; okay so im having trouble getting the right outputs for this program i am writing for my class. its supposed ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    need help with my loops project

    okay so im having trouble getting the right outputs for this program i am writing for my class.
    its supposed to be taking numbers from the user and analyzing them for their divisors(factors)
    and also whether they are prime or perfect numbers(i haven't gotten to this point yet).
    the problem with the outputs is that it won't put out all the divisors or will put out doubles.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    
    {
    	int input,
    		j, //number divding into the input number
    		decider;
    	double divisor; //an output number
    		
    	char ans;	
    		
    	do
    	{	
    		
    		do
    		{	cout << "Please enter a number between 1 and 1000" << endl;
    			cin >> input;
    		}
    		while (!(input >= 1) || !(input <= 1000));
    
    		cout << "Number: " << input << endl;
    		if (input != 1)
    			cout << "Divisors: ";
    		else
    			cout << "This number is neither prime nor perfect" << endl;
     
    		for (j = 1; (j <= input) && (input % j == 0); j++)
    		{	
    			divisor = static_cast<float>(input)/j;
    			if (divisor != j)
    				cout << divisor<< " ";
    								
    			if (j != divisor)
    				cout << j << " ";
    		}
    	
    		cout << "\nWould you like to continue (Y/N)?\n";
    		cout << "You must enter a Y or a N." << endl;
    		cin >> ans;
    	}
    	while (((ans != 'n') && (ans != 'N')) || ((ans == 'Y') || (ans == 'y')));
    		
    	
    return 0;
    
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    for (j = 1; (j <= input) && (input &#37; j == 0); j++)
    In other words, this loop will stop as soon as input % j == 0. Actually, this will happen on the first test since input % 1 = 0. In other words, the loop will actually never execute.

    Code:
    			if (divisor != j)
    				cout << divisor<< " ";
    								
    			if (j != divisor)
    				cout << j << " ";
    If divisor != j, then j != divisor. Right?

    Code:
    while (((ans != 'n') && (ans != 'N')) || ((ans == 'Y') || (ans == 'y')));
    Consider tolower() or toupper() from <cctype>.

    I think you need to have another look at the problem. How would you do it on paper?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    2
    well the parameters are what we have been taught so far in this class so basically i have to use do while, while, and for loops, the loop does execute but with the way im coding it it just doesn't want to put out either all the divisors or only one iteration of a divisor(as in it will output 1 1 3 4 4)

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    well the parameters are what we have been taught so far in this class so basically i have to use do while, while, and for loops, the loop does execute but with the way im coding it it just doesn't want to put out either all the divisors or only one iteration of a divisor(as in it will output 1 1 3 4 4)
    Yeah, I read your expression as the opposite of what it is.
    Code:
    for (j = 1; (j <= input) && (input &#37; j == 0); j++)
    That loop stops as soon as it reaches a number that is not a divisor. For example, if you enter 100, it will loop with j=1 and find 100 and 1 as divisors; then it will loop with j=2 and find 50 and 2; and then when j=3, input % j != 0, and the loop exits, missing some divisors of 100 including 20 and 25.

    That's not what you want. You don't want the loop to stop executing as soon as it finds a number that isn't a divisor. You want the loop to keep going and simply not print anything in this case.

    BTW, if you get it working, as written, you'll find every divisor twice. And you won't find a divisor if that divisor is the square root of the number.

    Like I said, think about how you would do this on paper.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. renaming a project created with MSVC 6
    By Bleech in forum Windows Programming
    Replies: 2
    Last Post: 10-31-2006, 01:19 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 01:48 AM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. DJGPP project problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 08:16 PM
  5. c++ OOP project ideas
    By newbie17 in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2002, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21