0
votes

Having trouble with structures. How do i declare this? I need to d the following:

Your function will return an integer.

  • Your function should only accept one argument (an array).
  • Your function will ask the user for the number of characters that will be inputted. Then, inside your function, you will check if this value is greater than or equal to 70. If the values is greater than or equal to 70, you will print out an error message and return 1. Otherwise, you will use a loop to scan the characters and return 0.
#include <stdio.h>

struct info {
    char name[70];
    char lastname[70];
    char address[70];
};

void printarray(char name[]) {
    int i;
    int number;
    printf("How many characters will be inputted? \n");
    printf("It cannot be more than 70!\n");
    scanf("%d", &number);
    printf("What is your name? \n");
    for(i=0; i<number; i++) {
        scanf(" %c", &info.name[i]);
    }
    return;
}

int main() {
    struct info name;

    return 0;

}
2
Welcome to StackOverflow! Please properly format code samples inside code blocks so that others can read your code easily to better assist you. stackoverflow.com/help/formattingElan Hamburger
You never call to printarray function in mainItati
info is the name of a type like int or float. scanf needs a variable name to put the stuff into and not the type of the variable - you wouldn't write scanf(" %d", &int); for the same reason The way to fix it is to make a variable of struct info type and then use that in the scanf call. You could even call it info (but I wouldn't - imagine the confusion that would create!)Jerry Jeremiah
Why are you calling scanf (" %c",...) in a loop to collect characters in the first place? Why not call scanf (" %70s",...) instead and then the user doesn't need to say how many characters they are going to put in.Jerry Jeremiah
@JerryJeremiah info is not the name of a type. struct info is the name of a type.Keith Thompson

2 Answers

0
votes

This

struct info {
    char name[70];
    char lastname[70];
    char address[70];
};

defines a structure template, not a structure variable, so you can't use scanf(" %c", &info.name[i]); becuase struct info is a data type defined by you.

Inside main() you defined the structure variable name.

As you said in your question, that when the number is >= 70, it should print an error msg and return 1; but you aren't checking for the error.

Lastly, you never called printarray().

Probably, you want to do this:

#include <stdio.h>

struct info {
    char name[70];
    char lastname[70];
    char address[70];
};

void printarray(struct info var) {
    int i;
    int number;
    printf("How many characters will be inputted? \n");
    scanf("%d", &number);
    if (number >= 70)
    {
      printf("Number should be less than 70.\n");
      return 1;
    }
    printf("What is your name? \n");
    for(i=0; i<number; i++) {
        scanf(" %c", &var.name[i]);
    }
    return;
}

int main() {
    struct info name;
    printarray(name);
    return 0;

}
0
votes

The code has several defects:

  1. The program is supposed to receive a char array, not a struct template.

  2. No condition is set to compare the number with >= 70.

  3. The function printarray(char[]) was never used.

  4. The function printarray() returns nothing (i.e. void), expecting to return an integer 1 or 0 is impossible with it.

  5. return in the last line of the function, isn't required.

  6. The identifier info in the syntax:

    scanf(" %c", &info.name[i]); // obviously an error
    

    is a type name and not an instance of info struct.

Aside: Avoid using magical numbers, use constants when the number isn't going to be changed anywhere (e.g. MAX which holds 70 integer value in the program given).


Example code redefined is as follows:

#include <stdio.h>

#define MAX 70

// struct template
struct info {
    char name[MAX];
    // ...
};

// function declaration
int print_array(info);

int main(void) {
    info in;
    int exit_code;

    // passing a single argument
    exit_code = print_array(in);

    printf("\nExit code was: %d\n", exit_code);

    return 0;
}

// function definition
int print_array(info i) {
    int num;

    printf("Enter a number (not >= 70): ");
    scanf("%d", &num);

    if (num >= 70) {
        printf("The input exceeds the limit.\n");
        return 1;
    }

    for (int it = 0; it < num; it++)
        i.name[it] = getchar();
        
    printf("%s\n", i.name);
    // ...

    return 0;
}

The code will output:

Enter a number (not >= 70): 30
Hello world, how are you today? hope you're doing good!

Hello world, how are you toda    // rest are truncated

Exit code was: 0