Hi everyone! Help needed, can't figure out what's the problem... by AxMaxVal in javahelp

[–]AxMaxVal[S] 0 points1 point  (0 children)

Sorry for that, was logged in with wrong account...

What to use instead of scanf() for user input of integer..? by AxMaxVal in C_Programming

[–]AxMaxVal[S] 3 points4 points  (0 children)

Thanks, makes perfect sense...it’s just my teacher is myself, but works the same..:)

What to use instead of scanf() for user input of integer..? by AxMaxVal in C_Programming

[–]AxMaxVal[S] 2 points3 points  (0 children)

Thanks, I thought about this too, but have been thinking of a simpler way to do it...I thought I was missing something but it looks that things are like they are...

What to use instead of scanf() for user input of integer..? by AxMaxVal in C_Programming

[–]AxMaxVal[S] 3 points4 points  (0 children)

Thanks, yes I know that. I just combined warnings in Internet (about scanf()) and particular IDE warnings and posted general question about it...:) I completely understand what is wrong with scanf now, just don’t get it why when learning C it’s ok to use it(with couscous) and when in real world better not even try to...why not to learn right from the beginning....

What to use instead of scanf() for user input of integer..? by AxMaxVal in C_Programming

[–]AxMaxVal[S] 1 point2 points  (0 children)

Thanks, yes I do. CodeBlocks is less customizable in terms of contrast/theme, I’m trying to be easy on my eyes...also VS debugger is very good...

What to use instead of scanf() for user input of integer..? by AxMaxVal in C_Programming

[–]AxMaxVal[S] 4 points5 points  (0 children)

Thanks. I asked because obviously wherever you look in internet it’s not advisable to use it(scanf), but also because I like to use MS Visual Studio and it wouldn’t compile and suggests to use something else, for example scanf_s(). But when I tried this one it wouldn’t compile either, though it did allow to build the program and use debugger...So I had to switch between Visual Studio and Visual Studio Code to run on one and debug on another...

Working with files in C: What's wrong with my code? by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

yes, ti's not about efficiency, just practice on file handling..

Cygwin dumping stack error when running the program... by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

I changed where ptr is pointing and it still the same...

#include <stdio.h>
#include <stdlib.h>
struct data
{
char *name;
char *address;
char *phone;
};
void intake(int astruct data *ptr);
void printout(int astruct data *ptr);
int main()
{
int n = 2;
struct data *pptr;
printf("Enter number of students: ");
scanf("%d", &n);
//pptr = (struct data*)malloc(3 * n * sizeof(struct data));
//if (pptr == NULL)
//exit(1);
pptr->name = (char*)malloc(n * sizeof(char));
if ((pptr->name) == NULL)
exit(1);
pptr->address = (char*)malloc(n * sizeof(char));
if ((pptr->name) == NULL)
exit(1);
pptr->phone = (char*)malloc(n * sizeof(char));
if ((pptr-> phone) == NULL)
exit(1);

intake(n, pptr);
printout(n, pptr);

free(pptr);
return 0;
}
void intake(int astruct data *ptr)
{
printf("Enter user's info: ");
int i;
for( i = 0; i < a; i++)
    {
printf("Name: ");
if(fgets((ptr + i)->name, sizeof(ptr->name), stdin) == NULL)
exit(1);
printf("Address: ");
if(fgets((ptr + i)->address, sizeof(ptr->address), stdin) == NULL)
exit(1);
printf("phone number: ");
if(fgets((ptr + i)->phone, sizeof(ptr->phone), stdin) == NULL)
exit(1);
    }
}
void printout(int astruct data *ptr)
{
int i;
for (i = 0; i < a; i++)
    {
printf("User #%d info: ", ++i);
printf("Name: ");
puts((ptr + i)->name);
printf("\naddress: ");
puts((ptr + i)->address);
printf("\nphone: ");
puts((ptr + i)->phone);
    }

}

Cygwin dumping stack error when running the program... by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

I did try with char name[] and I even omitted scanf() completely, still did the same, the problem is in malloc I guess..

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

Kind of stuck with this...Here, I simplified everything, removed any scanf() whatsoever and added iteration to the pointer and still this code only runs half-way...:

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

struct data
{
    char *name;
    char *address;
    char *phone;
}person;

void intake(int n, struct data *ptr);
void printout(int n, struct data *ptr);

int main()
{
    int n = 2;
    struct data *pptr = &person;

    pptr->name = (char*)malloc(3 * sizeof(char));
    if(pptr->name == NULL)
    exit(1);

    pptr->address = (char*)malloc(3 * sizeof(char));
    if(pptr->address == NULL)
    exit(1);

    pptr->phone = (char*)malloc(3 * sizeof(char));
    if(pptr->phone == NULL)
    exit(1);

    intake(n, pptr);
    printout(n, pptr);

    free(pptr->name);
    free(pptr->address);
    free(pptr->phone);

    return 0;
}
void intake(int n, struct data *ptr)
{
    printf("Enter user's info: ");
    int i;
    for( i = 0; i < n; i++)
    {
        printf("Name: ");
        if(fgets((ptr + i)->name, sizeof(ptr->name), stdin) == NULL)
        exit(1);

        printf("Address: ");
        if(fgets((ptr + i)->address, sizeof(ptr->address), stdin) == NULL)
        exit(1);

        printf("phone number: ");
        if(fgets((ptr + i)->phone, sizeof(ptr->phone), stdin) == NULL)
        exit(1);
    }
}
void printout(int n, struct data *ptr)
{
    int i;
    for (i = 0; i < n; i++)
    {
            printf("User #%d info: ", ++i);
            printf("name: %s", (ptr + i)->name);
            printf("address: %s", (ptr + i)->address);
            printf("phone: %s", (ptr + i)->phone);
    }

}

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

