Rosha. R
Rosha. R

Reputation: 31

printing wrong number from defined function related to struct in c

I had an assignment about entering an unknown number of the students' information and then printing them out. the code that I wrote works properly for one student but when I enter other students, only the output of the phone numbers and IDs are wrong but there isn't any error message. This is the shorter version of my code. I was hoping that someone could help me find the error. Thank you for helping in advance!!

code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 75

struct student GetData();
int print_info (struct student *STU, int i);

struct company
{
    char Co_Intern[STRSIZE];
    long int Co_Tel; ///this variable has wrong output
};

struct student
{
    long int ID; ///this variable has wrong output
    char name[STRSIZE];
    long int Tel; ///this variable has wrong output
    struct company COE;
    int Tot_days;

    struct Date
    {
      int dd;
      int mm;
      int yyyy;
    }doj;
};

int main()
{
    struct student *STU;
    int i, n;
    printf("Enter the number of students: ");
    scanf("%d", &n);
    STU = (struct student*) malloc(n * sizeof(struct student));

    for(i = 0; i < n; ++i)
    {
        STU[i]= GetData(); //calling a struct function to scan data
    }
    printf("\nDisplaying Information:\n==============================\n\n");
    for(i = 0; i < n; ++i)
    {
        print_info (STU, i);  //calling a function to print data
    }
    return 0;
}

 struct student GetData() ///struct function to scan data
{
        struct student info_s;
        printf("\nEnter student's information:\n");

        printf("Student ID:\t");
        scanf("%li", &info_s.ID);

        printf("Student's name:\t");
        getchar();
        gets(info_s.name);

        printf("Student Telephone number:\t");
        scanf("%li", &info_s.Tel); 

        printf("\nEnter student's internship company date and information:\n");
        printf("Student's Company of internship:\t");
        scanf("%s", info_s.COE.Co_Intern);

        printf("Company's Telephone:\t");
        scanf("%li", &info_s.COE.Co_Tel); 

        printf("Beginning date: dd-mm-yyyy:\t");
        scanf("%d%d%d", &info_s.doj.dd, &info_s.doj.mm, &info_s.doj.yyyy);
        printf("Total days of internship:\t");
        scanf("%d", &info_s.Tot_days);
        return(info_s);
}

int print_info (struct student *STU, int i)
{
        printf("\nStudent-%d:\t\t\t\t\t\t",i+1);
        printf("\n\nStudent ID:\t\t\t\t\t%li", STU[i].ID);
        printf("\n\nStudent's name:\t\t\t\t\t%s", STU[i].name);

        printf("\n\nStudent Telephone number:\t\t\t%li", STU[i].Tel);
        //here is the problem

        printf("\n\nStudent's Company of internship:\t\t%s", STU[i].COE.Co_Intern);

        printf("\n\nCompany's Telephone:\t\t\t\t%li", STU[i].COE.Co_Tel);
//here is the problem

        printf("\n\nBeginning date: dd-mm-yyyy:\t\t\t%d%d%d", STU[i].doj.dd, STU[i].doj.mm, STU[i].doj.yyyy);
        printf("\n\nTotal days of internship:\t\t\t%d", STU[i].Tot_days);
        printf("\n\n==============================\n\n");
}

and this is the output in which the long numbers are wrong:

Enter the number of students: 2

Enter student's information:
Student ID:     1234567890
Student's name: xxxx xxxx
Student Telephone number:       987654321

Enter student's internship company date and information:
Student's Company of internship:        aaaa
Company's Telephone:    5554443330
Beginning date: dd-mm-yyyy:     22-10-2020
Total days of internship:       365

Enter student's information:
Student ID:     1112223330
Student's name: nnnn ssss
Student Telephone number:       9998887770

Enter student's internship company date and information:
Student's Company of internship:        name
Company's Telephone:    3333344444
Beginning date: dd-mm-yyyy:     20-10-2020
Total days of internship:       365

Displaying Information:
==============================


Student-1:

Student ID:                                     1234567890

Student's name:                                 xxxx xxxx

Student Telephone number:                       987654321

Student's Company of internship:                aaaa

Company's Telephone:                            1259476034

Beginning date: dd-mm-yyyy:                     22-10-2020

Total days of internship:                       365

==============================


Student-2:

Student ID:                                     1112223330

Student's name:                                 nnnn ssss

Student Telephone number:                       1408953178

Student's Company of internship:                name

Company's Telephone:                            -961622852

Beginning date: dd-mm-yyyy:                     20-10-2020

Total days of internship:                       365

==============================

as you can see the following lines have problem, and only the IDs were correct however sometimes randomly the second Id is also wrong and has the wrong value but the dates and durations that I have in the code are correct. i also tried other ways that others asked about like scanning them individually as an int and then assigning them to their related struct but it didn't work as well.

Company's Telephone: -961622852

Student Telephone number: 1408953178

Company's Telephone: 1259476034

Upvotes: 0

Views: 65

Answers (1)

William Pursell
William Pursell

Reputation: 212248

There are probably other issues, but the input string "%d%d%d" does not match the input given for the date. As a result, the scanf treats the values as negative numbers. To avoid this error, perhaps use a different input format. To avoid this type of error in general, you must always check the value returned by scanf, every time you call scanf. For example, perhaps use dd/mm/yy with:

if( 3 != scanf("%d/%d/%d", &info_s.doj.dd, &info_s.doj.mm, &info_s.doj.yyyy) ){
    fprintf(stderr, "Invalid input\n");
    exit(EXIT_FAILURE);
}

If you want to provide a friendlier interactive interface (which is best accomplished by using a language other than C!), you should probably not use scanf to read the input. Instead, use fgets and then parse the data (possibly with sscanf, but really you're better off avoiding the entire scanf family) after it has been read. Attempting to recover from unexpected input using scanf is often more difficult than it is worth.

See: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

Upvotes: 1

Related Questions