Loops

So I have been trying to solve for a long time. But,without any luck. Any help is appreciated.
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.

Hint: Define and use a loop variable.

So, this is what I have done.
#include <iostream>
using namespace std;
void PrintShampooInstructions( int numCycles ) {
if (numCycles < 1) {
cout << "Too few." << endl ;
}
if (numCycles > 4 ) {
cout << "Too many." << endl ;
}
else {
int i = 0 ;
int N = 0 ;
for ( i = N ; i < numCycles ; ++i) {
cout << "i: Lather and rinse." << endl ;
cout << "Done." << endl ;
}
}
}
return ;
}
int main() {
PrintShampooInstructions(2);

return 0;
}

But, it doesn't give the required solution. I am so confused!
A few issues here:

Your if statements output a message if the numCycles is too big or too small, but only the second if statement skips the instructions (because of the else) if the value is wrong. It's cleaner to execute a return statement to exit the function.
1
2
3
4
5
6
7
8
  if (numCycles < 1) 
  {  cout << "Too few." << endl ; 
      return;
  } 
  if (numCycles > 4 ) 
  {  cout << "Too many." << endl ; 
      return;
  } 


Your cout statement is going to output the letter i, not the value of i. You have i inside the quoted string.
cout << "i: Lather and rinse." << endl ;
What you want is this:
cout << i << ": Lather and rinse." << endl ;

You want this AFTER the for loop, not inside it.
cout << "Done." << endl ;
By having it inside, you're going to repeat it for each iteration of the loop.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Thank you so much for your help!
Sorry, I will use the code tags next time! :)
Topic archived. No new replies allowed.