Thread: Doubt with scaning characters

  1. #1
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70

    Doubt with scaning characters

    Hi all, for a CS project I need to make a menu, so the user must input values from 1 to 12 and q to exit the program.

    My main problem is that I am using getchar() to scan the menu selection.

    So inputs from 1 to 9 and q works, but 10 to 12 doesn't since getchar only supports ONE character.

    My first changes were to establish the switch cases to integers between range of 10 to 12.

    But still I need a suggestion of how to make the input 10, 11 and 12 to work.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "prints.h"
    #include "exams.h"
    #include "guessyn.h"
    #include "guessmn.h"
    #include "diceg.h"
    #include "largestn.h"
    #include "distance.h"
    #include "areapara.h"
    #include "areatrape.h"
    #include "perimepoly.h"
    #include "arrayv.h"
    #include "arrayc.h"
    
    int main ()
    {
    	int selection;
    	do{
    	printf("Choose a skill to practice:\n");
    	printf("1) Print shapes\n");
    	printf("2) Exam score\n");
    	printf("3) I guess your number\n");
    	printf("4) Guess my number\n");
    	printf("5) Dice game\n");
    	printf("6) Largest number\n");
    	printf("7) Distance\n");
    	printf("8) Area of parallelogram\n");
    	printf("9) Area of trapezoid\n");
    	printf("10) Perimeter of polygon\n");
    	printf("11) Array vowel\n");
    	printf("12) Array consonant\n");
    	printf("q) Quit\n");
    	printf("\n");
    	printf("Enter selection: ");
    	selection = getchar();
    	while ( getchar() != '\n');
    	while ( selection != '1' && selection != '2' && selection != '3' && selection != '4' && selection != '5' && selection != '6'
    	&& selection != '7' && selection != '8' && selection != '9' && selection != 10 && selection != 11 && selection != 12
    	&& selection != 'q' )
    	{
    		printf("\n");
    		printf("Wrong input!!! Please try again...\n");
    		printf("\n");
    		printf("Enter selection: ");
    		selection = getchar();
    		while ( getchar() != '\n');
    	}
    	printf("\n");
    	switch( selection ){
    	case '1': prints(); break;
    	case '2': exams(); break;
    	case '3': guessyn(); break;
    	case '4': guessmn(); break;
    	case '5': diceg(); break;
    	case '6': largestn(); break;
    	case '7': distance(); break;
    	case '8': areapara(); break;
    	case '9': areatrape(); break;
    	case 10: perimepoly(); break;
    	case 11: arrayv(); break;
    	case 12: arrayc(); break;
    	case 'q': 
    		printf("Thanks for playing!");
    		return EXIT_SUCCESS;
    	}
    	printf("\n");
    	}while( selection == selection );
    }
    Changes I made are in red

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of using getchar(), use say, scanf() to read into an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by laserlight View Post
    Instead of using getchar(), use say, scanf() to read into an int.
    That's a good suggestion, but I need to still maintain characters for the 'q' value.

    What I'm trying to do is find a way that both characters and integers can be stored in a integer.
    Last edited by georgio777; 11-16-2009 at 11:38 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah yes, I did not notice that. In that case, you have do a little parsing, methinks. Read the input as a string, then determine if it is a number or a letter. If it is a letter, that is easy, you just use it. If it is a number, you then convert it to the number value, e.g., by using strtol() (which can actually be used to determine if the input is a number).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by laserlight View Post
    Ah yes, I did not notice that. In that case, you have do a little parsing, methinks. Read the input as a string, then determine if it is a number or a letter. If it is a letter, that is easy, you just use it. If it is a number, you then convert it to the number value, e.g., by using strtol() (which can actually be used to determine if the input is a number).
    Thanks! Can you give me an example of how to use strtol().

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    Registered User georgio777's Avatar
    Join Date
    Sep 2009
    Posts
    70
    Quote Originally Posted by BEN10 View Post
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM