Thread: Interesting program, but stuck somewhere

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Interesting program, but stuck somewhere

    I need to output the first name, middle initial and the last name. It is not working. I don't know where I got wrong.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    /*
    Yog R. Panta
    Chapter#9: Lowercase to Uppercase
    04/27/2005
    */
    int main()
    {
    char letter;
    printf("\Please enter 1 in the program to exit the program");
    printf("\n-------------------------------------------------");
    printf("\nPlease enter a letter: ");
    fflush(stdout);
    letter=getchar();
    if (letter!='1')
    {
    while(letter!='1')
     {
      if(isalpha(letter)!=0)
      {
       letter=toupper(letter);
       printf("\nValid Entry! The letter in uppercase is: ");
       putchar(letter);
      }
      else if(isalpha(letter)!==0)
       printf("\nSorry, Invalid Entry!");
       getchar();
       printf("\n\nEnter another letter: ");
       fflush(stdout);
       letter=getchar();
     }
    }
    if(letter=='1')
    printf("\nEnd of the program");
    return 0;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    FAQ > Explanations of... > Why fflush(stdin) is wrong
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    If you want to get a whole string instead of just a single character, use scanf(). That function can also read in single characters.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by joshdick
    FAQ > Explanations of... > Why fflush(stdin) is wrong
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Where do you see ffluh(stdin)?

    [edit]And to read a string, generally avoid scanf until you really know its issues.
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    http://www.eskimo.com/~scs/C-faq/q12.20.html

    [oops]And getchar returns an int.
    Last edited by Dave_Sinkula; 04-27-2005 at 08:26 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by Dave_Sinkula
    Where do you see ffluh(stdin)?
    My bad.

    [edit]And to read a string, generally avoid scanf until you really know its issues.
    A valid point, but I think the poster's intention was to get a program up and running, not make it idiot-proof.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by joshdick
    A valid point, but I think the poster's intention was to get a program up and running, not make it idiot-proof.
    Ah, the same argument for the use gets.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Here it is again

    It is strange that the the prinf statement doesn't work..I have to enter twice to show that message up.


    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    /*
    Yog R. Panta
    Chapter#9: Lowercase to Uppercase
    04/27/2005
    */
    int main()
    {
    char letter;
    printf("\Please enter 1 in the program to exit the program");
    printf("\n-------------------------------------------------");
    printf("\nPlease enter a letter: ");
    fflush(stdout);
    letter=getchar();
    if (letter!='1')
    {
    while(letter!='1')
     {
      if(isalpha(letter)!=0)
      {
    	letter=toupper(letter);
       printf("\nValid Entry! The letter in uppercase is: ");
    	putchar(letter);
      }
      else if(isalpha(letter)!=0)
    
    	printf("\nSorry, Invalid Entry!");
    	printf("\nInvalid Entry!");
    	getchar();
    	printf("\n\nEnter another letter: ");
    	fflush(stdout);
    	letter=getchar();
     }
    }
    if(letter=='1')
    printf("\nEnd of the program");
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. An interesting challenge --> A word search program
    By desipunjabi in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 03:30 PM
  3. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM
  4. Interesting little exercise...
    By biterman in forum C Programming
    Replies: 13
    Last Post: 10-18-2001, 01:22 AM