array of struct C scanf with float - c

hello im trying to work with a struct of students but everytime i ask the input of grade the program stop running and i dont know why. im using Turbo C++ 4.0. if i use grades as int the program doesnt stop but when i use them as float the program stop running. please any help heres the code:
#include<conio.h>
#include<stdio.h>
#define num 2
struct student {
char name[50];
float cal1;
float cal2;
float prom;
} est[num];
int main () {
int i=0;
clrscr();
for(i=0;i<=num;i++) {
printf("\nName of student[%d]:",i);
scanf("%s",&est[i].name);
printf("\nGrade #1 [%d]:",i);
scanf("%f",&est[i].cal1);
printf("\nGrade #2 [%d]:",i);
scanf("%f",&est[i].cal2);
}
for(i=0;i<=num;i++) {
printf("\nStudent [%d]:",i);
printf("\nName: ");
printf("%s",est[i].name);
printf("\nGrade #1: ");
printf("%f",est[i].cal1);
printf("\nGrade #2: ");
printf("%f",est[i].cal2);
}
getch();
return 0;
}

You define:
#define num 2
struct student { … } est[num];
and you loop on:
for (i = 0; i <= num; i++)
{
…scanning code…
}
This is a buffer overflow. It attempts to read 3 students worth of data, with the third student's data going into the space after the space allocated for est. This is a buffer overflow and leads to undefined behaviour; anything can happen and it is OK.
In C, get used to using the idiomatic for loop:
for (i = 0; i < limit; i++)
In the code that reads data, you need to check that the scanf() calls succeed:
printf("\nName of student[%d]:",i);
if (scanf("%s", est[i].name) != 1) // Note no & for strings
…handle error…
printf("\nGrade #1 [%d]:",i);
if (scanf("%f", &est[i].cal1) != 1)
…handle error…
printf("\nGrade #2 [%d]:",i);
if (scanf("%f", &est[i].cal2) != 1)
…handle error…
The printing loop should not attempt to print more entries than were actually read, which might be less than num if there was an error. Obviously, it needs to be in the idiomatic form too, but you should really be using:
int j;
for (j = 0; j < i; j++)
or something similar so if only 1 entry was read, you only print that one entry.

here is an example of the code that
cleanly compiles, links, runs
I did not use conio.h as it is non-standard
(and not available on my machine)
this code still needs the error checking for returned value from scanf added
#include <stdio.h>
//#include <conio.h>
#include <stdlib.h> // system()
#define num (2)
struct student
{
char name[50];
float cal1;
float cal2;
float prom; // only one ';'
};
// separated definition, above, from declaration, below
struct student est[num];
int main ()
{
int i=0;
system( "cls" );
//clrscr();
for(i=0;i<num;i++) // note correction to for statement
{
printf("\nName of student[%d]:",i);
scanf(" %49s", est[i].name); // note correction to format string
// and using array
// as degraded into pointer
// otherwise
// unknown where trying to place data
// added vertical spacing for readability
printf("\nGrade #1 [%d]:",i);
scanf("%f", &est[i].cal1);
// added vertical spacing for readability
printf("\nGrade #2 [%d]:",i);
scanf("%f", &est[i].cal2);
}
for(i=0;i<num;i++) // note correction to 'for' statement
{
printf("\nStudent [%d]:",i);
printf("\nName: ");
printf("%s",est[i].name);
printf("\nGrade #1: ");
printf("%f",est[i].cal1);
printf("\nGrade #2: ");
printf("%f",est[i].cal2);
}
getchar(); // this will input the final newline, so will exit immediately
//getch();
return 0;
} // end function: main

Related

Having problems with 2D char arrays

