简体   繁体   中英

Adding Characters of a string in C

I have a programming assignment and we are asked to create a program in which the user will be able to enter the date in this format dd/mm/year. Then we have to find a way to extract each of those numbers and put in the day, month and year fields in a structure.

I thought to read the whole input of the user as a string and then just select the values of the string that I want. Here is my code.

The structure declaration:

 struct datestructure{
    char day;
   char month;
   char year;
  };

struct prj{
   int ID;
   struct namestructure name;
   struct datestructure date;
   float price;    

 };



 struct prj project[MAX];

And here is the code that I have written.

     char dateinput[11];
 scanf("%s", dateinput);


 printf("%s", dateinput); //making sure that the input is read correctly


 project[n].date.day = dateinput[0]+dateinput[1];


 printf("%s", project[n].date.day );

However this is not working and I am running out of ideas. Could you give me some guidance on how to solve this problem.

Thanks.

If you want to save them as numbers, use int :

struct datestructure{
    int day;
    int month;
    int year;
  };

As for to convert, char to int...

project[n].date.day = (dateinput[0] - '0') * 10 + dateinput[1] - '0';

Understand how this works and do the same month and year.

Here are some suggestions:

  1. Consider changing the types of day, month & year to int.
  2. Parse the day, month & year out of your complete date string
  3. Convert each of the day, month & year into integer either using one of the system functions (atoi or strtol etc) or your own logic code.

Some basic sample for parsing the string (Just an example for you to understand. There are other better methods also):

char day[3] = {0};
char month[3] = {0};
char year[5] = {0};

memcpy(day, dateinput,2);
memcpy(month,dateinput+3, 2);
memcpy(year,dateinput+5, 4);

Note: The above code doesn't take care of erroneous input and considers that day and month having single digits will be enetered with a preceding 0. Ex Input: 07/03/2012. Note the preceding 0 before 7 and 3. First you can try like this and later on improvise by yourself to handle all error scenarios.

You can take 12/05/2012[today] as input sting. Now write
i = 0;//consider the date is in str
printf("%d", str[i]);//getting the equivalent ascii char

now you know the acii char of char 2. Subtract it from the ascii char of int 2. store in a variable and subtract it in the whole array.

You're already using scanf , which can do more than you realise:

#include <stdio.h>

struct datestructure {
    int day;
    int month;
    int year;
};

int main() {
    struct datestructure date;
    int count = scanf("%d/%d/%d", &date.day, &date.month, &date.year);
    if (count != 3) {
        printf("Invalid input\n");
    } else {
        printf("You said %d-%d-%d\n", date.year, date.month, date.day);
    }
}

There's also strftime , which takes a little more effort to use but will validate the input based on knowing about the calendar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM