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

  • Context: Comp Sci 
  • Thread starter Thread starter Urmi Roy
  • Start date Start date
  • Tags Tags
    C++ Factorial Program
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to calculate the factorial of a number input by the user. Participants provide feedback on the code shared, addressing syntax errors and programming practices, while also discussing issues related to input handling in C++.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares an initial attempt at writing a C++ program to compute the factorial but includes multiple syntax errors.
  • Another participant points out that the code does not compile and suggests that the original poster should attempt to compile their code before seeking help.
  • Some participants express the need for constructive feedback rather than discouragement when addressing mistakes in the code.
  • There are discussions about the importance of using proper syntax, such as matching curly braces and correct use of `return` statements.
  • One participant suggests adding `using namespace std;` to simplify code, while another argues against this practice, citing potential issues in larger projects.
  • Concerns are raised about the handling of input, particularly regarding the incorrect use of array indexing when reading strings.
  • Participants discuss the importance of understanding array bounds and the implications of accessing out-of-bounds indices in C++.

Areas of Agreement / Disagreement

There is no consensus on the best practices for using `using namespace std;`, with some participants advocating for its use while others warn against it. Additionally, there is disagreement on the adequacy of the initial code provided, with multiple participants pointing out various errors without reaching a unified solution.

Contextual Notes

Participants highlight several syntactical errors in the code, including issues with variable initialization and input handling. The discussion also reflects a range of programming experience among participants, which influences the feedback provided.

Who May Find This Useful

Individuals learning C++ programming, particularly those seeking help with syntax errors and input handling in their code.

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()
{count<<"Enter a number:<<a;
cin>>a;
for(i=0,i<a;i++)
{f=a;
f=f*i;
}
count<<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;
count<<"Enter a number:'<<a;
cin>>a;
for(i=2,i<a;i++)
{
f=f*i;
}
count<<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::count and std::cin instead of count or cin.

you need to remove the << a from this line, since count 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).

count<<"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::count, 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()
{count<<"Enter name : ";
cin>>name[30];
count<<"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 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K