1

In this program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct person{
    char name[30];
    char address[30];
    int phoneNumber[30];
    char creditRating[30];
};

int main(){
    struct person p;
    printf("What is the person's name?\n");
    scanf(" %s", p.name);
    printf("What is the person's address?\n");
    scanf(" %s", p.address);
    printf("What is the person's phone number?\n");
    scanf("%d", &p.phoneNumber);
    printf("What is the person's credit rating?\n");
    scanf(" %s", p.creditRating);

    printf("The person's name is %s\n", p.name);
    printf("The person's address is %s\n", p.address);
    printf("The person's phone number is %d\n", p.phoneNumber);
    printf("The person's credit rating is %s\n", p.creditRating);
    return 0;
}

Can I have something like

For(i=0;i>=n;i++)
struct person [i];
printf("What is the person's name?\n");
scanf(" %s", [i].name);
printf("What is the person's address?\n");
scanf(" %s", [i].address);
printf("What is the person's phone number?\n");
scanf("%d", &[i].phoneNumber);
printf("What is the person's credit rating?\n");
scanf(" %s", [i].creditRating);

I want to have 100 structs with their inputs. It's a bit hard to write them one by one like:

struct person p;
.....
struct person q;
.....

//and etc...

How can I avoid that?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    `struct person p[100]; for(i=0;i<100;i++){... scanf("%29s", p[i].name);...` – BLUEPIXY Jan 03 '17 at 14:14
  • 2
    Be cautious. Using `scanf("%s", …)` means the name and address fields cannot take any blanks. You would probably do better with `fgets()` to read lines and `sscanf()` to analyse them when necessary. Beware newline characters. Also, you should check that the `scanf()` function calls succeed by testing the return value. If it isn't 1, something went wrong. Yes; you have to check every time. Well done for not wanting to write the same code out 100 times. That's important in programming. Avoid repetition. – Jonathan Leffler Jan 03 '17 at 14:21
  • 2
    Is there a problem with the spacebar - as the code needs to be formatted to make it readable – Ed Heal Jan 03 '17 at 15:35

1 Answers1

3

I want to have 100 structs with their inputs, but it is so hard to write them one by one...

Just use an array of structures of required size and loop over each element. Something like this:

struct person p[100]; //array of structures of required size

//loop over each array element
for(int i = 0; i < 100; i++) {
    printf("What is the person's name?\n");
    scanf(" %s", p[i].name);

    printf("What is the person's address?\n");
    scanf(" %s", p[i].address);

    printf("What is the person's phone number?\n");
    scanf("%d", &p[i].phoneNumber); 
    //NOTE: you are not scanning phone number correctly.
    //try using a loop or make it a string.

    printf("What is the person's credit rating?\n");
    scanf(" %s", p[i].creditRating);
}

Additionally, as others have suggested it's better to avoid using scanf(). Here's a link to why not use scanf()?. But if you still want to use scanf(), it's good if you check for its return value. The return value of scanf() is number of items read, as you are reading a single string in every scanf()(other than phoneNumber) check if scanf() returns 1

while(scanf(" %29s", string_name) != 1) {
    printf("wrong input");
}

Here, %29s is to avoid overwriting the space for terminating null character i.e, '\0' at the end of the string. The above while loop would not allow the program to proceed until a string is successfully scanned by scanf().

And as @IngoLeonhardt has mentioned in comments, it'd be easier if you use string to take in the phone number instead of an integer array which requires a loop to place elements read in successive indices.

Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37