So I've got an assignment where my program asks the brand (10 letters), model (10 letters), age (1986 - 2019) and cost (positive real number) of 10 cars and then wants the program to check which car is the oldest and to print out it's brand and model. I don't have a problem with the first part but with the second part.
The code is:
//First part
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define C 10
#define M 11
int main(void)
{
char brand[C][M];
char model[C][M];
int year[C];
float cost[C];
int i, len1, len2, min;
for(i=0; i<C; i++){
printf("Car %d\n", i+1);
do{
printf("Brand: ");
scanf("%s", brand[i]);
len1 = strlen(brand[i]);
} while(len1<0 || len1>10);
do{
printf("Model: ");
scanf("%s", model[i]);
len2 = strlen(model[i]);
} while(len2<0 || len2>10);
do{
printf("Year: ");
scanf("%d", &year[i]);
} while(year[i]<1986 || year[i]>2019);
do{
printf("Cost: ");
scanf("%d", &cost[i]);
} while(cost[i]<=0);
}
//Second part
year[0] = min;
for(i=0; i<10; i++)
if(year[i] < min){
min = year[i];
printf("\nThe oldest car is %s %s\n", brand[i], model[i]);
}
For some reason it either prints out gibberish in the place of brand[i] or if I lose the columns of the if statement prints out all the car brands and their models, where I only want the oldest one.
Aside from scanf not being recommended there are some problems with this code, first when you read the brand and model you do:
do{
printf("Brand: ");
scanf("%s", brand[i]);
len1 = strlen(brand[i]);
} while(len1<0 || len1>10);
The problem here is that you first write the string to brand[i] and then check if it's too long, but you have already written it into the array so if the string is longer than your space you already have a buffer overflow. Limit the size you can read with scanf using scanf("%10s, brand[i]) or better yet use fgets(brand[i], sizeof(brand[i]), stdin).
Next in the second part you use min without initializing it, and you overwrite the content of year[0] with it. You probably wanted something like:
min = 2020; // or a number that will be bigger than all your cars anyway
int older = 0;
i = 0;
for(i=0; i<C; i++){ // Use C here, you have it might as well use it instead of magic numbers
if(year[i] < min){
older = i;
min = year[i];
}
}
printf("\nThe oldest car is %s %s\n", brand[older], model[older]);
but bare in mind that this solution will print multiple cars if they are the oldest ones and have the same year

terminated by signal SIGSEGV (Address boundary error)

I am trying to query the user for first a number of inputs, then fill a dynamic list with that number of inputs. Then print them in reverse order. For some reason I get the SIGSEGV error, but I cannot find any infinite loops or address error. Help me find what is wrong.
#include <stdio.h>
#include <stdlib.h>
int main() {
int input;
printf("Enter a non-negative number: ");
scanf("%d", &input);
int *listA;
listA = (int*)malloc(sizeof(int[input]));
printf("Now, enter %d non-negative numbers: ", input);
for (int i = 0; i < input; i++) {
scanf("%d", listA[i]);
}
printf("In reverse order, your input is: ");
for (int i = input-1; i >=0; i-- ) {
printf("%d",listA[i]);
}
free(listA);
return 0;
}
Try compiling your code with all warnings enabled (-Wall with gcc). Your line:
scanf("%d", listA[i]);
should be:
scanf("%d", &listA[i]);

C: Array of Structs (Input into int array within array of structs)

Hi I have to create a database that stores students number, name and also stores an array of course marks (1-N) in C Programming Language.
Everything worked until I started coding for the array of course marks. Then every time I compiled the code it kept crashing as soon as it asked to input the course marks.
Can you please tell me where I'm going wrong in my programming for this task? I have attached it to this message.
The program worked for inputting the name, student number, however I could not get the program to input array of marks. I have asked how many course marks to be entered and then used a for loop within the "void insert(void)" function to keep inputting the course marks into the array *marks. I am referring specifically to lines 24 to 30 in my programming code.
Always at this point the program kept crashing and I could not proceed further to enter more names or print the stored student details.
I think there is a problem with this part:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n");
scanf("%d", &(list[num_students].marks[num_marks]));
}
Anyway here is the full code:
#include <stdio.h>
#include <string.h>
struct student{
int number;
char name[10];
int marks[5];
};
struct student list[10];
int num_students = 0;
int num_marks = 0;
int *p;
void insert(void)
{
int student_number;
int i;
printf("Enter number: \n");
scanf("%d", &list[num_students].number);
printf("Enter NAME: \n");
scanf("%s", &list[num_students].name);
printf("Enter NO of courses: \n");
scanf("%d", num_marks);
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
num_students++; // HOW DO WE INPUT ARRAY MARKS??? MARK1: , MARK2: , MARK3 ,
}
void printtest(void)
{
int i;
for (i=0; i < num_students; i++)
{
printf("Name: \n");
puts(list[i].name);
printf("Number: %d \n", list[i].number);
printf("Mark: %d /100 \n", list[i].marks);
printf("\n");
}
}
int main(void)
{
int code;
int opt1;
int courses, i, k, j, counter;
for (;;){
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) DISPLAY REPORT OF ALL STUDENTS: \n");
scanf(" %d", &code);
switch (code){
case 1 :
insert();
break;
case 2 :
printtest();
break;
default:
printf("Illegal code\n");
printf("\n");
}
}
}
Apart from what others pointed out, I'd like to draw your attention towards the following:
void insert(void)
{
int student_number;
int i;
printf("Enter number: \n");
scanf("%d", &list[num_students].number);
printf("Enter NAME: \n");
scanf("%s", &list[num_students].name);
printf("Enter NO of courses: \n");
scanf("%d", num_marks);
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
num_students++;
}
void printtest(void)
{
int i;
for (i=0; i < num_students; i++)
{
printf("Name: \n");
puts(list[i].name);
printf("Number: %d \n", list[i].number);
printf("Mark: %d /100 \n", list[i].marks);
printf("\n");
}
}
There are three problematic statements:
scanf("%s", &list[num_students].name); This is why beginners should use compilers with all warnings enabled.
printf("Mark: %d /100 \n", list[i].marks); What did you declare marks as in the first place?
scanf("%d", num_marks); Seems like you put & operator where not needed and ignore where needed. Read your textbook before asking a question next time.
Seems like you're having a tough time understanding the concept of arrays and pointers. Read your text book thoroughly before venturing into the world of pointers. If you don't use them correctly, even compiler can't help you.
Also, even if I don't expect your program to have a robust input mechanism, at least array bounds checking is expected. Learn good habits from the beginning. They'll save a lot of your time while debugging later.
Seems to be an error in:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
The crash is probably because num_marks as index indexes beyond the array. Change to:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[i]));
}

