Thread: Help a Newbie with my Dumb little Program

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    Help a Newbie with my Dumb little Program

    Hi, very new to C and practicing some if statements. Here is my code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char answer;
    	printf ("Are you a Miami Dolphins fan Y/N?\n");
    	scanf ("%c", &answer);
    	if (answer == 'y')
    	{
    	printf ("Good choice they will be Super Bowl Champs in 2010\n");
    	}	
    	else if (answer == 'n')
    	{
    		printf ("As long as your not a Jets fan you're all right by me\n");
    	}
    
    	else if (answer)
    	{
    		printf ("Invalid input, please enter Y/N:\n");
    	}
    return 0;
    }
    I would like to set this up such that if the user doesn't select y or n initially they get the 3rd response. Then, I would like the program to continue and give the user a 2nd chance and output the correct response based on the y or n answer. I'm thinking a do while loop with the 3rd response being moved to first and not coming out o the loop unless y or n is selected. Any ideas?

    Also is there a spot where I can find simple little programs to write and execute as practice?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could use:
    Code:
    char answer = 0;
    while (answer != 'y' || answer != 'n') {
    Notice, you must make sure first that answer isn't 'y' or 'n' to start with, which could happen if it is undefined.

    The next thing that will happen is wrong answers will cause the question to be repeated twice before you can answer it again. This is because a '\n' will be left in the stdin buffer after the scanf() call.

    At that point you'll want to read this:
    STDIN pitfalls
    which is a short explanation of the issue and some possible solutions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Look out, MK is flying - and feeling pretty righteous about it, too!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    76
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char answer = 0;
    	printf ("Are you a Miami Dolphins fan Y/N?\n");
    	scanf("%c", &answer);
    	getchar(); 
    	while (answer != 'y' && answer != 'n')
    	{
    	printf ("Invalid input, please enter Y/N:\n");
    	scanf("%c", &answer);
    	getchar();
    	}
    	if (answer == 'y')
    	{
    	printf ("Good choice they will be Super Bowl Champs in 2010\n");
    	}
    	else if (answer == 'n')
    	{
    	printf ("As long as your not a Jets fan you're all right by me\n");
    	}
    return 0;
    }
    Got it, does anyone know where I can get some simple program ideas for practice?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Adak View Post
    Look out, MK is flying - and feeling pretty righteous about it, too!
    Yes, if only I could spell these brand names correctly

    Quote Originally Posted by dolfaniss View Post
    Got it, does anyone know where I can get some simple program ideas for practice?
    Check in the kitchen One of the first exercises in the first book I read on C was to create a "note taking" program, which saved per note files in a directory and allowed you to browse them, etc, via a simple menu on the command line. Lots of ways you can expand upon the idea (I think security was a theme in the book, so per user checks were incorporated).

    The better you get, the more you know you can do and the ideas will flow from there. A similar program to the "note taker" is the address book, also very simple. It just involves managing input and output (again: to files*)and learning how to write functions. After that maybe you want to learn GUI interfaces and the real fun begins. I would bet there is a very high percentage of modern young programmers out there with their own system of organizing mp3's thru various little utilities.

    Also there are lots of algorithmic puzzles and stuff around, "Tower of Hanoi" and stuff that may help you with logic. Sorting and data structures -- but you may want to hold off on that till you've written a few more simple programs.

    * if you haven't deal with file I/O yet that's what you should do next. Read files, output data, input data, output files.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-02-2009, 09:46 PM
  2. Newbie help with expontential program
    By BSmith4740 in forum C Programming
    Replies: 28
    Last Post: 06-11-2008, 06:54 PM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. newbie : compiling a C++ program in linux
    By gemini_shooter in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2005, 02:45 PM
  5. newbie: steps to create and compile c program in SUSE
    By gemini_shooter in forum Linux Programming
    Replies: 12
    Last Post: 06-22-2005, 06:35 PM