Hello!
I have completed a basic program, and it consists of a while loop and it runs sufficiently. I am trying to add a loop so that the user is prompted to rerun the program. This additional loop is causing unwanted behavior. I think I am just tired, but I had it fixed then broke it again. Now I am stuck.
suggestions are appreciated.
Code:
int main()
{
char temple[15][5] = { ".-","-" };
char input[25];
char letnum[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6',
'7','8','9','0',' ' };
int i = 0, j, k;
cout << "Please enter a message to be converted to temple Code,";
cout << "\nand no more than 50 characters in length: ";
cin.getline(input, 15);
while (input[i] != '\0')
{
for (j = 0; j < 15; j++)
{
if (toupper(input[i]) == letnum[j])
{
for (k = 0; k < strlen(temple[j]); k++)
cout << temple[j][k];
cout << " ";
}
}
i++;
}
cout << endl;
system("pause");
return 0;
}
I want to opt the user to run or not run the program, so I have this:
Code:
int main()
{
char again; //User loop option
do // do ... while
{
char temple[15][5] = { ".-","-...",};
char input[25];
char letnum[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','1','2','3','4','5','6',
'7','8','9','0',' ' };
int i = 0, j, k;
cout << "Please enter a message to be converted to temple Code,";
cout << "\nand no more than 50 characters in length: ";
cin.getline(input, 15);
while (input[i] != '\0')
{
for (j = 0; j < 15; j++)
{
if (toupper(input[i]) == letnum[j])
{
for (k = 0; k < strlen(temple[j]); k++)
cout << temple[j][k];
cout << " ";
}
}
i++;
}
cout << endl;
cout << "Do you want to run the program again? (Y/N) "; // prompts user
cin >> again; // input
} while (tolower(again) == 'y'); // for case, calls 'again'
system("pause");
return 0;
}
When I add the do...while, it loops in an undesired way. It does not allow the user to enter values. It just skips my getline and goes to the cin>>again.