Thread: switch statements.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    Smile switch statements.

    trying to write a program with a switch statement which is a type of guessing game. new to c. The program contains a stored variable and the user will have to input the number and a switch statement will be prompting higher and lower. I have managed to make a skeleton program and it compiles. but it just does nothing when the number is input.


    any help would be great !

    Code:
    #include <stdio.h>
    #include "/Volumes/Macintosh HD/csci1401.h"
    
          main()
          {
             int i,n,a;
    
             printf("Enter a number\n"); n = getInt();
    	 
    	 a=7; 
    	 if (a==n)
    	 {
    		 printf("You have guessed the right answer 7 \n");
    		 
    	 }
    	 
    	 else
    	 {
    	 
    		 
                switch(a)
    	    
    	    {
    	    case 0: n>7; printf("Lower"); break; 
                case 1: n<7; printf("higher"); break; 
                default:n==7;printf("you have guessed rite"); 
    	    
    	    }
    	    
    	
    	 }
    	 
          }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Some basic info first:

    main() should be int main(void);
    Code:
    if (a == n) {
      printf("You guess right");
      return 0; //<======  Gets you out of the program.
    }

    Your switch statement and getInt() needs to be in a while loop.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You've totally misunderstood the syntax of switch. Re-read the appropriate section in your tutorial/book.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. Switch Statements
    By lazyturtle in forum C++ Programming
    Replies: 12
    Last Post: 05-02-2007, 02:40 AM
  3. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 02:59 PM
  4. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM