clrscr error in main program debug - c

I am receiving the error in function main for my clrscr(); but I thought I had to clear when using fflush(stdin);?
I feel like I am missing something simple here but if anyone can shed some like I would appreciate it!
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
char name[10];
int age;
};
typedef struct person NAME;
NAME stud[10], temp[10];
void main()
{
int no,i;
void sort(int N); /* Function declaration */
clrscr();
fflush(stdin);
printf("Enter the number of students in the list\n");
scanf("%d",&no);
for(i = 0; i < no; i++)
{
printf("\nEnter the name of person %d : ", i+1);
fflush(stdin);
gets(stud[i].name);
printf("Enter the age of %d : ", i+1);
scanf("%d",&stud[i].age);
temp[i] = stud[i];
}
printf("\n*****************************\n");
printf (" Names before sorting \n");
/* Print the list of names before sorting */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",temp[i].name,temp[i].age);
}
sort(no); /* Function call */
printf("\n*****************************\n");
printf (" Names after sorting \n");
printf("\n*****************************\n");
/* Display the sorted names */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",stud[i].name,stud[i].age);
}
printf("\n*****************************\n");
} /* End of main() */
/* Function to sort the given names */
void sort(int N)
{
int i,j;
NAME temp;
for(i = 0; i < N-1;i++)
{
for(j = i+1; j < N; j++)
{
if(strcmp(stud[i].name,stud[j].name) > 0 )
{
temp = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
} /* end of sort() */

Put the function prototype void sort(int N); outside main()
You don't have (but you may) execute clrscr() before fflush(stdin). In this case contents of your screen (which you want to clear) have nothing to do with stdin.
You can read more about fflush() and the motivation to use it, here.

I assume that you get a compilation error. It is caused by the line above the one where you see the error.
As suggested by #Catalyst, it is caused here by the line
void sort(int N); /* Function declaration */`
because C does not allow functions to be declared locally inside other functions (and main is a function).
You can simply fix it that way :
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
char name[10];
int age;
};
typedef struct person NAME;
NAME stud[10], temp[10];
void sort(int N); /* Function declaration */
int main() // void main is incorrect
{
int no,i;
clrscr();
fflush(stdin);
...
Note also the int main() instead of void main(). It is harmless on Windows, but is still incorrect.

Related

how to do a delete and modify function correctly

This code has several issues that are beyond my understanding.
First, no matter how hard I try, the variable ok doesn't get saved in the file, thus ruining the display. It is strange because other variables work, it is just that one that doesn't seem to be working, even if I change the order.
Also, the delete and modify functions aren't working, as well.
I tried everything, but nothing seems to be working.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
// Structure of the employee
struct emp {
char name[50];
char ok[50];
int hr;
int age;
int id;
};
FILE *f;
emp* add(emp *e,int n){
int i;
char p;
emp *t;
int k;
t=(emp*)malloc(n*sizeof(int));
f=fopen("Data.txt","w");
for(i=0;i<n;i++){
printf("\nEnter Name : ");
scanf("%s",(t+i)->name);
printf("\nEnter Departement: ");
scanf("%s",&(t+i)->ok);
printf("\nEnter Age : ");
scanf("%d",&(t+i)->age);
printf("\nEnter avearge hour of works(per week) : ");
scanf("%d",&(t+i)->hr);
// k = ( ( (t+i)->hr ) *40 );//40 is minimum wage for each hour of work
// printf("Salary of The employe: %d\n",k);
printf("\nEnter EMP-ID : ");
scanf("%d",&(t+i)->id);
fprintf(f,"%s\n%s\n%d\n%d\n%d\n",(t+i)->name,(t+i)->ok,(t+i)->age,(t+i)->hr,(t+i)->id);
}
fclose(f);
return t;
}
void aff(emp *t,int n){
int I;
f=fopen("Data.txt","r");
for(i=0;i<n;i++){
if (f != NULL){
fscanf(f,"%s",&(t+i)->name);
fscanf(f,"%s",&(t+i)->ok);
fscanf(f,"%d",&(t+i)->age);
fscanf(f,"%d",&(t+i)->hr);
fscanf(f,"%d",&(t+i)->id);
printf("Name : %s\n",(t+i)->name);
printf("departement : %s\n",(t+i)->ok);
printf("Age : %d\n",(t+i)->age);
printf("Hours : %d\n",(t+i)->hr);
printf("ID : %d\n",(t+i)->id);
}
}
fclose(f);
}
//emp* modf(emp *t,int n){
// int i;
// int k;
// char nv[50];
// printf("id of the entry you want to be modified" );
// scanf("%d\n",&k);
// for(i=0;i<n;i++){
// if(k==(t+i)->id){
// scanf("%s",&nv);
// (t+i)->name=nv;
// }
// }
// return t;
//}
//void del(emp *t,int n){
// int i;
// emp a;
// int k;
// printf("position of the entry you want to delete?");
// scanf("%d",&a);
// for(i=0;i<n;i++){
// if (a == *(t+i)) {
// for(i=k;i<n-1;i++){
// *(t+i)=*(t+i+1);
// }
// }
// }
//}
int main(int argc, char *argv[]){
int c;
emp e;
emp *k;
k=&e;
int n;
emp *t;
t=&e;
char p;
ka:
printf("Welcome To The employe management Menu\n");
printf("1.Add employes?\n");
printf("2.show all employes?\n");
printf("3.delete an entry?\n");
scanf("%d",&c);
switch (c){
case 1:
ed:
printf("How many employes you want to add?\n");
scanf("%d",&n);
add(k,n);
if(p=='y'){
goto ed;
}
else goto ka;
break;
case 2:
aff(t,n);
break;
case 3:
del(t,n);
break;
}
return 0;
}
At least these issues:
Wrong allocation size #Weather Vane
Avoid allocation errors. Allocate to the size of the refenced object, not the type.
// t=(emp*)malloc(n*sizeof(int));
t = malloc(sizeof t[0] * n);
Easier to code right, review and maintain.
Uninitialized p #Craig Estey
f(p=='y') is undefined behavior as p is uninitialized.
This hints that OP is not compiling with all warnings enabled. Save time - enable all warnings.
emp not defined
Perhaps OP is not using a C compiler, but a C++ one?
i not defined in for (i = 0; i < n; i++) {
Is int I; the true code?
Bad "%s"
Without a width, "%s" is worse than gets(). Use a width like "%49s".

Why am I getting garbage value after displaying the data

I am getting garbage value when I display the records.
I have to create a database of students in C using array of structures and without pointers.
Is there any other way of doing this?
How to use array of structures?
#include <stdio.h>
struct student {
char first_name[10],last_name[10];
int roll;
char address[20];
float marks;
};
void accept(struct student);
void display(struct student);
void main() {
struct student S[10];
int n, i;
printf("Enter the number of records to enter : ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
accept(S[i]);
}
for (i = 0; i < n; i++) {
display(S[i]);
}
}
void accept(struct student S) {
scanf("%s", S.first_name);
scanf("%s", S.last_name);
scanf("%d", &S.roll);
scanf("%s", S.address);
scanf("%f", &S.marks);
}
void display(struct student S) {
printf("\n%s", S.first_name);
printf("\n%s", S.last_name);
printf("\n%d", S.roll);
printf("\n%s", S.address);
}
Everything in C is pass-by-value. Which means you are modifying variable copy in stack frame, while real variable passed as parameter remains untouched.
You have to pass an pointer to variable which you want to modify in function.
// Function declaration
void accept(struct student *);
// Call
accept(&S[i]);
// Usage in function via dereference operator
scanf("%s",S->first_name);
If you would like to enter unknown amount of records, you should use VLA (since c99) or dynamically allocate structures.
VLA
scanf("%d",&n);
struct student S[n];
Dynamic callocation
scanf("%d",&n);
struct student * S = malloc(sizeof(struct student) * n);
Because in your case, if user input more that 9 records you are touching outside of bounds, which has undefined behavior.
There are multiple issues in your code:
The standard prototype for main without arguments is int main(void)
You should allocate the array dynamically with calloc.
you should pass structure pointers to the accept and display functions instead of passing structures by value. Passing the destination structure by value is incorrect as the accept function cannot modify the structure in the main function, which remains uninitialized and causes garbage to be displayed. Note that it is actually undefined behavior to access uninitialized data so the program could behave in even worse ways.
You should provide scanf() with the maximum number of arguments to store into character arrays to avoid potential buffer overflows.
you should verify the return values of scanf() to avoid undefined behavior on invalid input.
you could use the %[^\n] scan set to allow embedded spaces in the address field.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
struct student {
char first_name[10], last_name[10];
int roll;
char address[20];
float marks;
};
void accept(struct student *sp);
void display(const struct student *sp);
int main(void) {
struct student *S;
int n, i, j;
printf("Enter the number of records to enter : ");
if (scanf("%d", &n) != 1)
return 1;
S = calloc(sizeof(*S), n);
if (S == NULL) {
return 1;
}
for (i = 0; i < n; i++) {
accept(&S[i]);
}
for (i = 0; i < n; i++) {
display(&S[i]);
}
free(S);
return 0;
}
void accept(struct student *sp) {
if (scanf("%9s%9s&d %19[^\n]%f",
sp->first_name, sp->last_name, &sp->roll,
sp->address, &sp->marks) != 5) {
printf("missing input\n");
exit(1);
}
}
void display(const struct student *sp) {
printf("%s\n", sp->first_name);
printf("%s\n", sp->last_name);
printf("%d\n", sp->roll);
printf("%s\n", sp->address);
printf("%f\n", sp->marks);
printf("\n");
}

C programming, solving C2073 error in structures?

So, I'm new in this, and trying structures out... Error C2073 appeares..can someone help and give some advice?
I tried with FOR in main function to call functions "ispis" which is for printf only, and function "unos" which is for scanf so many times, how big is int "broj_knjiga". I tried to work with -> instead of . but I simply can't solve this problem(which is simple). Someone help?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ispis(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\ID knjige: %d", &pt[i].ID_knjige);
printf("\Autor knjige: %s", &pt[i].autor);
printf("\Naslov knjige: %d", &pt[i].naslov);
}
}
void unos(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\nUnesite ID knjige: ");
scanf("%d", &pt[i].ID_knjige);
printf("\nUnesite autora knjige: ");
scanf("%d", &pt[i].autor);
printf("\nUnesite naslov knjige: ");
scanf("%d", &pt[i].naslov);
}
}
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
int main()
{
struct knjiga *pt;
int broj_knjiga;
printf("Unesite koliko knjiga unosite: ");
scanf("%d", &broj_knjiga);
pt=(struct knjiga*)malloc(sizeof(struct knjiga)*broj_knjiga);
unos(pt, broj_knjiga);
ispis(pt, broj_knjiga);
return 0;
}
C is compiled from the top-down. Structs only exist below their declarations. Moving your struct definition above the rest of your code will fix your problem.
knjiga myStruct1; // invalid
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
knjiga myStruct2; // valid

Exporting text to .txt file in C

My program doesn't export anything to the .txt file; actually, I can't even compile it in this form.
#include <stdio.h>
typedef struct /*We define structure type to save memory
( im not sure about this!!!) */
{
char name[20];
int num;
} cont;
void input(cont a[],int n) /*With this function i enter data in main program.*/
{
int i;
for(i=0;i<n;i++)
{
printf("Insert name:");
scanf("%s",&a[i].name);
printf("Insert number:");
scanf("%d",&a[i].num);
printf("\n\n\n");
}
}
void export(cont a[],int n) /*Export data that is entered in main program to text file
output.txt that is in same folder as program*/
{
FILE *text;
int i;
text=fopen("output.txt","w");
fprintf(text,"Name:%s\nNumber:%d\n",a[i].name,a[i].num);
fclose(text);
}
void printinprog(cont a[],int n) /*This just prints data in program so we can check
that program works correctly.*/
{
int i;
for(i=0;i<n;i++)
{
printf("Name:%s Number:%d",a[i].name,a[i].num);
printf("\n\n");
}
}
main()
{
cont per[20];
int c;
printf("Enter number of contacts:");
scanf("%d",&c);
input(per,c);
export(per,c);
system("pause");
}
So when i move main part of export function to printinprog function it looks like this:
#include <stdio.h>
typedef struct /*We define structure type to save memory
( im not sure about this!!!) */
{
char name[20];
int num;
} cont;
void input(cont a[],int n) /*With this function i enter data in main program.*/
{
int i;
for(i=0;i<n;i++)
{
printf("Insert name:");
scanf("%s",&a[i].name);
printf("Insert number:");
scanf("%d",&a[i].num);
printf("\n\n\n");
}
}
void printinprog(cont a[],int n) /*This just prints data in program so we can check
that program works correctly.*/
{
int i;
for(i=0;i<n;i++)
{
printf("Name:%s Number:%d",a[i].name,a[i].num);
printf("\n\n");
}
FILE *text;
text=fopen("output.txt","w");
fprintf(text,"Name:%s\nNumber:%d\n",a[i].name,a[i].num);
fclose(text);
}
main()
{
cont per[20];
int c;
printf("Enter number of contacts:");
scanf("%d",&c);
input(per,c);
printinprog(per,c);
system("pause");
}
Now program works but i get wrong data in .txt file and that looks like this.
So i am pretty sure that problem is in typedef struct actually i think there is a problem with data types cuz i define it like cont and that doesnt actually exist so proggram doesnt see that as a text data and the gives wrong data to .txt file.
Problem is that
FILE *text;
text=fopen("output.txt","w");
fprintf(text,"Name:%s\nNumber:%d\n",a[i].name,a[i].num);
fclose(text);
should be inside the loop, and it's outside it. What's happening is that it uses the last value for i (n) and is essentially printing from random memory locations....

Am I using structs in the wrong way?

I have come across this wierd and mysterous (at least to me) error that I am finding a very hard time finding. It gives me an error at the line where I call my function input(student_list1[MAX], &total_entries); where the compiler says:
incompatible type for agument 1 in 'input'
What am I doing wrong here? I sense it something very simple and stupid but I have gone through the code several times now without any avail.
#define MAX 10
#define NAME_LEN 15
struct person {
char name[NAME_LEN+1];
int age;
};
void input(struct person student_list1[MAX], int *total_entries);
int main(void)
{
struct person student_list1[MAX];
int total_entries=0, i;
input(student_list1[MAX], &total_entries);
for(i=0; i<total_entries; i++)
{
printf("Student 1:\tNamn: %s.\tAge: %s.\n", student_list1[i].name, student_list1[i].age);
}
return 0;
} //main end
void input(struct person student_list1[MAX], int *total_entries)
{
int done=0;
while(done!=1)
{
int i=0;
printf("Name of student: ");
fgets(student_list1[i].name, strlen(student_list1[i].name), stdin);
student_list1[i].name[strlen(student_list1[i].name)-1]=0;
if(student_list1[i].name==0) {
done=1;
}
else {
printf("Age of student: ");
scanf("%d", student_list1[i].age);
*total_entries++;
i++;
}
}
}
struct person student_list1[MAX] in the function argument is actually a pointer to struct person student_list1.
student_list1[MAX] you passed is a (out of bound) member of the array struct person student_list1[MAX]. Valid array index shoudl be between 0 to MAX - 1.
Change it to:
input(student_list1, &total_entries);
Note that here the array name student_list1 is automatically converted to a pointer to student_list1[0].
There are many things wrong with the code; this is my attempt at making it somewhat more robust:
#include <stdio.h>
#include <string.h>
#define MAX 10
#define NAME_LEN 15
// use a typedef to simplify code
typedef struct person {
char name[NAME_LEN];
int age;
} person_t;
// size qualifier on student_list is redundent and person_t* does the same
void input(person_t *student_list, int *total_entries);
int main(void)
{
person_t student_list[MAX];
int total_entries, i;
// pass array and not the non-existent 'student_list[MAX]' element
input(student_list, &total_entries);
for(i=0; i<total_entries; i++)
{
// age is an int, not a string so use %d
printf("Student 1:\tName: %s.\tAge: %d.\n", student_list[i].name, student_list[i].age);
}
return 0;
} //main end
void input(person_t *student_list, int *total_entries)
{
int done = 0, i = 0;
*total_entries = 0;
while (i < MAX) {
printf("Name of student: ");
// use NAME_LEN instead of strlen(list[i].name) because latter is
// probably not initialized at this stage
if (fgets(student_list[i].name, NAME_LEN, stdin) == NULL) {
return;
}
// detect zero-length string
if (student_list[i].name[0] == '\n') {
return;
}
printf("Age of student: ");
scanf("%d", &student_list[i].age);
// read the newline
fgetc(stdin);
*total_entries = ++i;
}
}
input(student_list1[MAX], &total_entries); shoud be input(student_list1, &total_entries);.
In C,
void input(struct person student_list1[MAX], int *total_entries);
equals
void input(struct person *student_list1, int *total_entries);

Resources