Comp Sci To write a C++ program to find the factorial of a number

AI Thread Summary
The discussion focuses on writing a C++ program to calculate the factorial of a number, highlighting several coding errors in the user's initial attempts. Key issues include syntax errors, such as mismatched parentheses and incorrect use of `cin` and `cout`, which prevent the code from compiling. Participants emphasize the importance of compiling code before seeking help and suggest using `std::cout` instead of `using namespace std;` to avoid potential issues in larger projects. Additionally, a problem with input handling in a separate code snippet is identified, where incorrect array indexing leads to input being ignored. Overall, the conversation provides constructive feedback on improving coding practices and debugging techniques.
Urmi Roy
Messages
743
Reaction score
1

Homework Statement



To write a C++ program to find the factorial of the number input 'a'.

Homework Equations



the factorial evaluated is denoted as 'f'
i is the counter

The Attempt at a Solution



Pls check if my program is correct...(here f is the value of factorial and a is the number whose factorial is to be found...)

#include<iostream>
int f
int a
int i
void main()
{cout<<"Enter a number:<<a;
cin>>a;
for(i=0,i<a;i++)
{f=a;
f=f*i;
}
cout<<f;
end1
}

Please tell me if this is okay.
 
Physics news on Phys.org
No, it's not OK. The code you posted doesn't even compile. Even after you fix the syntactical errors, it won't print a!.
 
Considering this question is under 'homework help', some constructive feedback from ppl who are more adept with C++ would be more helpful than just a discouraging statement.
 
Have you tried to compile your program? What kind of error messages did you get?

Also, is what you typed in the opening post an exact replica of the code you are trying to compile and execute, or did you rewrite it for this thread? There are a number of syntactical errors in what you wrote. If your code does compile, please post that code (e.g., copy and paste) rather than retyping it.
 
I haven't even got C++ on my computer...this was homework from where I'm taking C++ coaching (only just begun, as you can well imagine)...my teacher asked me to write a code and show him..
 
How about this...

#include<iostream>
void main()
int f;
int a;
int i;
f=1;
i=2;
cout<<"Enter a number:'<<a;
cin>>a;
for(i=2,i<a;i++)
{
f=f*i;
}
cout<<f;
return0
}
 
At first sight - it still won't compile. Curly parentheses are not matched. " and ' don't match. No such thing as return0. Please TRY to compile your code before posting it here, otherwise this thread is a waste of time.
 
BTW, there are many free C++ compilers out there for download for any platform/OS. If you're using Linux or some Unix (including OS X), you probably have it already installed, and accessible through the terminal. So there is absolutely no excuse for not attempting compilation before posting here.

Syntax errors should, at a minimum, be completely fixed before posting to ask for help. Posting to clarify the program flow because the output is not as expected - now that's far more reasonable.
 
Urmi Roy said:
How about this.
You need to add this line:

using namespace std;

just after the #include <iosream> would be ok. This line is needed or else you have to use std::cout and std::cin instead of cout or cin.

you need to remove the << a from this line, since cout is used to output data, and at this point in your program, 'a' is not initialized (no specific value has been stored in 'a yet).

cout<<"Enter a number:'<<a;

This part of your program is getting close, but it needs a minor fix.

for(i=2,i<a;i++)
{
f=f*i;
}

If you have windows, you can download visual c / c++ 2010 express for free. There's also visual studio express 2012, but that needs windows 7.
 
  • #10
rcgldr said:
You need to add this line:

using namespace std;
My advice: Don't ever do that. Not ever. It's a bad habit and it will get you in trouble. Put that in a header file and it will get not only you but everyone who includes that header into trouble.

It's not just my opinion. Programming standard after programming standard prohibits the use of "using namespace xxx" no matter what that xxx is.
 
  • #11
D H said:
My advice: Don't ever do that. Not ever. It's a bad habit and it will get you in trouble. Put that in a header file and it will get not only you but everyone who includes that header into trouble.
There was no mention of putting "using namespace xxx" into a header file, perhaps you had a different point to make here?
 
  • #12
Yeah. Use std::cout, std::sqrt, std::this, std::that. It's five extra characters that will save your life. Remember the adage "Always program as if the person who will maintain or use your code is a homicidal maniac who knows where you live."
 
  • #13
OK, I got visual C++ (actually visual studio 2008) , and here's a code that does compile, and gives no errors. However, the problem is that after I enter my name, it displays 'Enter your age:' but does not take any input.
Pls provide some feedback.

#include<iostream>
#include<conio.h>
using namespace std;
class customer
{ char name[30];
int age;
public:
void accept()
{cout<<"Enter name : ";
cin>>name[30];
cout<<"Enter your age : ";
cin>>age;
}
};

void main()
{customer obj;
obj.accept();
getch();
}
 
  • #14
cin>>name[30] is a bad statement. You are supposed to use only cin>>name.
The reason for that is name[29] is the last allocated byte for the variable. name[30] might point to garbage or nothing at all.
 
Last edited:
  • #15
Urmi Roy said:
OK, I got visual C++ (actually visual studio 2008) , and here's a code that does compile, and gives no errors. However, the problem is that after I enter my name, it displays 'Enter your age:' but does not take any input.
Pls provide some feedback.
Sure, here's some. When you post code here at Physics Forums, put a [code[/color]] tag at the top and a [/code[/color]] tag at the bottom. Doing this preserves whatever indentation you started with, and makes your code easier to read.

I have done this below.
Urmi Roy said:
Code:
#include<iostream>
#include<conio.h>
using namespace std;
class customer
{	
   char name[30];
   int age;

   public:

   void accept()
   {
      cout<<"Enter name : ";
      cin>>name[30];
      cout<<"Enter your age : ";
      cin>>age;
   }
};

void main()
{
   customer obj;
   obj.accept();
   getch();
}
 
  • #16
Urmi Roy said:
OK, I got visual C++ (actually visual studio 2008) , and here's a code that does compile, and gives no errors. However, the problem is that after I enter my name, it displays 'Enter your age:' but does not take any input.
Pls provide some feedback.

Here's what's happening.
name is an array with indices running from 0 through 29.

Program: Enter name :
You: Urmi Roy

The type of name[30] is char, so the statement, cin << name[30]; stores the character 'U' at a location just outside the name array. This is why Millennial said this was a bad statement.

The input buffer still contains the characters rmi Roy, so when the prompt to enter your age appears, none of the characters in the input buffer can be construed to be a number, so your program ignores them, and ends.
 

Similar threads

Replies
2
Views
3K
Replies
3
Views
1K
Replies
3
Views
2K
Replies
15
Views
3K
Replies
3
Views
1K
Replies
23
Views
3K
Back
Top