So I have another problem that I am running into. I have been trying to fix this for about an hour with no luck. I cannot get this nested while loop to work. The code should be putting in lines according to the input but currently it goes on forever.
#include <iostream>
using namespace std;
void PrintLines(char characterValue, int characterCount, int lineCount);
// I'm going to have to change char characterValue to int characterValue
// will this still work if they are in seperate files?
void PrintLines(char characterValue, int characterCount, int lineCount)
{
while (lineCount--) //This is the problem
{
while (characterCount--)
{
cout << characterValue;
}
cout << "\n";
}
}
int main()
{
char Letter;
int Times;
int Lines;
cout << "Enter a capital letter: ";
cin >> Letter;
cout << "\nEnter the number of times the letter should be repeated: ";
cin >> Times;
cout << "\nEnter the number of Lines: ";
cin >> Lines;
PrintLines(Letter, Times, Lines);
return 0;
When I do this to check if it works properly. I see that it does...
while (lineCount--) //This is to check
cout << "\n%%%";
{
while (characterCount--)
{
cout << characterValue;
}
}
It prints :(If Lines = 4 and Times = 3 and Letter = A)
%%%
%%%
%%%
%%%AAA
while (lineCount--) //This is the problem
{
while (characterCount--)
{
cout << characterValue;
}
cout << "\n";
}
After the first iteration of lineCount, characterCount is negative. You keep decrementing it, and it will never reach zero again until it overflows.
Do:
while (lineCount--) //This is the problem
{
int tmpCount = characterCount;
while (tmpCount--)
{
cout << characterValue;
}
cout << "\n";
}