What am I doing Wrong?

This is a discussion on What am I doing Wrong? within the C++ Programming forums, part of the General Programming Boards category; ok. I see why the while loop doesn't work. if I use the same code with a do ... while ...

  1. #16
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    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");

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    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.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  5. #20
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    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:  &#37;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.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21