Having issues with strcmp() in C

Corey Vessar

I've been working on this for a few hours now and it's stating to test my sanity.

The assignment is to write a program that when it reads a name it checks through the given struct's names and looks for a match, then displays the other data from the structure. It is supposed to work despite the input string's case and if nothing matches the name it should tell the user.

Of course everything compiles perfectly fine, but when I input a name that should hit (because it is in the struct) it doesn't see it.

I know the issue is somewhere in the strcmp calling.

Any help would be greatly appreciated.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>



typedef struct solar    
{
    char name[50];
    char type[50];
    double radius;
    double mass;
    double distsun;

}solar_obj;

void main()
{
    char input_name[50];

    solar_obj solarobj[] = { { "CERES", "DWARF PLANET", 471, 9.5E20, 413.7E6    },
                            { "EARTH", "PLANET", 6380, 5.974E24, 149.6E6 },
                            { "ERIS", "DWARF PLANET", 1163, 1.7E22, 10210E6 },   
                            { "HAUMEA", "DWARF PLANET", 650, 4E21, 6484E6 },
                            { "JUPITER", "PLANET", 71500, 1.899E27, 778.4E6 },    
                            { "MAKEMAKE", "DWARF PLANET", 715, 2.2E21, 6850E6 },
                            { "MARS", "PLANET", 3400, 6.419E23, 227.9E6 },
                            { "MERCURY", "PLANET", 2440, 3.302E23, 57.9E6 },
                            { "NEPTUNE", "PLANET", 24770, 1.024E26, 4498.3E6 },    
                            { "PLUTO", "DWARF PLANET", 1184, 1.3E22, 5906.4E6},
                            { "SATURN", "PLANET", 60270, 5.685E26, 1426.7E6 },
                            { "SUN", "STAR", 696000, 1.9891E30, 0 },
                            { "URANUS", "PLANET", 25560, 8.685E25, 2870.9E6 },  
                            { "VENUS", "PLANET", 6050, 4.869E24, 108.2E6 }
                          };

    printf("Please enter the name of the solar object:");

    fgets(input_name, sizeof(input_name), stdin);

    //to make all inputed string chars into uppercase
    for (int i = 0; input_name[i] != '\0'; i++){
        input_name[i] = toupper(input_name[i]);
    }

    //search through struct for matching name then print the data
    for (int j = 0; j < 14; j++)
        {
            if (strcmp(solarobj[j].name, input_name) == 0)
                {
                printf("\nObject name is :  %s \n", solarobj[j].name);
                printf("%s is a %s \n", solarobj[j].name, solarobj[j].type);
                printf("Radius (km) : %f \n ", solarobj[j].radius);
                printf("Mass (kg) : %f\n", solarobj[j].mass);
                printf("Distance from sun (km) : %f \n", solarobj[j].distsun);
                }

            else // if a name that doesnt match anything in the struct tell the user
            {
               if (j == 13){
                  printf("No objects in our solar system have the name: %s\n", input_name);
                  break;
               }    

               continue;
            }     
        }    


    getch();
}
Weather Vane

First, fgets() keeps the newline at the end of the entry. You can get rid of it like this.

char *strp = strchr(input_name, '\n');  // look for newline
if (strp) *strp = '\0';                 // terminate string

Next, you can use stricmp() for case insensitive compare.

if (stricmp(solarobj[j].name, input_name) == 0)

And your loop could be simplified to be like this

int j;
for (j = 0; j < 14; j++)
{
    if (stricmp(solarobj[j].name, input_name) == 0)
        {
        printf("\nObject name is :  %s \n", solarobj[j].name);
        printf("%s is a %s \n", solarobj[j].name, solarobj[j].type);
        printf("Radius (km) : %f \n ", solarobj[j].radius);
        printf("Mass (kg) : %f\n", solarobj[j].mass);
        printf("Distance from sun (km) : %f \n", solarobj[j].distsun);
        break;
    }     
}    
if (j == 14)
    printf("No objects in our solar system have the name: %s\n", input_name);

Notice I declared j outside the loop so its scope is still valid when I test if a match was found.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related