61 lines
1009 B
C
61 lines
1009 B
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
void read(char *res, int maxsize);
|
||
|
|
||
|
struct Person {
|
||
|
char name[28];
|
||
|
int age;
|
||
|
};
|
||
|
|
||
|
void printPerson(struct Person *p) {
|
||
|
printf("%s is %d years old\n", p->name, p->age);
|
||
|
}
|
||
|
|
||
|
void readPerson(struct Person *p) {
|
||
|
printf("Enter your name: ");
|
||
|
read(p->name, sizeof p->name);
|
||
|
|
||
|
do {
|
||
|
printf("Enter your age: ");
|
||
|
scanf("%d", &p->age);
|
||
|
} while (p->age < 0);
|
||
|
}
|
||
|
|
||
|
|
||
|
void read(char *res, int maxsize) {
|
||
|
/* initialize each element to 0 to ensure that the result is null terminated */
|
||
|
char buffer[1024] = {0};
|
||
|
int ch;
|
||
|
int i = 0;
|
||
|
|
||
|
// break on newline, max size or end-of-file
|
||
|
while((ch=getchar()) != '\n' && i < maxsize)
|
||
|
{
|
||
|
if (ch == EOF) break;
|
||
|
buffer[i++] = ch;
|
||
|
}
|
||
|
|
||
|
memcpy(res, buffer, maxsize);
|
||
|
}
|
||
|
|
||
|
void greet() {
|
||
|
struct Person p;
|
||
|
|
||
|
char flag[32];
|
||
|
strcpy(flag, "<REMOVED>");
|
||
|
|
||
|
readPerson(&p);
|
||
|
printPerson(&p);
|
||
|
|
||
|
if (strcmp(p.name, flag) == 0) {
|
||
|
printf("Wauw, you have the same name as our flag!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
greet();
|
||
|
return 0;
|
||
|
}
|