What can I do in this code to prompt an error when a negative number is used?











up vote
3
down vote

favorite
1












i was wondering what can I do to prompt an error when I input a negative number. I used a for loop for this program. What this program does, or at least its supposed to do, is to output the factor of any given number (if positive). But I am not sure how to make my code to prompt an error or at least keep asking for a number if the number that input is less or equal to 0.



I used the variable n as the number to input by the user.



I am really new to programming and I am eager to finish this program as soon as possible. Can you please assist?



  #include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;


int main()
{
double n;
cout << "Welcome to this program... Hope you like it" << flush << endl;
do
{
int i, fact = 1, n;
cout << "Please enter a value for the variable " "n: ";
cin >> n;

for (i = 1; i <= n; i++) {
fact = fact * i;
}
cout << "Factorial of " << n << " is: " << fact << endl;
cout << " Thanks for using this program... hope you liked it!" << endl;
} while (n >= 0);
return 0;
}









share|improve this question


















  • 6




    Wait. You have 2 ns. Best not to do this. It's legal since they are in different scopes, but it's source of confusion.
    – user4581301
    Nov 20 at 0:41










  • Consider drawing a picture of what you want the code to do. Then draw a picture of what the code does. Them move the code around until the picture of what the code does matches what the code should do.
    – user4581301
    Nov 20 at 0:46















up vote
3
down vote

favorite
1












i was wondering what can I do to prompt an error when I input a negative number. I used a for loop for this program. What this program does, or at least its supposed to do, is to output the factor of any given number (if positive). But I am not sure how to make my code to prompt an error or at least keep asking for a number if the number that input is less or equal to 0.



I used the variable n as the number to input by the user.



I am really new to programming and I am eager to finish this program as soon as possible. Can you please assist?



  #include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;


int main()
{
double n;
cout << "Welcome to this program... Hope you like it" << flush << endl;
do
{
int i, fact = 1, n;
cout << "Please enter a value for the variable " "n: ";
cin >> n;

for (i = 1; i <= n; i++) {
fact = fact * i;
}
cout << "Factorial of " << n << " is: " << fact << endl;
cout << " Thanks for using this program... hope you liked it!" << endl;
} while (n >= 0);
return 0;
}









share|improve this question


















  • 6




    Wait. You have 2 ns. Best not to do this. It's legal since they are in different scopes, but it's source of confusion.
    – user4581301
    Nov 20 at 0:41










  • Consider drawing a picture of what you want the code to do. Then draw a picture of what the code does. Them move the code around until the picture of what the code does matches what the code should do.
    – user4581301
    Nov 20 at 0:46













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





i was wondering what can I do to prompt an error when I input a negative number. I used a for loop for this program. What this program does, or at least its supposed to do, is to output the factor of any given number (if positive). But I am not sure how to make my code to prompt an error or at least keep asking for a number if the number that input is less or equal to 0.



I used the variable n as the number to input by the user.



I am really new to programming and I am eager to finish this program as soon as possible. Can you please assist?



  #include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;


int main()
{
double n;
cout << "Welcome to this program... Hope you like it" << flush << endl;
do
{
int i, fact = 1, n;
cout << "Please enter a value for the variable " "n: ";
cin >> n;

for (i = 1; i <= n; i++) {
fact = fact * i;
}
cout << "Factorial of " << n << " is: " << fact << endl;
cout << " Thanks for using this program... hope you liked it!" << endl;
} while (n >= 0);
return 0;
}









share|improve this question













i was wondering what can I do to prompt an error when I input a negative number. I used a for loop for this program. What this program does, or at least its supposed to do, is to output the factor of any given number (if positive). But I am not sure how to make my code to prompt an error or at least keep asking for a number if the number that input is less or equal to 0.



I used the variable n as the number to input by the user.



I am really new to programming and I am eager to finish this program as soon as possible. Can you please assist?



  #include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;


int main()
{
double n;
cout << "Welcome to this program... Hope you like it" << flush << endl;
do
{
int i, fact = 1, n;
cout << "Please enter a value for the variable " "n: ";
cin >> n;

for (i = 1; i <= n; i++) {
fact = fact * i;
}
cout << "Factorial of " << n << " is: " << fact << endl;
cout << " Thanks for using this program... hope you liked it!" << endl;
} while (n >= 0);
return 0;
}






c++ visual-studio-2010 visual-c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 0:35









Diego Montilla

161




161








  • 6




    Wait. You have 2 ns. Best not to do this. It's legal since they are in different scopes, but it's source of confusion.
    – user4581301
    Nov 20 at 0:41










  • Consider drawing a picture of what you want the code to do. Then draw a picture of what the code does. Them move the code around until the picture of what the code does matches what the code should do.
    – user4581301
    Nov 20 at 0:46














  • 6




    Wait. You have 2 ns. Best not to do this. It's legal since they are in different scopes, but it's source of confusion.
    – user4581301
    Nov 20 at 0:41










  • Consider drawing a picture of what you want the code to do. Then draw a picture of what the code does. Them move the code around until the picture of what the code does matches what the code should do.
    – user4581301
    Nov 20 at 0:46








6




6




Wait. You have 2 ns. Best not to do this. It's legal since they are in different scopes, but it's source of confusion.
– user4581301
Nov 20 at 0:41




