How many loops?

This is a discussion on How many loops? within the C++ Programming forums, part of the General Programming Boards category; This isn't homework, I'm working through the Malik book on my own. Am I supposed to use one big loop ...

  1. #1
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79

    How many loops?

    This isn't homework, I'm working through the Malik book on my own.

    Am I supposed to use one big loop for this whole task, or a seperate loop for each sub-task?

    Code:
    //******************************************************************************
    // Malik Chapter 5 programming exercise 8
    // Pretty stinkin' involved!
    // 'Write a program that uses a while loop to perform the following steps:
    //******************************************************************************
    
    #include<iostrea>
    using namespace std;
    
    int main()
    {
        //a. Prompt the user to input two integers (smaller one first)
        int firstNum, secondNum;
        
        //b. Output all odd numbers between firstNum and secondNum.
        
        //c. Output the sum of all even numbers between firstNum and secondNum.
        
        //d. Output the number and their square between 1 and 10.
        
        //e. Output the sum of the square of odd numbers between firstNum and secondNum.
        
        //f. Output all uppercase letters.
    Huh?

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    a probably needs a separate loop, so does f because it doesn't rely on firstNum and secondNum. b through e can be in the same loop, but it might be simpler to split them up into four loops. Try both and see.

  3. #3
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Thanks Noir

    I couldn't figure out how to use a loop for step a but the rest was easy enough.
    This would make more sense using functions but that's the next chapter.
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    
        //a. Prompt the user to input two integers (smaller one first)
        int firstNum, secondNum;
        cout<<"Please enter two integers, smaller one first: ";
        cin>>firstNum>>secondNum;
        cout<<endl;
        
    
        //b. Output all odd numbers between firstNum and secondNum.
        int x = firstNum;
        while(x <= secondNum)
        {if(x%2)
         cout<<x<<" ";
         x++;}
         cout<<endl<<endl;
        
        //c. Output the sum of all even numbers between firstNum and secondNum.
        int y = firstNum;
        int sum = 0;
        while(y <= secondNum)
        { if(!(y%2))
          sum = sum + y;
           y++;}
        cout<<"Sum of even numbers between "<<firstNum<<" and "<<secondNum
            <<" is "<<sum<<"."<<endl<<endl;
        
        //d. Output the numbers and their square between 1 and 10.
        int z = 1;
        while(z <= 10)
         {cout<<z<<" squared = "<<z*z<<endl<<endl;
         z++;}
        
        //e. Output the sum of the square of odd numbers between firstNum and secondNum.
          int a = firstNum;
          int sqr = 0;
          sum = 0;
        while(a <= secondNum)
        {if(a%2)
         {sqr = a * a;
         sum = sum + sqr;}
         a++;}
         cout<<"The sum of the square of odd numbers between your numbers is "
             <<sum<<"."<<endl<<endl;
           
        //f. Output all uppercase letters.
        char letter = 'A';
        while(letter <= 'Z')
        {cout<<letter; letter++;}
        
        cin.ignore();
        cin.get();
        return 0;
    }
    Huh?

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    For a I was thinking of an error handling loop:
    Code:
    while ( true ) {
      cout << "Please enter two integers, smaller one first: ";
    
      bool rc = cin >> firstNum >> secondNum;
    
      if ( !rc ) {
        cin.clear();
        cin.ignore( 256, '\n' );
        cout << "error, try again\n";
        continue;
      } else if ( firstNum > secondNum ) {
        swap( firstNum, secondNum );
      }
    }

  5. #5
    -JM
    -JM is offline
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    I agree, error handling of input is important but I wasn't sure how to do it.

    The loop you suggest keeps displaying

    Please enter two integers, smaller one first:
    no matter what the input is.

    I don't understand some of your code so I don't know why it doesn't terminate.
    Huh?

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    First, there's no output expression, so even if the swap occurs, you're not going to see it on screen.

    Second, "while(true)" is supposed to be infinite because the condition is hard coded. You'll either have to make it a conditional dependent on some variable or you'll have to embed a "break" statement somewhere inside the loop pending some fulfilled condition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21