logo elektroda
logo elektroda
X
logo elektroda

C++ Iterative Factorial Algorithm: Console Closing Issue, For Loop, Long Int Syntax

zbigniew 10123 27246 11
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 6532732
    zbigniew 10123
    Level 12  
    I have presented an algorithm that calculates strongly iteratively in C++.
    Problem:
    -the code compiles and displays on the screen, but after entering the number it closes the console window and that's it;
    -gives him a number for n! and end of display;
    -I tried adding the library [#include ], but it didn't help to display the results;
    - the factorial function in braces should have a good syntax because I rewrote it based on the book;
    - preferably the algorithm should be built on the [for] loop;
    -searching in google, I found little about it, so I started to write it myself, but it doesn't want to work, there is an error somewhere.

    [Code for factorial iteratively in C++]

    [code:1:119d82eecd]#include
    #include
    #include
    using namespace std;


    long silnia(int liczba )
    {
    long int silnia=1;
    for(int i=2;i n;
    cout
  • ADVERTISEMENT
  • #2 6532772
    piterus99
    Level 43  
    No sense - first you call the function, then you get the value from the keyboard on which this function is to work, and at the end you display what the user typed...

    [code:1:ac360adee9]cout > n;
    cout
  • #3 6532937
    anonymousexd
    Level 24  
    [code:1:2223e695e7]#include
    #include
    #include
    using namespace std;


    long silnia(int liczba )
    {
    long int silnia=1;
    for(int i=2;i n;
    //jesli n to zero lub liczba ujemna
    if (n == 0 || n < 0)
    {
    cout
  • ADVERTISEMENT
  • #4 6533241
    zbigniew 10123
    Level 12  
    Everything is ok. The program compiles but does not display the results.
  • #5 6533267
    piterus99
    Level 43  
    This one may work:
    [code:1:e6b4ea4290]#include
    #include

    using namespace std;

    long silnia(int liczba )
    {
    long int silnia=1;
    for(int i=2;i n;
    cout
  • #6 6539089
    zbigniew 10123
    Level 12  
    I really missed this instruction and that was the problem. The function counts well is ok. And how to protect it so that it does not calculate n
  • Helpful post
    #7 6539594
    Łagu
    Level 12  
    Maybe I can help

    [code:1:37fde233f7]

    #include
    #include

    using namespace std;

    long silnia(int liczba )
    {
    long int silnia=1;
    if (liczba
  • ADVERTISEMENT
  • #8 6545426
    piterus99
    Level 43  
    Compiling, please, compiling!

    Can you read messages? It tells you that you didn't declare the variable n...
  • #9 6546853
    zbigniew 10123
    Level 12  
    Tell me where this 'n' should be declared
    C++ Iterative Factorial Algorithm: Console Closing Issue, For Loop, Long Int Syntax

    Maybe it's hard to see, but something else always came out with the error. Where is this minor error?
  • ADVERTISEMENT
  • #10 6547309
    pawelhabas
    Level 11  
    It should be declared before using it, i.e. a line before this error :)
  • Helpful post
    #11 6547336
    Łagu
    Level 12  
    [code:1:c870f2ec19]
    #include
    #include

    using namespace std;

    long silnia(int liczba )
    {
    long int silnia=1;
    if (liczba
  • #12 6547412
    zbigniew 10123
    Level 12  
    When I get up in the morning, I'll let you know if it works in DEV C++. ok :D

    Added after 23 [minutes]:

    Do you know any code to find the roots of a quadratic equation using a function as well. Regards hey :D

Topic summary

The discussion revolves around a C++ iterative algorithm for calculating factorials, where the user encounters an issue with the console window closing immediately after inputting a number. The code compiles but fails to display results. Various responses suggest checking the order of function calls, ensuring proper variable declaration, and using a for loop for the factorial calculation. The user acknowledges missing an instruction that caused the problem and seeks further assistance on error handling and additional coding tasks, such as finding roots of quadratic equations.
Summary generated by the language model.
ADVERTISEMENT