I am fairly new to c++ and have been working on this problem and asked the professor for help, but he has really been non-compliant. I am lost on this and keep gettin error messages that I do not understand how to fix. Any help that could be given would be absolutely great, thanks!

Copy the following code exactly as shown. Starting with the following code base, add code in the area shown to create a function called printAllFactors. Do not change any of the other code anywhere else in the program! Write only one function (not two). You must write your own function – do not use a library function. If you break any of these rules you will get a zero for this question! Remember that a factor is any number that divides into another number without any remainders. For example, the factors of 8 are: 4 2 1. Use the screen shots below as a guide.
#include <iostream>
using namespace std;
// Do NOT change anything ABOVE HERE
// WRITE THE FUNCTION BETWEEN HERE
// AND HERE
// Do NOT change anything BELOW HERE
int main()
{
int i1, i2;
cout << "Enter an integer: ";
cin >> i1;
printAllFactors(i1);
cout << "Enter another integer: ";
cin >> i2;
printAllFactors(i2);
}

#include <iostream>

using namespace std;

class dic(object)
{
	def printAllFactors(self,x)
		while (x)
		{
			if (x %2 == 0)
			{
				print x
					x /= 2
					if (x == 1)
					{
						print x
							break
					}
			}
			else
			{
				print x
					break
			}
		}
		return (printAllFactors)
}

int main()
{
	int i1, i2;
	cout << "enter an integer: ";
	cin >> i1;
	printAllFactors(i1);

	cout << "enter another integer: ";
	cin >> i2;
	printAllFactors(i2);
}

what are a few of the error messages? What compiler and operating system are you using?

It looks like you are attempting to compile pseudo code that your teacher gave you as if it is c/c++ code. You can't do that. You have to translate that pseudocode into c++ code before the compiler can use it.

Edited 6 Years Ago by Ancient Dragon: n/a

I am fairly new to c++ and have been working on this problem and asked the professor for help, but he has really been non-compliant. I am lost on this and keep gettin error messages that I do not understand how to fix. Any help that could be given would be absolutely great, thanks!

Copy the following code exactly as shown. Starting with the following code base, add code in the area shown to create a function called printAllFactors. Do not change any of the other code anywhere else in the program! Write only one function (not two). You must write your own function – do not use a library function. If you break any of these rules you will get a zero for this question! Remember that a factor is any number that divides into another number without any remainders. For example, the factors of 8 are: 4 2 1. Use the screen shots below as a guide.
#include <iostream>
using namespace std;
// Do NOT change anything ABOVE HERE
// WRITE THE FUNCTION BETWEEN HERE
// AND HERE
// Do NOT change anything BELOW HERE
int main()
{
int i1, i2;
cout << "Enter an integer: ";
cin >> i1;
printAllFactors(i1);
cout << "Enter another integer: ";
cin >> i2;
printAllFactors(i2);
}

#include <iostream>

using namespace std;

class dic(object)
{
	def printAllFactors(self,x)
		while (x)
		{
		if (x %2 == 0)
			{
				print x
					x /= 2
					if (x == 1)
					{
						print x
							break
					}
			}
			else
			{
				print x
					break
			}
		}
		return (printAllFactors)
}

int main()
{
	int i1, i2;
	cout << "enter an integer: ";
	cin >> i1;
	printAllFactors(i1);

	cout << "enter another integer: ";
	cin >> i2;
	printAllFactors(i2);
}

If all you need to do is write a function for the main to use, you don't need to make a class. Create a function, translate

def printAllFactors(self,x)
		while (x)
		{
		if (x %2 == 0)
			{
			print x
			x /= 2
			if (x == 1)
			{
				print x
				break
			}
			}
			else
			{
				print x
				break
			}
		}
		return (printAllFactors)

into c++ code. Try that, and monkey around with the code a bit once you have it written, and you should figure it out.

void printAllFactors( int in )
{
	//remove the = if you want it to not show "in" as being a factor  
	for( int i = 1; i <= in; i++ ) //start the counter at 1 and loop it until it reaches the inputted number
		if( in % i == 0 ) //check if i (the counter) divides evenly into the inputted number
			cout << i << " "; //if it does then it is a factor
	cout << endl;
}

The reason why I'm posting this solution is because from looking at your pseudo code it doesn't really look like you know what you are trying to do.

If you want it to print in descending order (this is ascending) then flip the loop so it starts at i = in (or i = in-1 if you want it to not show "in" as being a factor), stop at i <= 1 and increment it by i--.

Well, since your original code is so far off, and sfuo already posted a valid solution, let me just post a slightly better one that applies if you don't care about the print order at all:

void printAllFactors( int in )
{
	//start at 2 if you want it to not show 1 and "in" as a factor  
	int hi = in; //declare a variable for the higher factor in the factor-pairs.
        for( int lo = 1; lo <= hi; ++lo ) //start the counter at 1 and loop it until the higher factor reaches the lower factor
		if( in % lo == 0 ) { //check if lo divides evenly into the input number
                        hi = in / lo; //compute the higher factor of the pair
			cout << lo << " " << hi << " "; //print the factor-pair
                };
	cout << endl;
}

Yeah I thought about doing that mike but unless you wanna throw in a vector or something to sort them I think just running through the list looks better.

Thanks so much! I actually understand what you guys are saying. I looked ahead in my textbook, which is where I got the ideas for my wrong code, so I'm glad you guys could clear that up for me, thanks a lot!

This article has been dead for over six months. Start a new discussion instead.