undefined symbol st (student record system) in C - c

When I try to run the following code, it's generating an error:
undefined symbol st
It's showing me an error on option 2 when I try to display full student records.
I'm running it on turbo C++ compiler.
void main()
{
int option, i;
while (5)
{
printf("========== Student Database ==========\n");
printf("--------------------------------------\n");
printf("1. Insert Record\n");
printf("2. Display Record\n");
printf("3. Edit/Update Record\n");
printf("4. Delete a Record\n");
printf("5. Exit\n");
printf("--------------------------------------\n");
printf("Enter Your Choice: ");
scanf("%d",&option);
if(option==1)
{
struct student st[9];
{
printf("\student data");
}
clrscr();
break;
}
else if(option==2)
{
printf("\n===== Displaying Student Information =====\n");
printf("\n Roll No: \t Name \t \t \t Marks \t Mobile Number\n");
for (i = 0; i < 9; ++i)
{
printf("\n %d \t %st \t \t \t %d \t %d\n", st[i].roll, st[i].name, st[i].marks, st[i].number);
}
clrscr();
break;
}
getch();
}

The problem is that your declaration is in the wrong place.
if(option==1)
{
struct student st[9];
...
}
This declaration is only visible inside the if(option==1) clause, but you try and use it inside else if(option == 2)
I'm guessing that you should move the declaration to the start of your program
void main()
{
int option, i;
struct student st[9];
You should read about a couple of concepts that are important when you use variables, scope which is the area of your program where the variable is visiable, and extent which is the time for which you variable exists. Both were wrong in the code you wrote.
There are lots of other errors in your code, but I guess you'll find out about those as you go a long.

struct student st[9]; is confined to the scope of option equal to 1, so st is out scope in the other parts of the if block, hence the compiler diagnostic.
Declare it at the start of main, as you do for option.
Lastly, think about migrating from a Turbo compiler: The standards have moved on considerably since then, and you're only getting yourself into bad habits.

struct student st[9]; is a local variable in the if blocks, that is not available in else block and you try to use it. Move the declaration above the if to make st array available in the both blocks.

It's because of the scope of st. In your code the variable is only valid inside the if block, i.e. it's not available in the else block. Therefore you get a compile error.
Try this instead:
struct student st[9]; // Declare outside the if
if(option==1)
{
// struct student st[9]; Don't do it inside the if

Related

Getting weird output in c with structures

I'm new to C programming and just doing homework, thing is, I cant figure out why this the choice "Visualizar platos del dia" outputs weird symbols and not the things that I've input in with the cargar structure.
#include <stdio.h>
#include <string.h>
#include <strings.h>
struct platos{
char aperitivo[40];
char plato1[40];
char plato2[40];
char postre[40];
char bebida1[40];
char bebida2[40];
char bebida3[40];
};
void cargar(struct platos a);
void vis(struct platos a);
void emit(struct platos a);
int main(){
int a=0, b=0;
struct platos plato;
do{
printf("Bienvenido a restaurante 'Senior bigotes'\n\n");
printf("1:Cargar platos del dia\n");
printf("2:VIsualizar platos del dia\n");
printf("3:Emitir comanda y factura\n");
printf("4:Salir\n");
scanf("%d", &a);
switch(a){
case 1: cargar(plato);
break;
case 2: vis(plato);
break;
case 3: emit(plato);
break;
case 4:
b++;
break;
}
}while(b<1);
return 0;
}
void cargar(struct platos a){
printf("Ingrese aperitivo\n");
__fpurge(stdin);
gets(a.aperitivo);
printf("Ingrese plato 1\n");
__fpurge(stdin);
gets(a.plato1);
printf("Ingrese plato 2\n");
__fpurge(stdin);
gets(a.plato2);
printf("Ingrese postre\n");
__fpurge(stdin);
gets(a.postre);
printf("Ingrese bebida 1\n");
__fpurge(stdin);
gets(a.bebida1);
printf("Ingrese bebida 2\n");
__fpurge(stdin);
gets(a.bebida2);
printf("Ingrese bebida 2\n");
__fpurge(stdin);
gets(a.bebida3);
}
void vis(struct platos a){
printf("\nAperitivo: %s", a.aperitivo);
printf("\nPlato 1: %s", a.plato1);
printf("\nPlato 2: %s", a.plato2);
printf("\nPostre: %s", a.postre);
printf("\nBebidas: %s | %s | %s\n", a.bebida1, a.bebida2, a.bebida3);
int c = getchar();
}
void emit(struct platos a){
printf("\nAperitivo: %s $10", a.aperitivo);
printf("\nPlato 1: %s $25", a.plato1);
printf("\nPlato 2: %s $35", a.plato2);
printf("\nPostre: %s $20", a.postre);
printf("\nBebidas: %s | %s | %s $15\n", a.bebida1, a.bebida2, a.bebida3);
printf("Total: $105");
int c = getchar();
}
I'm using manjaro btw with visual code. I asked the teacher and he wants me to debug the code but Idk how to do that in visual code. Any help?
If you mean Visual Studio Code, it has a Run menu item that contains the the debugging items, such as Toggle Breakpoint and Start Debugging.
The first can be used on the statement you want to start debugging with, and the second to start running your code. Once the breakpoint is hit, there should be a small panel on the screen somewhere with controls for continue, step over, step into, and so on:
If you hover over those controls, it should also show you the equivalent keyboard commands you can use.
However, you should be aware that, for your immediate problem, structures are passed by value in C, not by reference. That means that the cargar function gets a copy of plato and populates the fields of that copy.
Then that copy is thrown away when returning from cargar, leaving the original plato with the arbitrary field it was given at creation time. So, when that is subsequently passed to vis or emit, you won't see the data you entered.
The easiest solution is probably to pass a pointer (&plato) to cargar, and use -> rather than . to access the fields within that function. In other words, something like (see notes below as to why I'm using fgets()):
switch (a) {
case 1: cargar(&plato); break;
and
void cargar(struct platos const *a_ptr){
printf("Ingrese aperitivo\n");
__fpurge(stdin);
fgets(a_ptr->aperitivo, sizeof(a_ptr->aperitivo), stdin);
// And so on for the other fields ...
That particular const before *a_ptr applies to the pointer value itself rather than the value "behind" the pointer. You need to be able to change the value since that's the whole purpose of the function.
However, I'd also do something similar for vis() and emit() so as to not be copying large structures around but I'd use (for example):
void vis(const struct platos const *plato) {
to make it clear neither the pointer nor the value behind the pointer is expected to change.
And, as others have mentioned in comments, you should use fgets() rather than gets(), there is no way to use the latter safely. If you want to know how to use it for user input, see this answer.
It should be an indication of how bad gets() is considered that it's actually been deprecated and removed from the standard.

Running C program returns -1.#QNAN0 instead of number stored in floating-point variable

Before going on, I'd like to say that this is my first time here and I don't know how things work yet so please pardon any errors on my part.
When compiled,(source code below) everything works fine except for the content of the float disp which is equal to -1.#QNAN0. Any help on this? Thanks in advance. Some parts of the code are not complete like the switch-case structure. Please temporarily that(Unless it affects the result).
The source code for the C program:
#include <stdio.h>
#include <stdlib.h>
float moneyup(float m);
int main()
{
char name[20];
char x;
int y;
float disp;
int hunger;
printf("\t\t**********************************************\n");
printf("\t\t* *\n");
printf("\t\t* How To Get Rich Quick! *\n");
printf("\t\t* *\n");
printf("\t\t**********************************************\n");
printf("\nThis is an experimental command line interface game made by NayNay AKA Nathan\n");
printf("\nPlease pardon the poor user interface.");
for(;;)
{
printf("\nPlease enter your name(one only)");
scanf("%s", &name);
printf("\nThe name you entered is %s. Is this correct? (type y/n for yes or no)\n");
fflush(stdin);
x=getchar();
if(x=='y') /*This part with the for loop is used to get the name of the*/
{ /*user and confirm the correctness of that name. If the name is*/
printf("Okay! Moving on..."); /*wrong, the user has the option to change it. Bulletproofing used*/
break; /*here*/
}
else if(x=='n')
{
printf("Alright let's try again.");
continue;
}
else
{
printf("Let's try this again.");
continue;
}
}
printf("\nOkay %s, Let's get this story started",name);
printf("\n\nOne sad dreary morning, %s got up from sleep and went to the kitchen to get breakfast.");
printf("\nUnfortunately for him his pantry only contained a bunch of cockroaches going at it and laying their eggs everywhere");
printf("\nHe then checked his pockets and pulled out his last 5-dollar bill. That was all he had left,");
printf("\nHe bought a sandwich for $2 and decides to start a business with $3 as capital");
printf("\n\nChoose how to start");
printf("\n1. Begging.");
printf("\n2. Mow lawns.");
printf("\n3. Apply for post of newspaper boy.");
fflush(stdin);
y=getchar();
switch(y)
{
case '1':
printf("You begged for 6 hours and got $5.25\n");
disp=moneyup(5.25);
printf("You now have $%f\n",disp);
}
return 0;
}
float moneyup(float m)
{
float money;
money=(float)money+m;
return(money);
}
The variable money is uninitialized in the function moneyup when used in expression
money=(float)money+m;

C program stops in the middle of running

Hi im new here also im new to programming and id like you to help me on this : to problem is that after compiling and running the program it stops in the middle of it when running and i didnt know what is causing this and sorry for the unreadable previous post :
here is my program :
char answer[15];
char place[15];
char fullname[15];
int age;
printf("What Is Your Full Name?: ");
scanf("%s",fullname);
printf("What Is Your Age?: ");
scanf("%d",age);
printf("Where Do You Live?: ");
scanf("%s",place);
if(strcmp(place,"gafsa")==0) {
printf("Aint a bad place you know");
}
else{
printf("hmmm %s cool\n",place);
}
printf("your name is %s, %d year old from %s is that right?: ",fullname,age,place);
scanf("%s",answer);
if(strcmp(answer,"yes")==0){
printf("you my friend are awesome\n");
}
else{
printf("you suck\n");
}
and this is an image to show the problem clearly:
http://i.stack.imgur.com/yFTwK.png
You need to pass the address of the variable:
scanf("%d",&age);
^
You're taking input at a memory location of value of uninitialized age. i.e. some garbage
Use:
scanf("%d",&age); // notice & , pass address of variable age

My program crashes, and I don't understand why

I am coding a record-keeping program in C using binary file handling. I am using Code::Blocks, with gcc to compile my C program on Windows 8.
When the program reaches to the following block, an error-message appears:
My code:
int dispaly(student record[], int count)
{
/*
This is what structure `student` looks like:
int id;
char name[200], phone[20], address[200], cclass[50];
char sec[20], roll[50], guardian_name[200], relation[200] ;
char p2_colg[100], slc_school[200];
float plus2_percent, slc_percent;
struct date dob;
struct date enr_date;
struct date looks like
int day, month, year;
*/
printf("Reached"); /*Program Runs Fine upto here*/
int i = 0;
for(i=0; i<count; i++)
{
printf("\nId: %d\tPhone: %s\nName: %s\nAddress: %s"
"\nClass: %s\tSection: %s\nRoll: %s\nGuardian Name: %s\tRelation:%s"
"\nPlus-Two in: %s\tPercentage:%f\nSLC School: %s\tPercentage: %f"
"\nDate Of Birth(mm/dd/yyyy): %d/%d/%d"
"\nEnrolled in (mm/dd/yyyy): %d/%d/%d\n\n---------------------------------------\n", record[i].id, record[i].name, record[i].address
, record[i].cclass, record[i].sec, record[i].roll, record[i].guardian_name, record[i].relation, record[i].p2_colg
, record[i].plus2_percent, record[i].slc_school, record[i].slc_percent, record[i].dob.month, record[i].dob.day, record[i].dob.year
, record[i].enr_date.month, record[i].enr_date.day, record[i].enr_date.year);
}
getch();
return 0;
}
The program compiles without any errors or warnings.
What's going on?
Hard to tell exactly what crashed without looking at the exact data in your array, but you forgot "phone" in the arguments list to printf, which could certainly result in a crash inside printf.
There's not a terribly good reason to stack those all up into one call. It would have been easier to spot your bug of the missing "phone" if you separated each line out into its own printf. Also, you could cut down on the redundancy if you captured record[i] into a pointer.
Contrast with:
student * r = &record[i];
printf("\n");
printf("Id: %d\tPhone: %s\n", r->id, r->phone);
printf("Name: %s\n", r->name);
printf("Address: %s\n", r->address);
printf("Class: %s\tSection: %s\n", r->cclass, r->sec);
printf("Roll: %s\n", r->roll);
printf("Guardian Name: %s\tRelation:%s\n", r->guardian_name, r->relation);
printf("Plus-Two in: %s\tPercentage:%f\n", r->p2_colg, r->plus2_percent);
printf("SLC School: %s\tPercentage: %f\n", r->slc_school, r->slc_percent);
printf("Date Of Birth(mm/dd/yyyy): %d/%d/%d\n",
r->dob.month, r->dob.day, r->dob.year);
printf("Enrolled in (mm/dd/yyyy): %d/%d/%d\n"
r->enr_date.month, r->enr_date.day, r->enr_date.year);
printf("\n");
printf("---------------------------------------\n");
In a technical sense, making multiple calls to printf will incur some function call overhead. And declaring a pointer variable for the current student in the array will incur some storage space. But it is basically negligible, and of no consequence in a case like this. Under the hood, the output is buffered anyway.

How to sort blank arrays to the top so I can delete them

int id_swap()
{
char tempstring[15];
strcpy(tempstring, id[index+1]);
strcpy(id[index+1], id[index]);
strcpy(id[index], tempstring);
}
int delete_student()
{
int found=0;
char id_to_find[10];
printf("Please enter the student ID to delete\n\n");
scanf("%s", & id_to_find);
fflush(stdin);
system("cls");
for(index=0;index<height_of_array+1;index++)
{
if(strcmpi(id_to_find, id[index]) == 0)
{
found=1;
id_swap();
system("cls");
printf("Student deleted");
height_of_array = height_of_array--;
}
Okay so that's part of my code. A quick example of what happens
I sort the students that are already in the program so it comes up like this for e.g
Student#1 Chris ID: 1831
Student#2 etc
student#3 etc
student#4 Brian ID: 4432
student#5 etc
student#6 etc
But when I try to delete say Brian for example, it deletes it but then it looks like this
student#1 ID:
Student#2 Chris ID:1831
Student#3 etc
Is there any way to move that blank array to the last position so I can then decrement my "Height_of_array" so that the number of storable students goes down by 1 to reflect the deletion
There are a few problems with your code: that jump at me:
fflush(stdin);
Never do that. Only flush output streams.
height_of_array = height_of_array--;
Is undefined behavior, you meant:
height_of_array--;

Resources