Thread: New to programming need some help

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    4

    Question New to programming need some help

    Hi guys,
    I just started out and I am having some difficulty getting my program to work. I was following an online guide to this program but it ended sorta midway and I am left quite baffled.
    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        int userAge;
        
        printf("How old are you?\n");
        scanf("%d", &userAge);
        
        char citizen[3] = {'\0'};
        printf("Are you a citizen of the USA?\n");
        scanf("%s", citizen);
        
       if (userAge >= 18 && citizen == ("yes")) {
                   printf ("Yup ya can vote boo. Go get 'em");
            }
       else {
                   printf("You are not eligible to vote. Soz.");
            }
        getchar ();
        getchar ();
        
        return 0;
    }
    The problem is the citizen variable. I want the user to input a positive or negative answer to the question but I am pretty sure I am doing it wrong. Can anyone help out? Plus if you could I would really appreciate if you could explain to me why do I need the & character before userAge?

    And of course I would greatly appreciate if you had any online course to recommend that would cater to my basic needs

    Have a great day!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well part of the problem may be that you don't have enough room in your citizen array. You only have room for two characters plus the end of string character. Also you can't use the comparison operator to compare two strings, you need to investigate strcmp().

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Plus if you could I would really appreciate if you could explain to me why do I need the & character before userAge?
    Code:
    scanf("%d", &userAge);
    '&' is the "address-of" operator. "scanf()" needs the address of the given variable so that the variable can be updated. You will learn more about this behavior when you study pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread