-
ok. I see why the while loop doesn't work. if I use the same code with a do ... while I end up with the same results it runs just one time. Why might that be? example:
Code:
scanf ("%c", &letter);
select = letter;
select = tolower(select);
do
{
if (select == 'f')
printf ("\t Fill\n");
else if (select == 'p')
printf ("\t Print\n");
else if (select == 's')
printf ("\t Sort\n");
else if (select == 'q')
printf ("\t Query\n");
else
}while ( select == 'z')
printf ("\tYou are finished.\n");
-
Because there still isn't a way for select to change inside the loop. Stop guessing, and think for a minute: the scanf/select bit must be inside the loop.
-
Pleae forgive me. I do not want to offend. I really am just trying to understand this more completely. I read the text and I am still lost at times. So I did as you suggested:
Code:
do
{
scanf ("%c", &letter);
select = letter;
select = tolower(select);
if (select == 'f')
printf ("\t Fill\n");
else if (select == 'p')
printf ("\t Print\n");
else if (select == 's')
printf ("\t Sort\n");
else if (select == 'q')
printf ("\t Query\n");
else
}while ( select == 'z');
printf ("\tYou are finished.\n");
Now that I have alter the code it now will not run at all. The program asks me to place a semicolon after the last else statement. I know this to be incorrect coding and not the code will not run at all, I remove the semicolon and I get 1 error still. This is one of the most challenging classes I have taken and this code is stumping me at just about every turn I take. I do appreciate the help you offer. Thank you in advance.
-
If you're not using the last else, then just take it out.
I don't know what you mean by "the code will not run at all" when you take out the semicolon. You don't have a prompt printed, at least not in the loop, so it's just going to wait for you to type something. Or maybe there's something else going on outside the loop that you haven't posted.
-
ok. I do apologize. here is all the code.
Code:
/*Create the menu of the main function. This is where the user selects the
function they want to us to fill, sort, search or print an array.
Continuation of arrays.cpp which works
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//Function Declarations
void functionUse (char choice);
int main (void)
{
//Local Declaration
char letter;
char select;
//Statements
printf ("\t\tMenu\t\t\n");
printf ("=================================================\n\n");
printf ("Select one of the following options:\n");
printf ("\t F. Fill array with a random number series\n");
printf ("\t P. Print the array\n");
printf ("\t S. Sort the array\n");
printf ("\t Q. Query the array\n");
printf ("\t Z. Terminate the program\n");
do
{
printf ("\tYour choice is: %2c");
scanf ("%c", &letter);
select = letter;
select = tolower(select);
if (select == 'f')
printf ("\t Fill\n");
else if (select == 'p')
printf ("\t Print\n");
else if (select == 's')
printf ("\t Sort\n");
else if (select == 'q')
printf ("\t Query\n");
}while ( select == 'z');
printf ("\tYou are finished.\n");
system("PAUSE");
return 0;
} //main
I get two error messages and the is the message I get and it will not print the menu now.
I again appreciate your help.
-
You have a %2c in your printf format string, but you do not give it a character to print, so I'm guessing that's one error. I don't see any others, nor does my compiler here, so you'll have to actually cough up the other error message.
As to the looping, you loop while select is z, which is the opposite of what you want.