Crashes before last loop

This is a relatively simple program for tabulating grades.
My program crashes while trying to complete the last loop, specifically at the last midterm input. Any help here?
#include <stdio.h>
#include <string.h>
#define ARRAYSIZE 2
int main(void)
{
char studentID[ARRAYSIZE][10];
int midterm[ARRAYSIZE];
int fina[ARRAYSIZE];
int i=0;
double overall[ARRAYSIZE];
for (i=0;i<ARRAYSIZE;i++)
{
printf("\nInput Student ID:");
scanf("%s",&studentID[i][10]);
printf("\nInput midterm score:");
scanf("%d",&midterm[i]);
printf("\nInput final score:");
scanf("%d",&fina[i]);
overall[i]=midterm[i]*0.3+fina[i]*0.7;
}
printf("\nStudent ID MidTerm Final Overall\n");
for (i=0;i<ARRAYSIZE;i++)
{
printf("%s%5d%5d%5f",studentID[i][10],midterm[i],fina[i],overall[i]);
}
return 0;
}
The process returned is -1073741819 (0xC0000005). Thanks.
The error is in the final printf statement, specifically 'studentID[i][10]' here you are accessing an element at an illegal index (0 to 9 are legal in your case)
please try this
//declaration
int ARRAYSIZE=2;
char studentID[ARRAYSIZE][10];
//Then, you need to enter the string into the array
int i;
for (i=0; i<ARRAYSIZE; i++) {
scanf ("%s" , studentID[i]);
}
// in oreder to print them use
for (i=0; i<ARRAYSIZE; i++) {
printf ("%s" , studentID[i]);
}

while loop asks user input one time only

#include <stdio.h>
#include <stdlib.h>
struct the_struct
{
char FirstName[20];
char LastName[32];
int Score[20];
};
int main ()
{
int i,n;
struct the_struct *ptr[100];
printf("how many students?\n");
scanf("%d",&n);
while (i<=n);
{
i==0;
ptr[i] = malloc(sizeof(struct the_struct));
printf("Enter First Name \n");
scanf("%s",ptr[i]->FirstName);
printf("Enter Last Name \n");
scanf("%s",ptr[i]->LastName);
printf("Enter Score? \n");
scanf("%s",ptr[i]->Score);
printf("%s %s %s\n",ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score);
i++;
}
}
hey guys, so when i enter the first input, it goes only once without going on for the number the user inputs, i tried the for loop but same result.
still learning C so my apology if i misunderstood something.
Thanks in advance.
Your while loop is problematic. You could rewrite it as:
for (i = 0; i < n; ++i)
{
ptr[i] = malloc(sizeof(struct the_struct));
printf("Enter First Name \n");
scanf("%s",ptr[i]->FirstName);
printf("Enter Last Name \n");
scanf("%s",ptr[i]->LastName);
printf("Enter Score? \n");
scanf("%s",ptr[i]->Score);
printf("%s %s %s\n",ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score);
}
And since you use %s to read and print Score, you should declare it as char Score[20]; instead of int.
The problem is that i is uninitialized. Therefore, the loop while (i <= n) has undefined behavior, and can end at any time.
Add int i = 0 initializer to fix this problem.
Notes:
i == 0 expression at the beginning of the loop has no effect
Since i starts at zero, your while loop should be while (i < n), not <=.
You should check the results of scanf to see if the user entered something meaningful
You should specify the size of the array into which you read a string, e.g. scanf("%31s",ptr[i]->LastName); This prevents buffer overruns.

Resources