PrintShampooInstructions(): Function for Looping Cycles

  • Context: MHB 
  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function Loop
Click For Summary
SUMMARY

The forum discussion centers on the implementation of the function PrintShampooInstructions() in C++. This function takes an integer parameter numCycles and outputs specific instructions based on its value. If numCycles is less than 1, it prints "Too few." If greater than 4, it prints "Too many." For values between 1 and 4, it correctly prints the cycle number followed by "Lather and rinse." The main issue discussed is the incorrect output order, which can be resolved by printing the loop index instead of the remaining cycles.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with control flow statements (if-else, loops)
  • Basic knowledge of function definitions and parameters in C++
  • Experience with standard output streams in C++ (cout)
NEXT STEPS
  • Review C++ function definitions and parameter passing
  • Learn about C++ control flow and loop constructs
  • Explore debugging techniques for C++ programs
  • Study output formatting in C++ using cout
USEFUL FOR

Programmers, especially those learning C++, and educators looking for examples of function implementation and control flow in coding exercises.

Teh
Messages
47
Reaction score
0
Write a function PrintShampooInstructions(), with int parameter numCycles, and void return type. If numCycles is less than 1, print "Too few.". If more than 4, print "Too many.". Else, print "N: Lather and rinse." numCycles times, where N is the cycle number, followed by "Done.". End with a newline. Example output for numCycles = 2:

1: Lather and rinse.
2: Lather and rinse.
Done.



HTML:
#include <iostream>
using namespace std;

/* Your solution goes here  */
void PrintShampooInstructions(int numCyles) {
   int i = 1;
   if (numCyles < 1) {
      count << "Too few." << endl;
   }
   else if (numCyles > 4) {
      count << "Too many." << endl;
   }
   
   else {
          
     while (numCyles > 0) {
        count << numCyles << ": Lather and rinse."    << endl;  
        i++;
        numCyles--;

   }
   count << "Done." << endl;
   }
   return;
}int main() {
   PrintShampooInstructions(2);

   return 0;
}

Testing with 0Your output:
Too few.


Testing with 2



Expected output:
1: Lather and rinse.
2: Lather and rinse.
Done.



Your output:
2: Lather and rinse.
1: Lather and rinse.
Done.



Tests aborted.


Cant figure out how to change this:

2: Lather and rinse.
1: Lather and rinse.
Done.


Into:

1: Lather and rinse.
2: Lather and rinse.
Done.
 
Technology news on Phys.org
Print [m]i[/m] instead of [m]numCyles[/m].
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
12
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 3 ·
Replies
3
Views
5K