Wait. You have 2 ns. Best not to do this. It's legal since they are in different scopes, but it's source of confusion.
– user4581301
Nov 20 at 0:41












Consider drawing a picture of what you want the code to do. Then draw a picture of what the code does. Them move the code around until the picture of what the code does matches what the code should do.
– user4581301
Nov 20 at 0:46




Consider drawing a picture of what you want the code to do. Then draw a picture of what the code does. Them move the code around until the picture of what the code does matches what the code should do.
– user4581301
Nov 20 at 0:46












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Commens in the code:



#include <iostream>

// it's best not to do using namespace std. It's a huge namespace

int main()
{
std::cout << "Welcome to this program... Hope you like itn";
do
{
int i, fact = 1, n;
std::cout << "Please enter a value for the variable " "n: ";
std::cin >> n;

// if std::cin is in a failed state then break out of the loop.
if (std::cin.fail()) break;

// added check if n<0. if n<0 ptint error on stderr
if (n<0) {
std::clog << "Error: you must supply a number equal to or greater than zeron";
} else { // if n>=0
for (i = 1; i <= n; i++) {
fact = fact * i;
}
std::cout << "Factorial of " << n << " is: " << fact << "n";
}
} while (true); // <- now always true

std::cout << "nThanks for using this program... hope you liked it!n";
return 0;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384606%2fwhat-can-i-do-in-this-code-to-prompt-an-error-when-a-negative-number-is-used%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Commens in the code:



    #include <iostream>

    // it's best not to do using namespace std. It's a huge namespace

    int main()
    {
    std::cout << "Welcome to this program... Hope you like itn";
    do
    {
    int i, fact = 1, n;
    std::cout << "Please enter a value for the variable " "n: ";
    std::cin >> n;

    // if std::cin is in a failed state then break out of the loop.
    if (std::cin.fail()) break;

    // added check if n<0. if n<0 ptint error on stderr
    if (n<0) {
    std::clog << "Error: you must supply a number equal to or greater than zeron";
    } else { // if n>=0
    for (i = 1; i <= n; i++) {
    fact = fact * i;
    }
    std::cout << "Factorial of " << n << " is: " << fact << "n";
    }
    } while (true); // <- now always true

    std::cout << "nThanks for using this program... hope you liked it!n";
    return 0;
    }





    share|improve this answer



























      up vote
      0
      down vote













      Commens in the code:



      #include <iostream>

      // it's best not to do using namespace std. It's a huge namespace

      int main()
      {
      std::cout << "Welcome to this program... Hope you like itn";
      do
      {
      int i, fact = 1, n;
      std::cout << "Please enter a value for the variable " "n: ";
      std::cin >> n;

      // if std::cin is in a failed state then break out of the loop.
      if (std::cin.fail()) break;

      // added check if n<0. if n<0 ptint error on stderr
      if (n<0) {
      std::clog << "Error: you must supply a number equal to or greater than zeron";
      } else { // if n>=0
      for (i = 1; i <= n; i++) {
      fact = fact * i;
      }
      std::cout << "Factorial of " << n << " is: " << fact << "n";
      }
      } while (true); // <- now always true

      std::cout << "nThanks for using this program... hope you liked it!n";
      return 0;
      }





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Commens in the code:



        #include <iostream>

        // it's best not to do using namespace std. It's a huge namespace

        int main()
        {
        std::cout << "Welcome to this program... Hope you like itn";
        do
        {
        int i, fact = 1, n;
        std::cout << "Please enter a value for the variable " "n: ";
        std::cin >> n;

        // if std::cin is in a failed state then break out of the loop.
        if (std::cin.fail()) break;

        // added check if n<0. if n<0 ptint error on stderr
        if (n<0) {
        std::clog << "Error: you must supply a number equal to or greater than zeron";
        } else { // if n>=0
        for (i = 1; i <= n; i++) {
        fact = fact * i;
        }
        std::cout << "Factorial of " << n << " is: " << fact << "n";
        }
        } while (true); // <- now always true

        std::cout << "nThanks for using this program... hope you liked it!n";
        return 0;
        }





        share|improve this answer














        Commens in the code:



        #include <iostream>

        // it's best not to do using namespace std. It's a huge namespace

        int main()
        {
        std::cout << "Welcome to this program... Hope you like itn";
        do
        {
        int i, fact = 1, n;
        std::cout << "Please enter a value for the variable " "n: ";
        std::cin >> n;

        // if std::cin is in a failed state then break out of the loop.
        if (std::cin.fail()) break;

        // added check if n<0. if n<0 ptint error on stderr
        if (n<0) {
        std::clog << "Error: you must supply a number equal to or greater than zeron";
        } else { // if n>=0
        for (i = 1; i <= n; i++) {
        fact = fact * i;
        }
        std::cout << "Factorial of " << n << " is: " << fact << "n";
        }
        } while (true); // <- now always true

        std::cout << "nThanks for using this program... hope you liked it!n";
        return 0;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 6:30

























        answered Nov 20 at 0:53









        Ted Lyngmo

        1,368214




        1,368214






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384606%2fwhat-can-i-do-in-this-code-to-prompt-an-error-when-a-negative-number-is-used%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'