MHB PrintShampooInstructions(): Function for Looping Cycles

  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function Loop
AI Thread Summary
The discussion revolves around creating a function called PrintShampooInstructions() that takes an integer parameter numCycles and has a void return type. The function should handle three scenarios based on the value of numCycles: if it is less than 1, it should print "Too few."; if it exceeds 4, it should print "Too many."; and for values between 1 and 4, it should print "N: Lather and rinse." for each cycle, followed by "Done." after the cycles are completed. A code snippet is provided, but there is an issue with the output order when numCycles is greater than 1. The current implementation prints the cycle number in reverse order. The solution involves using a separate counter variable, i, to correctly print the cycle number starting from 1 up to numCycles. The corrected output should reflect the proper sequence of cycle instructions.
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) {
      cout << "Too few." << endl;
   }
   else if (numCyles > 4) {
      cout << "Too many." << endl;
   }
   
   else {
          
     while (numCyles > 0) {
        cout << numCyles << ": Lather and rinse."    << endl;  
        i++;
        numCyles--;

   }
   cout << "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].
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top