C++ Factorial Calculator Exercise Solution

Click For Summary

Discussion Overview

The discussion revolves around a C++ homework exercise that requires participants to write a program calculating the factorial of an integer using direct multiplication and a for loop. The conversation includes attempts at coding, clarifications on concepts, and suggestions for structuring the program.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the factorial calculation and the use of the variable nfac, indicating a lack of understanding of the assignment requirements.
  • Another participant clarifies that nfac is simply a variable name and asks if the original poster knows how to calculate factorials manually.
  • A participant suggests separating the solution into distinct sections rather than combining them into one subroutine, proposing to keep the existing solution intact while adding a new section for the factorial calculation.
  • Some participants recommend creating a function for the factorial calculation, while others suggest using a for loop to achieve the same result in a single statement.
  • There is a suggestion to write an algorithm in plain English before coding, which may help clarify the logic needed for the program.
  • A participant advises moving the input prompt outside the for loop to prevent it from repeating unnecessarily.
  • Another participant points out that all code should be contained within the main function, emphasizing the importance of proper structure in C++ programs.
  • One participant encounters compilation errors and seeks advice on how to resolve them, leading to further discussion about code organization and variable scope.
  • There are suggestions to simplify the code by reducing the number of integer variables used and ensuring that the factorial calculation is correctly implemented within the loop.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper code structure and the importance of understanding the algorithm before coding. However, there are differing opinions on the best approach to implement the factorial calculation, and the discussion remains unresolved regarding the exact implementation details.

Contextual Notes

Participants express uncertainty about specific C++ concepts, such as the use of subroutines and variable scope, which may affect their understanding of the assignment. There are also unresolved issues regarding the correct implementation of the factorial calculation within the provided code structure.

  • #31
estro said:
You main function looks like this: int main (void), it means that your function must return an integer. [you can change it to void main (void), then you will not need any return...]


Actually, main implicitly returns 0 for C++, so (for the main function only) you may omit "return 0;"

void main(void) is not okay!

It is not what is specified by the standards.

The caller is expecting a return value, and that value should be 0 for success.

It could result in stack corruption.
 
Last edited:

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K