
-----------------------------------
JR
Tue Oct 19, 2004 10:27 pm

Answer wont display right
-----------------------------------
this program calculates the points the user got from the questions, well a part of it. now when i putput the points i get some strange number which is wrong, how do i make it output the right nunbmer at the end of the 5 questions when it calculated all the points?



// Alex R
// A5
// October 10th


#include 
#include 
#include 



/*
Name: Ran
Purpose: Generate a random number
Gets: A int variable
Retrun: num which is the random number

*/
int ran(int &num)
{


num= (rand() % 100)+1; //Gets a random number from 1-100
	return (num);
}

/*
Name: add
Purpose: adds 2 generated random numbers
Gets: 2 numbers(variables)
Retrun: the addition between the numbers

*/
int add (int &a, int &b)
{
int total=0;
	ran(a);
	ran(b);
	 total = a + b;
	return (a+b);
}

/*
Name: subtract
Purpose: subtracts 2 generated random numbers
Gets: 2 variables
Retrun: subtraction of 2 numbers

*/
int subtract (int &a, int &b)
{
	int total=0;
	ran(a);
        ran(b);
	total = a - b;
	return (a-b);
}

/*
Name: multiply
Purpose: multiplies 2 generated random numbers
Gets: 2 variables
Retrun: multiplication of 2 numbers

*/
int multiply (int &a, int &b)
{
	int total=0;
	ran(a);
	ran(b);
	total = a * b;
	return (a*b);
}

/*
Name: answers
Purpose: checks the answers
Gets: one variable
Retrun: N/A

*/
void answers( int &total){
	
	// 10 points if answered correctly the first time.
	//  5 points if answered correctly the second time.
	//  2 points three times a charm.
	//  0 points if you struck out!

	int answer;
	int wrong=0;
	int points;
		
	for(int i=0; i < 3; i++){
		
		cin>>answer;
		
		if( answer != total ){ 
			if (i < 2){
				cout input)
				printf ("%d.\n", i);
			else {
				printf ("%d, ", i);
			}
		}
	}

	return 0;
}I tried replacing the '(input / i)' with '(input % i)', and a few other things, just to see if it would at least fix the error regardless of the task at hand, but it always gave me the runtime error unless I commented out that if statement.

-----------------------------------
wtd
Sat Oct 23, 2004 3:35 am


-----------------------------------
Which compiler are you using?

for (i = 0; i < input; i++)

When you divide by i, the first time through the loop, you're dividing by zero.  Division by zero does bad bad things.

Since 0 isn't a factor of anything, I suggest starting with:

for (i = 1; i < input; i++)

And a few other modifications:

#include 

int main (void)
{
   int input = 0, i;

   fprintf(stdout, "This program will first ask you for an integer.  It will then give you all of the factors of that integer.\n");

   fprintf(stdout, "Please input any integer: ");
   fscanf(stdin, "%d", &input);

   fprintf(stdout, "Here are the factors of your input (%d).\n", input);
   for (i = 1; i 