help with program/scanf function

This is a discussion on help with program/scanf function within the C Programming forums, part of the General Programming Boards category; For some reason my scanf function will not work properly. The first scanf works, if i hit 'a' it will ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    help with program/scanf function

    For some reason my scanf function will not work properly. The first scanf works, if i hit 'a' it will go to that case, same thing for 'd' and 'q'.

    I enter 'a' to go to the first case, but no matter what i enter into that scanf function it skips down to the else and says 256 is not a valid entry.....

    What is wrong, someone pls help me.
    It is probly something stupid so forgive me cuz im new to this...

    I tried replacing scanf with getchar, the first one would work fine, but the second one would get skiped over????????

    Maybe i should use a different function?



    Code:
    #include <stdio.h>
    
    main ()
    {
    	int s1, s2, name, number;
    
    	clrscr ();
    	printf ("                MAIN MENU                 \n\n");
    	printf ("[A]dd Records\n");
    	printf ("[D]isplay Records\n");
    	printf ("[Q]uit Program\n\n");
    	printf ("Selection: ");
    
    	scanf("%c", &s1); //the first scanf
    	
    	switch (s1)
    	{
    	
    		case 'a':	clrscr ();
    				printf ("               ADD RECORDS                \n\n");
    				printf ("Enter [N]ame\n");
    				printf ("Enter [P]hone Number\n");
    				printf ("[R]eturn To Main Menu\n\n");
    				printf ("Selection: ");
    				scanf ("%c", &s2); //the second scanf, the one that doesn't work
    
    				if (s2 == 'n')
    				{
    					clrscr ();
    					printf ("                 ADD NAME                 \n\n");
    					printf ("Enter Name <Lastname, Firstname>: ");
    
    				}
    			
    				if (s2 == 'p')
    				{
    					clrscr ();
    					printf ("             ADD PHONE NUMBER             \n\n");
    				}			
    
    				if (s2 == 'r')
    				{
    					clrscr ();
    				}
    				else
    				{
    					clrscr ();
    					printf ("%c Is Not A Valid Entry, Please Select Again\n", s2);
    					printf ("Selection: ");
    				}
    
    			
    			
    			
    		case 'd':	printf ("             DISPLAY RECORDS              \n\n");
    				break;
    
    
    		case 'q':	clrscr ();
    				printf ("Thank You, BYE\n\n");
    				break;
    
    
    		default:	clrscr ();
    				printf ("%c Is Not A Valid Entry, Please Select Again\n", s1);
    				printf ("Selection: ");
    				break;
    
    	}
    		
    
    return 0;
    }
    Thanks for you time and help ppl

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    9
    > scanf("%c", &s1); //the first scanf
    Let's change to scanf("%d", &s1);
    if you declare s1 as int.

    Or you keep scanf, but change the declaration
    e.g. int name, number;
    e.g. char s1,s2;

    int ,use %d
    char, use %c
    float, use %f.......you better look up help on your compiler.

    To get char like 'a','q'... from user,
    Code:
    int c;
    c = getchar();
    switch(c)
    {  
      case 'a': /*......*/
           break;
      case 'b': /*......*/
           break;
      /* something like that */
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    9
    Do you require the user to hit 'a', then press ENTER?
    Or when user press 'a', program immediately print out
    ADD RECORDS
    Enter [N]ame
    Enter [P]hone Number
    [R]eturn To Main Menu.....

    Perhaps you consider getch() and getche() in conio.h for your program applications.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2
    getch (); worked.... thanks for all the help guys
    Last edited by CDavis; 04-02-2003 at 11:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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