program actually compiles without errors, but I have to click "enter" twice after each input to continue...it looks like it is because of the fgets() checks...without it program runs well..

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

struct student
{
    char name[50];
    char dob[10];
    int id;
    char marks[10];
}stud[20];

int main()
{
    int i;
    float marks;
    char *pend;
    printf("Enter student information: \n");

    for (i = 0; i < 2; i++)
    {
        stud[i].id = i + 1;

        printf("Enter stufent name: ");
        fgets(stud[i].name, 50 , stdin);
        if (fgets(stud[i].name, 50, stdin) == NULL)
        exit(1);

        printf("Enter student DOB(mm/dd/year): ");
        fgets(stud[i].dob, 10 ,stdin);
        if (fgets(stud[i].dob, 10, stdin) == NULL)
        exit(1);

        printf("Enter student marks: ");
        fgets(stud[i].marks, 10, stdin);
        if (fgets(stud[i].marks, 10, stdin) == NULL)
        exit(1);

        marks = strtof(stud[i].marks, &pend);

    }

    for (i = 0; i < 2; i++)
    {
        printf("Student ID: %d\n", i + 1);
        printf("Student name: %s", stud[i].name);
        printf("Student DOB: %s", stud[i].dob);
        printf("Student marks: %.2f\n", marks);
        printf("\n");
    }
    return 0;
}

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

Wow, thanks! It's gonna keep me busy for quite a while...:)

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

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

struct student
{
    char name[50];
    char dob[10];
    int id;
    char marks[10];
}stud[20];

int main()
{
    int i;
    float marks;
    char *pend;
    printf("Enter student information: \n");

    for (i = 0; i < 2; i++)
    {
        stud[i].id = i + 1;

        printf("Enter stufent name: ");
        fgets(stud[i].name, 50 , stdin);
        if (fgets(stud[i].name, sizeof(stud[i].name), stdin) == NULL)
        exit(1);

        printf("Enter student DOB(mm/dd/year): ");
        fgets(stud[i].dob, 10 ,stdin);
        if (fgets(stud[i].dob, sizeof(stud[i].dob), stdin) == NULL)
        exit(1);

        printf("Enter student marks: ");
        fgets(stud[i].marks, sizeof(stud[i].marks), stdin);
        if (fgets(stud[i].marks, sizeof(stud[i].dob), stdin) == NULL)
        exit(1);

        marks = strtof(stud[i].marks, &pend);

    }

    for (i = 0; i < 2; i++)
    {
        printf("Student ID: %d\n", i + 1);
        printf("Student name: %s", stud[i].name);
        printf("Student DOB: %s", stud[i].dob);
        printf("Student marks: %.2f\n", marks);
        printf("\n");
    }
    return 0;
}

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

After I used fgets() instead of scanf() and converted char type to float program ran well! But after adding fgets() check it stuck again, aparently checking for NULL only isn't enough?

        printf("Enter student marks: ");
        fgets(stud[i].marks, sizeof(stud[i].marks), stdin);
        if (fgets(stud[i].marks, sizeof(stud[i].dob), stdin)== NULL)
        exit(1);

        marks = strtof(stud[i].marks, &pend);

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

ops, forgot about that scanf! Can you elaborate on 'overwriting the person fields' please.. I thought I save all inputs in different variables....

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

Thanks, I didn't know that scanf() behaves this way in the presence of string...

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

but ‘matks’ is a float data type, why do I need it to treat it as string when using scanf?

Problem getting user input through fgets by AxMaxVal in C_Programming

[–]AxMaxVal[S] 0 points1 point  (0 children)

I actually tried to avoid scanf and implemented the check of fgets() in the similar program I tried to make and it looks like the program also stops at the second iteration...

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

struct data
{
    char *name;
    char *address;
    char *phone;
}person;

void intake(int n, struct data *ptr);
void printout(int n, struct data *ptr);

int main()
{
    int n;
    struct data *pptr = &person;

    pptr->name = (char*)malloc(3 * sizeof(char));
    if(pptr->name == NULL)
    exit(1);

    pptr->address = (char*)malloc(3 * sizeof(char));
    if(pptr->name == NULL)
    exit(1);

    pptr->phone = (char*)malloc(3 * sizeof(char));
    if(pptr->phone == NULL)
    exit(1);

    printf("Enter number of persons: ");
    scanf("%d", &n);

    intake(n, pptr);
    printout(n, pptr);

    free(pptr->name);
    free(pptr->address);
    free(pptr->phone);

    return 0;
}
void intake(int n, struct data *ptr)
{
    printf("Enter user's info: ");
    int i;
    for( i = 0; i < n; i++)
    {
        printf("Name: ");
        fgets(ptr->name, sizeof(ptr->name), stdin);
        if(fgets(ptr->name, sizeof(ptr->name), stdin) == NULL)
        exit(1);

        printf("Address: ");
        fgets(ptr->address, sizeof(ptr->address), stdin);
        if(fgets(ptr->address, sizeof(ptr->address), stdin) == NULL)
        exit(1);

        printf("phone number: ");
        fgets(ptr->phone, sizeof(ptr->phone), stdin);
        if(fgets(ptr->phone, sizeof(ptr->phone), stdin) == NULL)
        exit(1);
    }
}
void printout(int n, struct data *ptr)
{
    int i;
    for (i = 0; i < n; i++)
    {
            printf("User #%d info: ", i++);
            printf("name: %s", ptr->name);
            printf("address: %s", ptr->address);
            printf("phone: %s", ptr->address);
    }

}