Home > Enterprise >  Comparing string to integer in C structure
Comparing string to integer in C structure

Time:04-28

Trying to make a program to define the structure for vehicle data: manufacturer (string), model (string), power in kWh (int) and price (float),program initializes data for 10 vehicles, asks the user for data on the minimum power and maximum price of vehicles requested by the user and prints data on all vehicles that meet the entered data, thanks in advance for any help

struct cars {
    char manufacturer[30];
    char model[30];
    int power;
    float price;
};

int main() {
    struct cars S1[100], S2[10] = {{ "Bmw", "x3", 125, 9800 },
                                   { "Audi", "a3", 84, 12000 },
                                   { "Golf", "mk5", 40, 7000 },
                                   { "Citroen", "c4", 60, 10200 },
                                   { "Mercedes", "c220", 70,5000 },
                                   { "Mazda", "cx", 170, 25000 },
                                   { "Fiat", "punto", 35, 3200 },
                                   { "Skoda", "octavia", 72, 6500 },
                                   { "Maserati", "m5", 220, 29000 },
                                   { "Ferrari", "488p", 300, 100000 }};
   int minpow;
   float maxprice;
   int i, j;
   printf("Min power of the vehicle: %d\n", minpow);
   printf("Max price of the vehicle: %f", maxprice);
   for (i = 0; i < 9; i  ) {
       if (strcmp(S2[i].power > minpow) && (strcmp(S2[i].price < maxprice) > 0) {
         printf("your vehicles:%s %s", S2[i].manufacturer, S2.model);
       }
   }

CodePudding user response:

There are some things in your code that don't seem right.

  1. To get data from the user, you should use scanf function instead of printf like this:
printf("Min power of the vehicle: ");
scanf("%d",&minpow);
printf("\nMax price of the vehicle: ");
scanf("%f",&maxprice);
printf("\n");

Note the & before the variables like in &minpow to pass the variable's address to scanf function. This way, your program will be saving the data in the correct format (int and float)

2.Then, in the for loop, you don't need to use strcmp since you are not longer comparing strings. Your loop code should look something like this:

 for(i=0;i<9;i  )
 {
     if(S2[i].power>minpow && S2[i].price<maxprice)
     {
         printf("your vehicles:%s %s\n",S2[i].manufacturer,S2.model);
     }
 }

Iago.

CodePudding user response:

What you need is the atoi function. It is generally not useful when trying to parse errors because when it fails it returns 0 or -1 (which could very well be the value contained in the string), but in your case none of these outputs count as valid values for minpow or maxprice. My suggestion would be:

#include <stdlib.h> // atoi
#include <stdio.h> // scanf, printf

int main() {
   char minpow_str[10];
   char maxprice_str[10];

   int minpow = 0;
   int maxprice = 0;

   for(;;) {
     printf("minimum power desired : ");
     scanf("%9s", minpow_str);
     printf("max price desired: ");
     scanf("%9s", maxprice_str);

     minpow = atoi(minpow_str);
     maxprice = atoi(maxprice_str);
 
     if (minpow > 0 && maxprice > 0)
       break;

     printf("invalid input values\n");
   }
   // rest of your code
}

To make it clear, 10 is the length I chose but you can put any length you'd like the user's input to be. The arguments for the scanf function limit reading input to that length.
Edit: As comments mention, %9s makes it so scanf can null terminate the string read in case theres is 9 or more chars to be read.

  •  Tags:  
  • c
  • Related