newbie stuff

This is a discussion on newbie stuff within the C Programming forums, part of the General Programming Boards category; program specifications: Write a C program to determine the eligibility of someone to vote. The program will prompt the user ...

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

    newbie stuff

    program specifications:
    Write a C program to determine the eligibility of someone to vote. The program will prompt the user about his/her age, citizenship, conviction status, and voter registration status, and depending on what the user inputs, will determine whether the voter is eligible to vote or not.

    I wrote a flow chart:
    I have attached it here...

    Based on my flowchart I tried to write this code (pasted below), which doesn't have any syntax errors that the devcpp compiler catches, but there is obviously a logic error somewhere that I can't see. I don't under stand where my problem is and my instructor for this class is...well let's just say he isn't going to help me.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char c, f, r;
      int age;
      
      printf( "The purpose of this questionnaire is to determine your eligibility to vote.\n" );
      puts( "Please enter your age: " );
      scanf( "%d", &age );
      if (age >=18){
                printf("Are you a legal citizen of the United States? Please enter Y or N\n");
                scanf("%c", &c);
                    }
                    if (c == 'y' || 'Y'){
                            printf("Are you registered to vote? Please enter Y or N\n");
                            scanf("%c", &r);
                                }
                                 if (r == 'y' || 'Y'){
                                    printf("Have you been convicted of any felonies? Please enter Y or N\n");
                                    scanf("%c", &f);
                                            }
                                        if ( f == 'n' || 'N'){
                                            printf("Thank you for completing this questionnaire! You are eligible to vote!\n");
                                                   }
                                        else if (f =='y'||'Y'){
                                            printf("You are NOT eligible to vote!\n");
                                                   } 
                                   else if (r == 'n' || 'N'){
                                      printf("You are NOT eligible to vote!\n");
                                                   }
                    else if (c == 'n' || 'N'){
                            printf("You are NOT eligible to vote!\n");
                                                   }
    else if (age <18){
         printf("You are NOT eligible to vote!\n");
               } 
    
      system("PAUSE");	
      return 0;
    }

    srry if this doesn't have the correct tags the sticky wanted, I'm new to all this and I didn't understand.

    Please help via post here or email LfgOgre@gmail.com

    EDIT: It looks sloppy because the post isn't reading the indents I put in...srry once again :/
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by lfgogre
    srry if this doesn't have the correct tags the sticky wanted, I'm new to all this and I didn't understand.
    I have added them in for you. Next time use [code][/code].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Code:
    if (c == 'y' || 'Y'){
    Won't work - you have to write out the condition in full each time:

    Code:
    if (c == 'y' || c == 'Y'){

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    6
    updated code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char c, f, r;
      int age;
      
      printf( "The purpose of this questionnaire is to determine your eligibility to vote.\n" );
      puts( "Please enter your age: " );
      scanf( "%d", &age );
      if (age >=18){
                printf("Are you a legal citizen of the United States? Please enter Y or N\n");
                scanf("%c", &c);
                    }
                    if (c == 'y' || c =='Y'){
                            printf("Are you registered to vote? Please enter Y or N\n");
                            scanf("%c", &r);
                                }
                                 if (r == 'y' || r =='Y'){
                                    printf("Have you been convicted of any felonies? Please enter Y or N\n");
                                    scanf("%c", &f);
                                            }
                                        if ( f == 'n' || f == 'N'){
                                            printf("Thank you for completing this questionnaire! You are eligible to vote!\n");
                                                   }
                                        else if (f =='y'||f == 'Y'){
                                            printf("You are NOT eligible to vote!\n");
                                                   } 
                                   else if (r == 'n' || r == 'N'){
                                      printf("You are NOT eligible to vote!\n");
                                                   }
                    else if (c == 'n' || c == 'N'){
                            printf("You are NOT eligible to vote!\n");
                                                   }
    else if (age <18){
         printf("You are NOT eligible to vote!\n");
               } 
    
      system("PAUSE");	
      return 0;
    }
    still doesn't execute properly, it "fails" correctly, but if the age is >18 it asks the next question then just ends w/o any user input

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I think an easier way to deal with this would be to ask the user all the questions first, THEN do the analysis of whether he is allowed to vote or not based on the input.

    Also note that your bracket-ing is a mess. Why not use this simple method:

    Code:
    if(something){
      .....
    }else{
        if(something else){
          ....
        }else{
    
        }
    }
    Last edited by claudiu; 05-31-2010 at 10:50 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    6
    Tried what you suggested:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char c, f, r;
      int age;
      
      printf( "The purpose of this questionnaire is to determine your eligibility to vote.\n" );
      puts( "Please enter your age: " );
      scanf( "%d", &age );
      printf("Are you a legal citizen of the United States? Please enter Y or N\n");
      scanf("%c", &c);
      printf("Are you registered to vote? Please enter Y or N\n");
      scanf("%c", &r);
      printf("Have you been convicted of any felonies? Please enter Y or N\n");
      scanf("%c", &f);
      if (age >=18 && c=='y' && f=='n' && r=='y'){
         printf("You are eligible to vote!\n");
               } 
               else if (age <18 || c=='n' || f=='y' || r=='n'){
                    printf("You are NOT eligible to vote!\n");
                    }
    
      system("PAUSE");	
      return 0;
    }
    did this and it still does the same thing, it will ask the age then ask the next two questions w/o pausing to get input.

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    The newline character is left in the standard input buffer, so you need to get rid of it.

    Code:
    int dummy;
    ...
    while ((dummy = getchar()) != '\n') ;
    following the scanf lines will eat everything left in the buffer, up to and including the newline.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Firstly, LEARN to indent:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char c, f, r;
    	int age;
    
    	printf( "The purpose of this questionnaire is to determine your eligibility to vote.\n" );
    	puts( "Please enter your age: " );
    	scanf( "%d", &age );
    	printf("Are you a legal citizen of the United States? Please enter Y or N\n");
    	scanf("%c", &c);
    	printf("Are you registered to vote? Please enter Y or N\n");
    	scanf("%c", &r);
    	printf("Have you been convicted of any felonies? Please enter Y or N\n");
    	scanf("%c", &f);
    	if (age >=18 && c=='y' && f=='n' && r=='y'){
    		printf("You are eligible to vote!\n");
    	} 
    	else if (age <18 || c=='n' || f=='y' || r=='n'){
    		printf("You are NOT eligible to vote!\n");
    	}
    
    	system("PAUSE");	
    	return 0;
    }
    Then, you may read SourceForge.net: Leaves data in input buffer - cpwiki which describes your problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    You might want to use variable names that are more descriptive. Also, since a person is either eligible to vote or not, there is no need to test for another condition before informing the user that he/she is not eligible to vote.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by rags_to_riches View Post
    The newline character is left in the standard input buffer, so you need to get rid of it.

    Code:
    int dummy;
    ...
    while ((dummy = getchar()) != '\n') ;
    following the scanf lines will eat everything left in the buffer, up to and including the newline.
    I'm sorry...don't blame this on me but...I have no idea what you're telling me. Blame Dr. Ruknet Cezzar

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by lfgogre
    I'm sorry...don't blame this on me but...I have no idea what you're telling me.
    Do you understand the statement that the "newline character is left in the standard input buffer"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Do you understand the statement that the "newline character is left in the standard input buffer"?
    I know what the newline character is, but sadly enough I have no idea about an input buffer...I'd ask my angry russian instructor but he'd just say the same thing he says to everyone else, "I can't tell you everything, you have to figure it out!"...but it would sound angry. So here I am...

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    We're talking about standard input, in this case the input from your keyboard. So what you do is that you typed in a number, and pressed enter. When you used scanf to read the number, the enter/newline was not read, and thus was read on the next scanf, instead of the character that you wanted to read.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by lfgogre View Post
    I know what the newline character is, but sadly enough I have no idea about an input buffer...I'd ask my angry russian instructor but he'd just say the same thing he says to everyone else, "I can't tell you everything, you have to figure it out!"...but it would sound angry. So here I am...
    Begin by reading the link I posted and then ask questions if you don't understand the contents.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lfgogre View Post
    I know what the newline character is, but sadly enough I have no idea about an input buffer...I'd ask my angry russian instructor but he'd just say the same thing he says to everyone else, "I can't tell you everything, you have to figure it out!"...but it would sound angry. So here I am...
    Elysia already pointed you to an explanation:

    Quote Originally Posted by Elysia View Post
    Then, you may read SourceForge.net: Leaves data in input buffer - cpwiki which describes your problem.
    Here's another:

    STDIN pitfalls

    Understand that this is probably the most frequent problem newbies at cboard run into, so people have prepared explanations for them rather than having to repeat the same thing over and over.

    If you have any questions about either of those articles, feel free to ask. But you'll have to read them first, it will not take that long.
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detailed newbie tutorials.
    By Hidden-Shadow in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2003, 03:07 AM
  2. the holy grail for newbie programmers...
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2003, 07:19 PM
  3. DirectX 8.1 SDK Tutorials And Stuff
    By c++_n00b in forum Game Programming
    Replies: 3
    Last Post: 04-06-2002, 08:01 AM
  4. newbie main(void)
    By Torvalds69 in forum C Programming
    Replies: 10
    Last Post: 04-02-2002, 03:06 PM

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