Thread: Using If and Else Statements

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    4

    Post Using If and Else Statements

    Hi all,

    I am trying to work on a surprise for my boyfriend for Valentines. He is in computer programming so I want to write a little program scavenger hunt kinda thing for him.

    To get started I want to have him enter his name. If it is his name it will allow him to continue and will say something like "hey you". If it is a different name it will say "invalid". I am having trouble getting it to work though.

    Here is the code I have.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
            char name[100];
            printf("Enter your name: ");
            fgets(name, 100, stdin);
            if (strcmp(name, "Brandon")==0 );
    {       
            printf("hey youuu");
    }
    else
    {
            printf("Invalid");
    }
            return 0;
    }
    
    


    Please help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > if (strcmp(name, "Brandon")==0 );
    Remove the ; from the end of this line.

    Also, be aware that name will have the \n still present from when fgets() wrote to the buffer.

    See the FAQ for further info on fgets().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    That allows me to compile the program. But then when I type in Brandon after being asked to enter my name it says Invalid. How can I fix this?

    (sorry really have no experience coding)

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    Okay so I understand that it is because there is a new line that my code isn't working right... but I'm still really confused as to how to fix it. Can anyone tell me what to add to my code specifically to fix this issue?

    Thanks!

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It's simple.
    Code:
    if (strcmp(name, "Brandon\n") == 0) {
        printf("Do me, baby!\n");
    }
    else {
        printf("Who are you? Do me anyway!\n");
    }
    Alternatively, you could remove the newline from the input string.
    Code:
    name[strcspn(name, "\n")] = 0;   // must include <string.h> for strcspn
    if (strcmp(name, "Brandon") == 0)
    ...
    The strcspn call essentially determines the position of the newline character (or the zero-terminator if there's no newline).

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    Yay thank you I got that to work! (funny joke too) So now I want to make the code continue, so if the correct name is inputed it says the message and then asks the next question. How would I go about this...?

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Maybe something like this. I removed the newline (so it's not needed in the comparison string) and also converted the input to lowercase for the comparison since you never know whether the user will upcase the first letter or write in all caps or whatever.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main() {
        char name[100], *p;
    
        printf("Enter your name: ");
        fgets(name, 100, stdin);
    
        // Convert to lowercase and remove newline
        for (p = name; *p; p++)
            if (*p == '\n')
                *p = '\0';
            else
                *p = tolower(*p);
    
        if (strcmp(name, "brandon") != 0) {
            printf("I don't know you.\nGoodbye.\n");
            return 0; // end program
        }
    
        printf("Hey youuu\n");
    
    
        // continue code here ....
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little help - If statements
    By mrben2k9 in forum C Programming
    Replies: 1
    Last Post: 11-09-2009, 06:41 PM
  2. help with if and else statements
    By kburyanek in forum C Programming
    Replies: 7
    Last Post: 09-25-2009, 01:28 AM
  3. If statements
    By DJ_Steve in forum C Programming
    Replies: 12
    Last Post: 08-13-2009, 12:43 PM
  4. IF statements
    By Viperz0r in forum C++ Programming
    Replies: 11
    Last Post: 03-05-2006, 11:08 AM
  5. Using if statements?!
    By nectron101 in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2003, 08:47 AM

Tags for this Thread