1

I am trying to learn to program in C but am having trouble with manipulating strings as C treats strings as arrays.

My aim was to make a program that stores the users first name and surname.

Here is my progress:

#include <stdio.h>

int main(int argc, const char * argv[]) {

//defining the variables
char first_name[100];
char surname[100];
char ch[2];


// Asking for the first name and storing it
printf("What's your first name?\n");
scanf("%s", first_name);

// Prints the first name
printf("Hey %s!\n",first_name);


//Asks the user if they want to store their surname
printf("Would you like to tell me your second name? This is optional so type 'Y' for yes and 'N' for no.\n");
scanf("%s", ch);

//validate if they want to store it or not
if (ch == "Y"){
    printf("What is your surname?\n");
    scanf("%s", surname);
    printf("Your whole name is %s %s", first_name, surname);
 }

return (0);
}

However, with this code, I get an error because my IDE(xCode) tells me to use the strcmp function. I then edited the code to become this:

    if (strcmp(ch, "Y")){
        printf("What is your surname?\n");
        scanf("%s", surname);
        printf("Your whole name is %s %s", first_name, surname);
    }

However variable ch is not a literal and so is not comparable.

Sidenote

I did try to compare two literals too, just to see how it works:

char *hello = "Hello";
char *Bye = "Bye";


if (strcmp(hello, Bye)){
    printf("What is your surname?\n");
    scanf("%s", surname);
    printf("Your whole name is %s %s", first_name, surname);
}

But even this gave an error:

Implicitly declaring library function 'strcmp' with type 'int (const *char, const *char)'

I believe I am not able to do this due to my lack of experience so it would be much appreciated if you could help me understand what I'm doing wrong and how I can fix the problem.

6
  • 1
    The first thing you should do is to read the strcmp man page. That will tell you that you need #include <string.h>. It would also tell you that strcmp returns 0 when there is a string match. Commented Jul 17, 2016 at 6:59
  • Oh, yes I forgot about that! I'll read it right away :) @kaylum Commented Jul 17, 2016 at 7:00
  • 1
    "However variable ch is not a literal and so is not comparable". What makes you think that? strcmp can compare strings contained in variables just fine. Commented Jul 17, 2016 at 7:00
  • 2
    Or, make int ch and read ch as a single char. Then you can make your simple comparison if (ch == 'Y') (note: you will have read problems with the way you use scanf) Commented Jul 17, 2016 at 7:00
  • You solved it, I needed to include the string.h library. Silly mistake. Commented Jul 17, 2016 at 7:02

3 Answers 3

3

You need to include the appropriate header:

#include <string.h>

Also note that your desired logic probably calls for:

if (!strcmp(hello, Bye))

Instead of:

if (strcmp(hello, Bye))

Since strcmp returns 0 in case of equality.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, you are completely correct! I have added an answer too as I felt it was more succinct. Have a look at it and tell me how I can improve my answering skills
1

There are several issues you should correct concerning how you handle input with scanf. First always, always validate the number of successful conversions you expect by checking the return for scanf. Next, as mentioned in the comment, there is NO need to include <string.h> in your code to make a one-letter comparison. Use a character comparison instead of a string comparison. Lastly, always limit your input to the number of characters available (plus the nul-terminating character.

Putting the bits together, you could do something like the following:

#include <stdio.h>

#define MAXN 100

int main (void) {

    char first_name[MAXN] = "", surname[MAXN] = "";
    int ch;

    printf ("What's your first name?: ");
    if (scanf ("%99[^\n]%*c", first_name) != 1) {
        fprintf (stderr, "error: invalid input - first name.\n");
        return 1;
    }
    printf ("Hey %s!\n", first_name);

    printf("Enter surname name? optional (Y/N) ");
    if (scanf("%c%*c", (char *)&ch) != 1) {
        fprintf (stderr, "error: invalid input - Y/N\n");
        return 1;
    }

    if (ch != 'y' && ch != 'Y')     /* handle upper/lower case response */
        return 1;

    printf ("Enter your surname?: ");
    if (scanf (" %99[^\n]%*c", surname) != 1) {
        fprintf (stderr, "error: invalid input - surname\n");
        return 1;
    }

    printf ("\nYour whole name is : %s %s\n", first_name, surname);

    return 0;
}

Example Use/Output

$ ./bin/firstlast
What's your first name?: David
Hey David!
Enter surname name? optional (Y/N) Y
Enter your surname?: Rankin

Your whole name is : David Rankin

Look it over and let me know if you have any questions.

2 Comments

This is very interesting but advanced. I will make sure to look back onto this when I am more confident in my abilities! Thank you for your input :)
Well, OK, actually it is about as basic as it gets. You prompt for the first-name, read/validate the response. The scanf format specifier %99[^\n]%*c is the minimum needed to read a string with whitespace and limit the number of characters to 99 + nul-terminating char. The %*c simply reads/discards the '\n' that you would otherwise leave in the input buffer (stdin) which would cause problems on your next call to scanf. (that's why fgets or getline are generally preferred over scanf. Take take to understand each char here -- it is bare minimum.
1

There are two problems here. Firstly you need to see what value is returned by the strcmp and secondly you must use the approprate hedder.

You must use:

#include <string.h>

Secondly, you must edit your if-else statement so it is like this:

if (strcmp(ch, "Y") == 0){
    printf("What is your surname?\n");
    scanf("%s", surname);
    printf("Your whole name is %s %s", first_name, surname);
}

We do this because the strcmp function returns a negative value if ch is smaller than "Y", or a positive value if it is greater than "Y" and 0 if both strings are equal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.