Thread: What am I doing Wrong?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    What am I doing Wrong?

    I am trying to use an if ... else statement in order to sequence choices. However regardless of what my choice it returns the Fill response. What have I done wrong in 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>
    
    //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");
    	printf ("\tYour choice is:  %2c");
    	
    	select = scanf ("%c", &letter);
    
           if (select == 'F'||'f')
    		   printf ("\t Fill\n");
    	   else  
    		  {
    		   if (select == 'P' || 'p')
    		       printf ("\t Print\n");
    	       else
    		     {
    			   if (select == 'S' || 's')
    		           printf ("\t Sort\n");
    	           else
    			    {   if (select == 'Q' || 'q')
    		                printf ("\t Query\n");
    				    else;
    		                printf ("Your are finished.");
    			    } //else s
    		      }//else p
    	       }//else f
    
    	system("PAUSE");
    	return 0;
    } //main
    Would appreciate any help.
    Thank You.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First: http://cpwiki.sourceforge.net/index.php/Indentation
    2nd:
    Code:
    if (select == 'F'||'f')
    Doesn't work that way. Your statement reads: If select == 'F' OR 'f', but in C/C++, it must be If select == 'F' OR select == 'f', so in other words, it should be:
    Code:
    if (select == 'F' || select == 'f')
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    Elysia,

    I reformatted the code so that it incorportates the
    Code:
     if (select == 'F'|| select == 'f')
                  printf ("\t Fill\n");
    did the changes all the way through. Now it just responds with you are finished.
    Why would that be.

    I do apologize for the appearance of my program. This is the format required by our instructor.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    I also copy and pasted from my program and it doesn't look exactly how I presented in my code on my screen why may that be?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by silhoutte75 View Post
    Elysia,

    I reformatted the code so that it incorportates the
    Code:
     if (select == 'F'|| select == 'f')
                  printf ("\t Fill\n");
    did the changes all the way through. Now it just responds with you are finished.
    Why would that be.
    Because you read the user input into letter, and not select. scanf returns the number of results read, not what those results actually are.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    Thank you Tabstop & Elysia. You both have helpped I truly appreciate it.

    Silhoutte75

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by silhoutte75 View Post
    I do apologize for the appearance of my program. This is the format required by our instructor.
    Indentation is never required by your teacher.
    Try to fix it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Quote Originally Posted by silhoutte75 View Post
    I also copy and pasted from my program and it doesn't look exactly how I presented in my code on my screen why may that be?
    Because you're mixing tabs and spaces, and tabs have different sizes in different places. If you want your code to look the same everywhere, use only spaces. If you use only tabs your code won't look exactly the same everywhere, but it most likely will look good everywhere, plus tabs are a bit easier to use in my opinion.
    Bottomline: use either tabs or spaces, not both.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Elysia View Post
    Indentation is never required by your teacher.
    Try to fix it.
    Unless you're programming in Python or something. Then it's required by your interpreter.

    Rather than using
    Code:
    if (select == 'F' || select == 'f')
    you could also convert select to uppercase or lowercase beforehand.
    Code:
    #include <ctype.h>  /* at the beginning of your program */
    select = tolower(select);
    if(select == 'f')
    You should know that system("PAUSE") is non-standard . . . [edit] http://cpwiki.sourceforge.net/index.php/Pause_console [/edit]

    By the way, this looks like C code, so what is it doing in the C++ forum?

    [edit]
    Because you're mixing tabs and spaces, and tabs have different sizes in different places. If you want your code to look the same everywhere, use only spaces. If you use only tabs your code won't look exactly the same everywhere, but it most likely will look good everywhere, plus tabs are a bit easier to use in my opinion.
    Bottomline: use either tabs or spaces, not both.
    Best of all: use an editor that converts tabs to spaces automatically or somesuch . . . . [/edit]
    Last edited by dwks; 01-19-2008 at 04:30 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sourceforge.net/index.php/Indentation
    Covers spaces vs. tabs shortly. Try to read and choose your style.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Something else you could consider if you wished to:

    Code:
    if ( tolower(select) == 'f' ) 
      // ...
    I always found it more favourable than ||. But that's just me. Can't remember in which header the function is found. toupper also exists if you prefer to work with upper case letters

    edit: Forgot to post early and apparently others posted before.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Do ... While problem...

    Code:
    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");
    Ok. I got it to run throught the if else statements, improved the look of the code and now I want it to loop. I used the do while loop and it just stops after one run. Why would this occur?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How does select change in that do-while loop?

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    69
    select is changed by the input of the user. in the menu file.

    Code:
    printf ("\tYour choice is:  %2c");
    	scanf ("%c", &letter);
    
    	select = letter;
    	select = tolower(select);
    
    	while ( select != 'z')
    	  {
    	  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
    	  }
    	printf ("\tYou are finished.\n");
    I also played with a while statement,but it did the same returned the choose function but only one run is achieved.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I still don't see a scanf inside the while loop. We are talking about this while loop, right?
    Code:
    	while ( select != 'z')
    	  {
    	  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
    	  }
    This loop will run either 0 or infinity times, since select can't change once you get inside of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03: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, 12: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, 01:04 PM