Thread: Help with alphabetic to numeric

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Help with alphabetic to numeric

    OK..guys this one has really stumped me!! I tried going through the logic and using a switch function, but I have been getting some crazy outputs. Plus I have to incorporate a "toupper" function inside it. Is that correct place?

    **NOTE: I can not use strings or anything else, b/c we have not learned them yet.**

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main()
    {
    
    char ch;
    int i = 0;
    
    printf("Enter a phone number: ");
    while ( ch = getchar() != '\n' );
    
    ch = toupper(ch);	
    	
    switch ( ch ) {
    
    	case 'A': case 'B': case 'C':
    	i = 2;
    	break;
    
    	case 'D': case 'E': case 'F':
    	i = 3;
    	break;
    
    	case 'G': case 'H': case 'I':
    	i = 4;
    	break;
    
    	case 'J': case 'K': case 'L':
    	i = 5;
    	break;
    
    	case 'M': case 'N': case 'O':
    	i = 6;
    	break;
    
    	case 'P': case 'R': case 'S':
    	i = 7;
    	break;
    
    	case 'T': case 'U': case 'V':
    	i = 8;
    	break;
    
    	case 'W': case 'X': case 'Y':
    	i = 9;
    	break;
    
    	default:
    		printf("%d", i);
    		break;
    	}
    
    printf("%d", i );
    
    }
    Thanks for any help!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> while ( ch = getchar() != '\n' );

    Look at the semicolen at the end of that statement. The loop stops there, as they say.

    Also, to avoid nasty side effects, always form conditionals like the above like:

    Code:
    while ( (ch = getchar()) != '\n' )
    {
     // code
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Help with alphabetic to numeric

    Originally posted by CrackerJack
    Code:
    ch = toupper(ch);	
    	
    switch ( ch ) {
    can also be written as:

    Code:
    switch (toupper(ch) ) {
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    You also need some extra brackets in your while statement..
    Code:
      while( ( ch = getchar()) != '\n' )
      {
         ...
    I compiled your code without the brackets (but with the other corrections) and still got a load of rubbish out.
    There is no such thing as a humble opinion

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Yeah...there was a bunch of junk wrong w/ it!!! I sat down w/ my teacher for almost an hour and we finally figured out what was wrong. Using the printf in the default just would not work, so we inserted into the case statements. Doesn't look all that pretty, but it works...

    JOE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  2. Send a numeric
    By nick048 in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-09-2007, 05:42 PM
  3. Simple vector class problem, please help
    By fidodidohk in forum C++ Programming
    Replies: 9
    Last Post: 03-30-2007, 09:13 AM
  4. only accept numeric data
    By willc0de4food in forum Windows Programming
    Replies: 6
    Last Post: 08-19-2005, 03:38 AM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM