how to initialize multiple loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • breed3000
    New Member
    • Nov 2014
    • 4

    how to initialize multiple loops

    hi everyone I am a newbie to C++. I have an assignment that has multiple loops. I have to use a for, do, while loops.
    I have to ask user to input numbers based on (test data 75, 62, 13,-35,55,0. My program should loop processing input from the user until user indicates user wants to quit by entering 0 for an integer.
    If the number is a multiple of 5, print all multiples of 5 from 5 through number entered. Use a For loop to generate numbers
    If number entered is even, print all numbers from 2 through number entered. Use a While loop to generate numbers
    If the number entered is a odd number, print all odd numbers from 1 through number entered.
    only print one set of numbers. For example if 20 is entered print 5 10 15 20 etc but do not print 2 4 6....20
    If the remainder of the number divided by 5 is 0, the number is a multiple of 5, If the remainder of the number divided by 2 is 0, the number is even

    Here is what I have so far. But when I tried using the while loop the number continuously ran?

    thank you for any and all suggestions given




    #include <iostream>
    using namespace std;
    int main()
    {

    int number;



    cout << " Enter a positive integer or a 0 to quit program?" << endl;
    cin >> number;


    if (number % 5 == 0)


    for (number = 5; number <= 75; number += 5)
    {

    cout << number << endl;

    }

    cout << endl;

    cout << " Enter a positive integer or a 0 to quit program?" << endl;
    cin >> number;

    if (number % 2 == 0 )

    for (number = 2; number <= 82; number += 2)
    {
    cout << number << endl;

    }
    cout << endl;


    cout << " Enter a positive integer or a 0 to quit program?" << endl;
    cin >> number;

    if(number % 1 == 0)

    do
    {
    cout << number << endl;
    number += 1;
    }

    while (number % 1 == 0);



    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    n % 1 is always 0. Therefore, your while loop becomes 0 == 0, which is always true and the loop runs forever.

    POst again if you are still stuck.

    Comment

    • breed3000
      New Member
      • Nov 2014
      • 4

      #3
      Hi weaknessforcats!
      Thank you so much for replying. So do I use
      Code:
      do
      {
      cout << number << endl;
      number += 1;
      }
      
      while (number > 0);


      also I am using Microsoft Visual Studio 2010 Professional, not quite sure that matters, but just in case it does.

      Code:
      #include <iostream>
      using namespace std;
      int main()
      {
      
      int number;
      
      
      
      cout << " Enter a positive integer or a 0 to quit program?" << endl;
      cin >> number;
      
      
      if (number % 5 == 0)
      
      
      for (number = 5; number <= 75; number += 5)
      {
      
      cout << number << endl;
      
      }
      
      cout << endl;
      
      cout << " Enter a positive integer or a 0 to quit program?" << endl;
      cin >> number;
      
      if (number % 2 == 0 )
      
      for (number = 2; number <= 82; number += 2)
      {
      cout << number << endl;
      
      }
      cout << endl;
      
      
      cout << " Enter a positive integer or a 0 to quit program?" << endl;
      cin >> number;
      
      if(number % 1 == 0)
      
      do
      {
      cout << number << endl;
      number += 1;
      }
      
      while (number > % 1 );
      
      
      
      return 0;
      }
      I'm just not clear on the while or do while statements. I mean the correct way to state them? The first For statement executes fine. The second for statement should have been a while statement. the do while is just running on forever. What have I done wrong? Thank you for your assistance and clarity

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Your do-while uses while (number > % 1). The > % will never compile and in any case a number modulo 1 is always zero. Why? Because any number divided by 1 has a zero remainder. Stop coding that.

        The problem says to use a while loop to generate the numbers. So this loop would generate 10 numbers:

        Code:
        int ctr = 0;
        int number = 0;
        while (ctr <10)
        {
           number = rand();
           ++ctr;
        }
        All you have to do now is insert code to check if the number is a multiple of 5 or is even.

        So this won't work:

        Code:
        if ((number %5) == 0)
        {
             --number is a multiple of 5
        }
        if ((number % 2) == 0)
        {
             -- number is even (a multiple of 2) --
        }
        because a number could be a multiple of 5 and even at the same time. To separate these cases use the "else" of the if statement:

        Code:
        if ((number %5) == 0)
        {
             --number is a multiple of 5
        }
        [B]else[/B] if ((number % 2) == 0)
        {
             (a multiple of 2) --
        }
        Place this code in the while loop between the call to rand() and the increment of the loop counter ++ctr.

        Comment

        • breed3000
          New Member
          • Nov 2014
          • 4

          #5
          thank you soo much weaknessforcats ! That helped me tremendously. I appreciate you taking time out of your day to be of assistance to me.
          I really need to read more on while and do while statements and execution any suggestions? Thank you so much

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            We have all been where you are now. Learning this just takes time and multiple exposures.

            Here's what I did:

            1) Pick any C++ book you think you can read.
            2) Read the book and work all the examples to see you get the same answer as the book
            3)Work all the problems in the book
            4) Never read the book again

            Repeat steps 1-4 using a different book.

            I was on book #6 when all the AHA! lights went on.

            Good luck.

            Comment

            • breed3000
              New Member
              • Nov 2014
              • 4

              #7
              Will do! Thank you so much for your patience and much needed suggestion.

              Comment

              Working...