Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Something is wrong with my code. Please help :/.

+1 vote
asked Dec 27, 2022 by codenxn (1,240 points)
closed Dec 30, 2022 by Admin
Hi! Thanks for clicking. There is my code: https://onlinegdb.com/njsBIf8lQ

The code is in C. Something is wrong with it. I just started learning C a few days ago, so don't judge please.
closed with the note: answered

3 Answers

+1 vote
answered Dec 27, 2022 by Peter Minarik (66,990 points)

Your code is fixed below with some tips:

  • If you do not know how a function works, consult the manual.
  • To compare two strings, do not use the equality operator (==), as it checks for memory address equality for pointers (strings in C are pointers). Instead, use the strcmp() function. It returns 0 when they are equal, negative if the first comes first, and positive if the second comes first.
  • printf() prints formatted text. Check the manual how to print various types. %s is for strings, %d is for integers (and there are many more).
  • If you provide a formatting argument for printf(), you need to provide the value.
  • To check if a string is empty, do not compare it with an empty string (again, that's pointer/memory address comparison), instead check if the length is 0.
  • To convert a whole string to lower/upper case, iterate through all the characters in the string and convert them one by one.
  • size_t is the return type of strlen(), which is an unsigned type, hence it's advised to compare it against unsigned values (0u indicates an unsigned 0 numeric literal).
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main() {
    char name[100];
    while (strlen(name) == 0u) {
        printf("Hi! Can you enter here your name?\n");
        scanf("%s", name);
    }
    int length_of_name = strlen(name);
    if (strlen(name) <= 2) {
        printf("Your name has to be at least 3 character long. Your name is %d character long currently", length_of_name);
    } else {
        char goodslashbad[100];
        
        printf("Oh, hi, %s! It's so nice to see you! How are you today? (good/bad)\n", name);
        scanf("%s", goodslashbad);
        
        // turn answer to lower case
        size_t length = strlen(goodslashbad);
        for (size_t i = 0; i < length; i++)
            goodslashbad[i] = tolower(goodslashbad[i]);
        
        if (strcmp(goodslashbad, "good") == 0) {
            printf("Looks like someone is having a great day today! =)");
        } else if (strcmp(goodslashbad, "bad") == 0) {
            printf("something bad happened? Oh no! I hope you'll be okay!");
        } else {
            printf("Your answer isn't recognised. Please try again.");
            scanf("%s", goodslashbad);
        }
    }
}

Good luck and keep on learning! :)

commented Dec 27, 2022 by codenxn (1,240 points)
Thanks! Sorry for asking, but why is the 'size_t' useful? What does it do? and when compared the length of a string to 0 why do I need to ad a u? the while (strlen(name) == 0u) line.
commented Dec 27, 2022 by Peter Minarik (66,990 points)
Most C/C++ compilers can be configured to warn about comparing signed numbers types against unsigned numbers types.

Consider the following little code

#include <stdio.h>

int main()
{
    int invalidAge = -1;
    unsigned int maximumAge = 0xFFFFFFFF;
    if (invalidAge == maximumAge)
        printf("They are the same!");
    else
        printf("They are different!");
}

Give it a go and run it.

You'll see that maximumAge is equal to invalidAge. But why? Well, this comes down to how signed and unsigned numbers are represented. I won't go into details (feel free to search the Internet).

The point is, it's better to show the compiler that you know what you're doing by comparing the right types and not allowing the compiler to force type conversion for you if you missed that.

As mentioned, size_t is used to store unsigned types, it is the return value of the strlen() function, hence we store the value in that type (and not int).

If you just do 0, not 0u, all will be fine. Worst case, the compiler will give you a warning. Problems happen when you compare negative numbers against unsigned types, as the above example shows.

I hope this cleared things up a little and didn't make things more confusing. :)
0 votes
answered Dec 27, 2022 by Annmary-1 (140 points)
strcmp() function must be used to compere to strings

to read strings '&' sign is not required as the variable name itself is the address.

the corrected code is given below

NOTE:it works only for lower case good and bad

#include <stdio.h>
#include <string.h>
int main()
{
    char name[100];
  
    printf("Hi! Can you enter here your name?\n");
    scanf("%s",name);
    
    int length_of_name = strlen(name);
    if (strlen(name) <= 2)
    {
        printf("Your name has to be at least 3 character long. Your name is %d character long currently",length_of_name);
    }
    else
    {
        char goodslashbad[100];
        
        printf("Oh, hi, %s! It's so nice to see you! How are you today? (good/bad)\n",name);
        scanf("%s",goodslashbad);
        if (strcmp(goodslashbad,"good")==0)
        {
            printf("Looks like someone is having a great day today! =)");
        }
        else if (strcmp(goodslashbad,"bad")==0)
        {
            printf("something bad happened? Oh no! I hope you'll be okay!");
        }
        else
        {
            printf("Your answer isn't recognised. Please try again.");
            //scanf("%s",goodslashbad);
        }
    }
    return 0;
}
0 votes
answered Dec 27, 2022 by Soumya Khandelwal (180 points)
#include <stdio.h>
#include <string.h>

int main() {
    char name[100] ;
    
        printf("Hi! Can you enter here your name?\n");
        scanf("%s", name);
    
    int length_of_name = strlen(name);
    if (strlen(name) <= 2) {
        printf("Your name has to be at least 3 character long. Your name is %d character long currently", length_of_name);
    } else {
        char goodslashbad[100];
        
        printf("Oh, hi, %s! It's so nice to see you! How are you today? (good/bad)\n",name);
        scanf("%s", goodslashbad);
        
        if (!strcmp(goodslashbad,"good")) {
            printf("Looks like someone is having a great day today!");
        }
        else if (!strcmp(goodslashbad,"bad"))
        {
            printf("something bad happened? Oh no! I hope you'll be okay!");
        
        }
        else
        {
            printf("OH I dont understood,Please try again (good/bad) \n");
        
        scanf("%s",goodslashbad);
        
        }
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...