10 Replies - 2678 Views - Last Post: 10 April 2011 - 06:39 AM Rate Topic: -----

#1 mightysparks  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 23-February 11

C++ factorial program?

Posted 06 April 2011 - 10:22 PM

We have started on modular programming, and one of our practice questions is:
Write a program consisting of two functions (plus main). The first function should read in a number and then pass this back to main. This value should then be passed into a second function that calculates and displays the factorial.

I've attempted it, but I keep getting errors when I'm compiling.
Error E2379 fac.cpp 7: Statement missing ; in function GetData(int &)
Error E2134 fac.cpp 35: Compound statement missing } in function GetData(int &)
Warning W8057 fac.cpp 35: Parameter 'num' is never used in function GetData(int
&)

Our lecture notes didn't have a ; at the end of 'void GetData(int &num)', but I put one there anyway and it then said 'declaration terminated incorrectly'. So now I'm lost. How can I fix these errors? And are there any other problems with my code so far?

#include <stdio.h>

/* Get user unput */
void GetData(int &num)
{
	printf("Enter a number: ");
	scanf{"%d%*c", &num);
	
	return;
}

/* Calculate factorial */
int factorial (int num)
{
	int fact = 1;
	if (n <= 1)
		return 1;
	else
		fact = n * factorial (n-1);
		
	printf("Factorial of &num is %d /n", num);
	
	return(fact);
}

int main ()
{
	int num, fact;
	
	GetData(num);
	
	factorial(num);

	return(0);
}


Is This A Good Question/Topic? 0
  • +

Replies To: C++ factorial program?

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C++ factorial program?

Posted 06 April 2011 - 10:40 PM

The function's argument type can't be &. You need to send a pointer to the function, so the signature should be
void GetData(int *num)

Now, since inside the GetData function, num will be a pointer, delete the & from the scanf statement (line 7). Also, you have a brace { instead of a left parenthesis ( right after "scanf".

And since you have to pass a pointer (i.e. an address) to the function, change the function call on line 30 accordingly.
Was This Post Helpful? 1
  • +
  • -

#3 mightysparks  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 23-February 11

Re: C++ factorial program?

Posted 06 April 2011 - 10:55 PM

Ok, I changed that, but now I get another error and a warning:
Warning W8004 fac.cpp 15: 'fact' is assigned a value that is never used in function factorial(int)
Error E2062 fac.cpp 30: Invalid indirection in function main()

Also we haven't learned about pointers yet, is that the only way to do this?

#include <stdio.h>

/* Get user unput */
void GetData(int *num)
{
	printf("Enter a number: ");
	scanf("%d%*c", num);
	
	return;
}

/* Calculate factorial */
int factorial (int num)
{
	int fact = 1;
	if (num <= 1)
		return 1;
	else
		fact = num * factorial (num-1);
		
	printf("Factorial of &num is %d /n", num);
	
	return(fact);
}

int main ()
{
	int num, fact;
	
	GetData(*num);
	
	factorial(num);

	return(0);
}


Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C++ factorial program?

Posted 06 April 2011 - 11:09 PM

You're already in effect using a pointer when you use scanf sending &num as the argument.

The reason for the error in main is that the call should be
GetData(&num);
same as you do with scanf.

But yes, you can design GetData without using a pointer. Instead, make its return type int instead of void, and return the value of the number that is entered by the user.

... and in that case, the call in main would instead be
num = GetData();

(Also, change the definition of GetData so as not to require any argument.)
Was This Post Helpful? 1
  • +
  • -

#5 mightysparks  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 23-February 11

Re: C++ factorial program?

Posted 06 April 2011 - 11:26 PM

I got it working with the pointer, but I don't understand what you mean by 'change the definition of GetData so as not to require any argument'.
Was This Post Helpful? 0
  • +
  • -

#6 mightysparks  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 23-February 11

Re: C++ factorial program?

Posted 06 April 2011 - 11:44 PM

Also, for part 2 of the question, we have to: Change the function that calculates the factorial so that the result is returned to main instead of being printed. A new function should be created and called from main that displays the calculated value.

It doesn't display the correct value now though. I put in 4 and get 2147328000 instead of 24.

#include <stdio.h>

/* Get user unput */
void GetData(int *num)
{
	printf("Enter a number: ");
	scanf("%d%*c", num);
	
	return;
}

/* Calculate factorial */
void Factorial(int num)
{
	int fact = 1;
	for (int i = 2; i <= num; i++) 
	{  
    fact = fact * i;  
	}
	
	return;
}

/* Print Results */
void PrintResults(int fact)
{
	printf("Factorial is %d", fact);

	return;
}

int main()
{
	int num, fact;
	
	GetData(&num);
	
	Factorial(num);
	
	PrintResults(fact);

	return(0);
}


Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: C++ factorial program?

Posted 06 April 2011 - 11:47 PM

If you wanted to do it without using a pointer as the function's argument, you could define the function as:
int GetData()
{
    int num;
    printf("Enter a number: ");
    scanf("%d%*c", &num);
    return num;
}

i.e. no argument, and returning an int.
Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: C++ factorial program?

Posted 07 April 2011 - 03:57 AM

Here's another suggestion to read the function tutorials linked in my signature.
Was This Post Helpful? 0
  • +
  • -

#9 mightysparks  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 23-February 11

Re: C++ factorial program?

Posted 09 April 2011 - 09:47 PM

EDIT: I changed some stuff around and worked it out :) Thanks for the help

This post has been edited by mightysparks: 09 April 2011 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#10 nazmul_nazim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-April 11

Re: C++ factorial program?

Posted 10 April 2011 - 04:10 AM

why don't you use:(lot more easier)


#include<stdio.h>
int main()
{
int fact=1,num;
scanf("%d%*c",&num);
if(num>=1)
for( ;num>1;num--)
{
fact=fact*num;
}
else fact=1;
printf("%d",fact);
return 0;
}
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • Joined: 08-August 08

Re: C++ factorial program?

Posted 10 April 2011 - 06:39 AM

View Postnazmul_nazim, on 10 April 2011 - 07:10 AM, said:

why don't you use:(lot more easier)
#include<stdio.h>
int main()
{
int fact=1,num;
scanf("%d%*c",&num);
if(num>=1)
  for( ;num>1;num--)
		{
		 fact=fact*num;
		}
else fact=1;
printf("%d",fact);
return 0;
}

Because that's just plain wrong.
  • It doesn't follow the rules of the assignment.
  • It has no structure.

The purpose of the assignment is to teach the idea of using functions to break down a larger problem into small components. While your code may work for this small project it becomes exponentially more difficult to write code like that as the size of the project grows.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1