I'm new to C programming and I am trying to create a function that adds values to a structure but it does not return anything.
This function is meant to allow the user to add records.
#include <stdio.h>
#include <string.h>
struct Produit {
int Num;
char Nom[50];
char Description[100];
float Prix;
} s[10];
void add(struct Produit s[],int n);
void display(struct Produit s[],int p,int n)
int main {
add(s,1);
display(s,1);
++n;
}
void add(struct Produit s[], int n){
again:
printf("\nEntrez le nom du produit à ajouter:");
scanf("%s",s[n].Nom);
if(searchP(s,s[n].Nom,n)!=-1){
printf("Déjà existant\n");goto again;
}
printf("Entrez la description :");
scanf("%s",&s[n].Description);
printf("Entrez le prix :");
scanf("%f",&s[n].Prix);
}
void display(struct Produit s[],int p,int n) {
printf("Nom du produit: ");
puts(s[p-1].Nom);
printf("Description: ");
puts(s[p-1].Description);
printf("Prix: %.1f", s[p-1].Prix);
printf("\n");
}
When I run this it works fine but when I verify if the record that I've entered is is there I don't find anything. I try to display the record but it's empty.
it returns this :
Entrez le nom du produit α ajouter:pen
Entrez la description :red
Entrez le prix :1.99
Nom du produit:
Description:
Prix: 0.0
can anyone tell what is wrong. thanks
PS : The function SearchP is working fine in other parts of the code so I don't think it is the problem, but nonetheless here is it.
int searchP(struct Produit s[],char Name[], int n) {
int found =-1,i;
for (i = 0; i < n-1 && found==-1; i++)
{
if (strcmp(s[i].Nom,Name)==0) {
found=i;
}
else
found=-1;
}
return found;
}
Here is a modified version of your code that works better but still needs some work.
As already commented you need a global counter for the s array (here: nbp).
There also one-off errors when searching into s.
#include <stdio.h>
#include <string.h>
struct Produit {
int Num;
char Nom[50];
char Description[100];
float Prix;
} s[10];
int nbp = 0;
void display(struct Produit s[],int p) {
printf("Nom du produit: ");
puts(s[p].Nom);
printf("Description: ");
puts(s[p].Description);
printf("Prix: %.1f", s[p].Prix);
printf("\n");
}
void add(struct Produit s[]);
int searchP(struct Produit s[],char Name[]) {
int found =-1,i;
for (i = 0; i < nbp && found==-1; i++)
{
if (strcmp(s[i].Nom,Name)==0) {
found=i;
}
else
found=-1;
}
return found;
}
int main () {
add(s);
display(s, 0);
return 0;
}
void add(struct Produit s[]){
again:
printf("\nEntrez le nom du produit à ajouter:");
scanf("%s",s[nbp].Nom);
if(searchP(s,s[nbp].Nom)!=-1){
printf("Déjà existant\n");goto again;
}
printf("Entrez la description :");
scanf("%s",s[nbp].Description);
printf("Entrez le prix :");
scanf("%f",&s[nbp].Prix);
nbp++;
}
Execution:
./myprog
Entrez le nom du produit à ajouter:Nom
Entrez la description :Desc
Entrez le prix :1
Nom du produit: Nom
Description: Desc
Prix: 1.0
Related
good morning, I am making a calculator with a "Stack" of 10 positions in the "C" lenguage. What I have to do is, for example, I write the number "50", I press ENTER and that "50" must go to the first position "01". Then I write another number, for example "20" + ENTER and 20 should go to position "01", "50" goes up to "02" and so on, always after pressing ENTER. I am using the Gotoxy for this.
I can manage to print the first number in position "01" but after that I don't know how to proceed.
I'm new to programming so excuse me if I don't realize obvious things, I'm trying to learn so if you can give me a hand I would appreciate it. Here I leave the code in case you want to try it and get an idea of what I'm trying to do, thanks
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void gotoxy(int x, int y);
void PilaPush();
void ImprimirPila();
int main() {
printf(" ____________________________________________________\n");
printf("|10:_________________________________________________|\r\n");
printf("|09:_________________________________________________|\r\n");
printf("|08:_________________________________________________|\r\n");
printf("|07:_________________________________________________|\r\n");
printf("|06:_________________________________________________|\r\n");
printf("|05:_________________________________________________|\r\n");
printf("|04:_________________________________________________|\r\n");
printf("|03:_________________________________________________|\r\n");
printf("|02:_________________________________________________|\r\n");
printf("|01:_________________________________________________|\r\n");
printf("| |\r\n");
printf("| |\r\n");
printf("|____________________________________________________|\r\n");
printf("|_____1_____|_____2_____|_____3_____|__+__|__DROP[P]_|\r\n");
printf("|_____4_____|_____5_____|_____6_____|__-__|__SWAP[S]_|\r\n");
printf("|_____7_____|_____8_____|_____9_____|__*__|__DEL[D]__|\r\n");
printf("|_____,_____|_____0_____|____EXE____|__/__|__NUM[N]__|\r\n");
printf("|_____'_____|_ARRIBA[W]_|__ABAJO[Z]_|_____|_SWAP1[Q]_|\r\n");
gotoxy(3, 12);
PilaPush();
ImprimirPila();
getch();
return 0;
}
void gotoxy(int x, int y) {
HANDLE Ventana;
Ventana = GetStdHandle(STD_OUTPUT_HANDLE);
COORD Coordenadas;
Coordenadas.X = x;
Coordenadas.Y = y;
SetConsoleCursorPosition(Ventana, Coordenadas);
}
typedef struct Nodo {
int elemento;
struct Nodo *siguiente;
} Nodo;
// funciones para utilizar un manejador global
Nodo *primero = NULL;
// Insertar un nuevo nodo a la pila
void PilaPush() {
Nodo *nuevo;
// crear el nuevo Nodo.
nuevo = (Nodo *)malloc(sizeof(Nodo)); // Asignar de forma dinamica un espacio de memoria al Nodo
printf(" ");
scanf("%d", &nuevo->elemento);
// Agregar el nodo al principio de la Pila
nuevo->siguiente = primero; // El nuevo dato ahora es primero, que apunta a (NULL)
primero = nuevo; // Primero (NULL) ahora es el nuevo dato
}
// sacar un elemento de una pila
void ImprimirPila() {
Nodo *actual = (Nodo *)malloc(sizeof(Nodo));
actual = primero;
if (primero != NULL) {
while (actual != NULL) {
gotoxy(9, 10);
printf("%d", actual->elemento);
actual = actual->siguiente;
gotoxy(3, 12);
printf(" ");
gotoxy(3, 12);
}
} else {
printf("No hay ningun dato ");
}
}
First of all, I want to explain my vocabulary of functions:
Universo: Universe
Edad: Age
Nombre: Name
Alias: Nickname
Nacionalidad: Country
Persona: Person
Insertar persona: Add Person to the Struct array
Mostrar persona: Print all the struct array elements
So I want to create a struct array and an options table in where I can choose to add persons or print them. The program max person capacity is 1000 and it checks if the person is already in the array or not, adding ir if so.
So I don't understand in first place why it doesnt print all the persons and if the persons actually are stored. Can you help me? It is a class work and I can't complicate it more that, so pointers aren't allowed. This is my code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_CAD 100
#include <string.h>
#include <ctype.h>
struct persona{
char nombre[MAX_CAD];
int edad;
char nacionalidad[MAX_CAD];
char alias[MAX_CAD];
};
int insertarPersona(struct persona universo[], int capacidad_max, struct persona nueva);
void mostrarMundo(struct persona universo[], int capacidad_max);
int main()
{
int seleccion, capacidadmax=1000, i=0;
struct persona universo[1000];
struct persona nueva;
strcpy(universo[i].nombre, "-");
strcpy(universo[i].nacionalidad, "-");
strcpy(universo[i].alias, "-");
universo[i].edad = -1;
do{
printf("1-Crear persona \^\// \n");
printf("2-Mostrar universo |. .| \n");
printf("3-Thanos-Chascar _ \ - / _ \n");
printf("4-Restaurar universo \_| |_/ \n");
printf("5-Salir \ \ \n");
printf(" __/_/__\n");
printf(" | |\n");
printf(" \ /\n");
printf(" \___/\n");
scanf("%d", &seleccion);
if(seleccion==1&&i<1000){
printf("Introduzca el nombre de la persona a añadir\n");
while(getchar()!='\n');
gets(nueva.nombre);
printf("Introduzca el alias de la persona a añadir\n");
gets(nueva.alias);
printf("Introduzca la nacionalidad de la persona a añadir\n");
gets(nueva.nacionalidad);
printf("Introduzca la edad de la persona a añadir\n");
scanf("%d", &nueva.edad);
if(insertarPersona(universo, capacidadmax, nueva)==1){
strcpy(universo[i].nombre, nueva.nombre);
strcpy(universo[i].alias, nueva.alias);
strcpy(universo[i].nacionalidad, nueva.nacionalidad);
universo[i].edad=nueva.edad;
printf("Persona añadida!\n");
i++;
}
else{
printf("El universo esta lleno o dicha persona ya esta dentro\n");
}
}else if(seleccion==2){
printf("pers. Nombre\t\t\t\t\t Alias\t\t\t\t Nacionalidad\t\t\t Edad\n");
printf("=====================================================================================================================\n");
mostrarMundo(universo, capacidadmax);
}
printf("%d",i);
}while (seleccion !=5);
return 0;
}
int insertarPersona(struct persona universo[], int capacidad_max, struct persona nueva){
capacidad_max=1000;
int espersona=0;
for(int i=0; i=='\0'&& i<capacidad_max;i++){
if ((strcmp(nueva.nombre, universo[i].nombre)&& (nueva.edad!=universo[i].edad)&& strcmp(nueva.nacionalidad, universo[i].nacionalidad)&& strcmp(nueva.alias, universo[i].alias)) !=0 && i<1000){
espersona=1;
}else {
espersona=0;
}
}
return espersona;
}
void mostrarMundo(struct persona universo[], int capacidad_max){
capacidad_max=1000;
for(int i=0; i=='\0'&&i<capacidad_max; i++){
printf("%d\t%-35s%-25s\t%-20s%10d\n", i+1, universo[i].nombre, universo[i].alias, universo[i].nacionalidad, universo[i].edad);
if(universo==0){
printf("Universo no habitado\n");
}
}
}
The Exercise is about filling Products names with their Stock capacity in file named fichierProduit and filling the movement (Import and Export) of this products in file named fichierMouvment. After filling these 2 files I create another file fichierdeResultat so I can write in it the new Stock Capacity. After doing some math the function related to this is ResultatdeMouvement, when displaying the Result it only displays the first product.
I tried to copy the name into the record "Resultat" (R) and then write it into the fichierdeResultat while displaying them in the function AfficheDeProduitApresMouvement it seems displaying only the first product name in file fichierProduit.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct Produit{
char NomP[10] ;
int StockP;
};
struct Mouvement{
char MouveP[10];
int MouveAp;
int MouveV;
};
struct Resultat{
char NomPr[10];
int StockR;
};
//INSERTING NEW PRODUCTS LIST
void SaisirProduits(FILE* ficheierProduit){
struct Produit P;
ficheierProduit = fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FProduit.dat","wb");
char rep;
do {
fseek(ficheierProduit, 0, SEEK_END);
printf("Saisir Nom de Produit: ");
scanf("%9s",&P.NomP);
printf("Saisir Capacite de Stock de Produit: ");
scanf("%i",&P.StockP);
fwrite (&P, sizeof(struct Produit), 1,ficheierProduit);
if(fwrite!=0){
printf("\n\Produit Ajoute avec succees !\n\n");
}
printf("Voulez Vouz Saisir un autre Produit (O,N): ");
scanf(" %c",&rep);
}while(toupper(rep)!='N');
fclose(ficheierProduit);
}
//INSERTING NEW MOVEMENT LIST
void SaisirMouvement(FILE* fichierMouvment){
struct Mouvement M;
fichierMouvment = fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FMouvment.dat","wb");
char rep;
do {
fseek(fichierMouvment, 0, SEEK_END);
printf("Saisir Nom de Produit a Ajouter un mouvement: ");
scanf("%9s",&M.MouveP);
printf("Saisir Capacite d'approvisionnement: ");
scanf("%i",&M.MouveAp);
printf("Saisir Nombre Vendu de ce Produit: ");
scanf("%i",&M.MouveV);
fwrite (&M, sizeof(struct Mouvement), 1,fichierMouvment);
if(fwrite!=0){
printf("\n\Mouvement Ajoute avec succees !\n\n");
}
printf("Voulez Vouz Saisir un autre Mouvement (O,N): ");
scanf(" %c",&rep);
}while(toupper(rep)!='N');
fclose(fichierMouvment);
}
//Function to verify if the Products Exist or not
int VerifierProduitExistouNon(char NomdeProduit[10],FILE* ficheierProduit){
struct Produit P; ficheierProduit=fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FProduit.dat","r+");
int found=0;
while(!feof(ficheierProduit)){
fread(&P,sizeof(P),1,ficheierProduit);
if(strcmp(P.NomP,NomdeProduit)==0){
found=1;
return found;
}else if(feof(ficheierProduit) && found==0)
return found;
}
fclose(ficheierProduit);
}
//Add New Products to the File
void AjouterNouveauProduit(FILE* fichierProduit){
struct Produit P;
fichierProduit=fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FProduit.dat","ab+");
printf("Saisir Nom de Nouveau Produit: ");
scanf("%9s",&P.NomP);
if(VerifierProduitExistouNon(P.NomP,fichierProduit)!=0)
printf("\n\nProduit deja Exist Merci de Verifier !\n\n");
else{
printf("\nSaisir Capcite de Stock de Produit: ");
scanf("%i",&P.StockP);
fwrite (&P, sizeof(struct Produit), 1,fichierProduit);
printf("\nSaisir de nouveau Produit avec Success ! \n");
}
fclose(fichierProduit);
}
//INSERTING NEW MOUVEMENT
void AjouterNouveauMouvement(FILE* fichierMouvment,FILE* fichierProduit){
struct Mouvement M;
fichierMouvment = fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FMouvment.dat","ab+");
printf("Saisir Nom de Produit a Verifier S'il exist: ");
scanf("%9s",&M.MouveP);
if(VerifierProduitExistouNon(M.MouveP,fichierProduit)!=0){
printf("\n\nProduit Exist Ajout de Nouveau Mouvement.....\n\n");
printf("\nSaisir Capcite d'approvisionnement de Ce Produit: ");
scanf("%i",&M.MouveAp);
printf("\nSaisir Capcite de Vente de Ce Produit: ");
scanf("%i",&M.MouveV);
fwrite (&M, sizeof(struct Mouvement), 1,fichierMouvment);
if(fwrite!=0){
printf("\nSaisir de nouveau Produit avec Success ! \n");
}
}else{
printf("\n\nCe Produit N existe pas dans le Stock Merci de Verifier ! \n\n");
}
fclose(fichierMouvment);
}
//TRYING TO FILE THE RESULT FILE
void ResultatdeMouvement(FILE* ficheierProduit,FILE* fichierMouvment,FILE* fichierResultat){
struct Produit P;
struct Mouvement M;
struct Resultat R; ficheierProduit=fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FProduit.dat","r+");
fichierMouvment=fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FMouvment.dat","r+");
fichierResultat=fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FResultat.dat","wb");
while(!feof(ficheierProduit)){
fread(&P,sizeof(P),1,ficheierProduit);
while(!feof(fichierMouvment)){
fread(&M,sizeof(M),1,fichierMouvment);
if(strcmp(P.NomP,M.MouveP)==0){
strcpy(R.NomPr,P.NomP);
R.StockR=P.StockP+(M.MouveAp-M.MouveV);
}
}
fwrite(&R,sizeof(R),1,fichierResultat);
}
fclose(ficheierProduit);
fclose(fichierMouvment);
fclose(fichierResultat);
}
void AfficheDeMouvement(FILE* fichierdeMouvement){
struct Mouvement M;
fichierdeMouvement=fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FMouvment.dat","r+");
while(fread(&M,sizeof(M),1,fichierdeMouvement)){
printf("\n\nNom de Produit: %s | approvisionnement= %i | Vente= %i\n\n ",M.MouveP,M.MouveAp,M.MouveV);
}
fclose(fichierdeMouvement);
}
void AfficheDeProduitavantMouvement(FILE* fichierProduit){
struct Produit P;
fichierProduit=fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FProduit.dat","r+");
while(fread(&P,sizeof(P),1,fichierProduit)){
printf("\n\nNom de Produit: %s | Stock Valable = %i \n\n",P.NomP,P.StockP);
}
fclose(fichierProduit);
}
void AfficheDeProduitApresMouvement(FILE* fichierResultat){
struct Resultat R;
fichierResultat=fopen("C:/Users/Ayoub/Desktop/EX5/EX5/FResultat.dat","r+");
while(fread(&R,sizeof(R),1,fichierResultat)){
printf("\n\nNom de Produit: %s | Stock Valable Apres Mouvement: %i \n",R.NomPr,R.StockR);
}
fclose(fichierResultat);
}
//Main
int main()
{ FILE* Produit;//file of the products
FILE* MouvementdeProduit;//file of the mouvment
FILE* ResultatdeMouvedeProduit;//file of the result
AfficheDeProduitavantMouvement(Produit);
AfficheDeMouvement(MouvementdeProduit); ResultatdeMouvement(Produit,MouvementdeProduit,ResultatdeMouvedeProduit);
AfficheDeProduitApresMouvement(ResultatdeMouvedeProduit);
}
There are many problems with your code, some of which I addressed in the comments. I will/did not identify all problems. I will just point you to the problem you are asking about.
The problem is that you skip a lot of records in the input file, so before processing the next product you must rewind the input file:
while(fread(&P,sizeof(P),1,ficheierProduit))
{
// You now have a product.
// Now collect its stock.
R.StockR= 0;
*R.NomPr= '\0';
while(fread(&M,sizeof(M),1,fichierMouvment))
{
if(strcmp(P.NomP,M.MouveP)==0){
strcpy(R.NomPr,P.NomP);
R.StockR=P.StockP+(M.MouveAp-M.MouveV);
}
}
if (*R.NomPr!='\0') fwrite(&R,sizeof(R),1,fichierResultat);
// You have now possibly skipped lots of products.
// So you must rewind the file.
fseek(fichierMouvment,0, SEEK_SET);
}
I have to do create a tree to register some passengers in it (from a plane flight) and then i will search them by their first letter.
My issue is that insert and print function work very well, but the search function do well for first time but when I want to do some other thing after in the main, it doesn't run the other function or things
(I tried to ask the user a letter, then use the function on research (works well here) but then for a second time to ask an other letter to user, and it prints "first letter of the person you are looking for" but i can't write the letter and it does not launch the function after this. so it stop the program there
int i;
char c;
typedef struct Passager
{
char nom[20];
char prenom[20];
int age;
int num_siege;
} Passager;
Passager liste_passagers[30]; //30 = nombre de passagers
typedef struct Arbre
{
Passager value;
struct Arbre *fils_gauche;
struct Arbre *fils_droit;
struct Arbre *racine;
} Arbre;
Arbre *const empty = NULL;
Arbre *passengers = empty; //passengers = arbre des passagers
/*--------------------------------------------*/
void liste_lettre_nom(Arbre *tree, char a)
{
Arbre *temp;
temp = tree;
if (!temp)
{
return;
}
else
{
if (a < temp->value.nom[0])
{
liste_lettre_nom(temp->fils_gauche, a);
}
if (a > temp->value.nom[0])
{
liste_lettre_nom(temp->fils_droit, a);
}
if (a == temp->value.nom[0])
{
printf("passager : \n");
print_passager(temp->value);
liste_lettre_nom(temp->fils_gauche, a);
liste_lettre_nom(temp->fils_droit, a);
}
}
}
/*--------------------------------------------*/
int main()
{
FILE* fichier = NULL;
fichier = fopen("/Users/Patoch/Desktop/Patoch /UNI/Informatique/info sem 2/Structure de données/Labo/TP3/Passager2.txt", "r");
if (fichier == NULL)
{ //test de la bonne ouverture du fichiers
printf("Impossible d'ouvrir le fichier Passagers.docx");
exit(EXIT_FAILURE);
}
for (i=0; i<(sizeof(liste_passagers)/sizeof(liste_passagers[0])); i++)
{
fscanf(fichier, "%s %s %d %d", liste_passagers[i].nom, liste_passagers[i].prenom, &liste_passagers[i].age, &liste_passagers[i].num_siege);
}
passengers = insertion(passengers, liste_passagers[1]);
passengers = insertion(passengers, liste_passagers[2]);
passengers = insertion(passengers, liste_passagers[0]);
passengers = insertion(passengers, liste_passagers[5]);
passengers = insertion(passengers, liste_passagers[26]);
print_arbre(passengers);
printf("-----------------------------\n");
printf("1ere lettre du nom de la personne recherchée: \n");
scanf("%c", &c);
liste_lettre_nom(passengers, c);
printf("-----------------------------\n");
printf("1ere lettre du nom de la personne recherchée: \n");
scanf(" %c", &c);
liste_lettre_nom(passengers, c);
destroy(passengers);
fclose(fichier);
return 0;
}
I have this code and I don't know why after, I ask if you want to introduce another student and I say 1 or 0 the program ends and said segmentation fault (core dumped).
I ask to introduce another student in _nodo *insertaEnLista
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct actividades
{
char tipoDeActividad[22];
char diaDeLaSemana[12];
char horaDeIncio[8];
char horaDeFin[8];
};
struct materias
{
char nombre[30];
char profesor[30];
char tipoDeMateria[20];
struct actividades *actividad;
};
struct alumnos
{
char nombre[30];
int cedula;
int telefono;
struct materias *materia;
struct alumnos *siguiente;
};
typedef struct alumnos _nodo;
_nodo *crearLista(_nodo *apuntador);
bool listaVacia(_nodo *apuntador);
_nodo *insetarEnLista(char nombre[], long cedula, long telefono, _nodo *apuntador);
void imprimirLista (_nodo *apuntador);
_nodo *crearNodo(char nombre[], long int cedula, long int telefono);
//AQUI SE CREA LISTA Y SE PONE PARA QUE APUNTE A NULL
_nodo *crearLista(_nodo *apuntador)
{
return (apuntador = NULL);
}
//ESTA FUNCION VERIFICA SI LA LISTA ESTA VACIA
bool listaVacia(_nodo *apuntador)
{
if (apuntador == NULL)
return (true);
else
return (false);
}
//AQUI SE CREA EL NUEVO NODO DE LA LISTA
_nodo *crearNodo(char nombre[], long cedula, long telefono)
{
_nodo *registroNuevo;
registroNuevo = (_nodo *) malloc(sizeof(_nodo));
printf("\n----NUEVO ELEMENTO----\n");
printf("NOMBRE: ");
fflush(stdin);
scanf("%s",nombre);
printf("CEDULA: ");
fflush(stdin);
scanf("%ld", &cedula);
printf("TELEFONO: ");
fflush(stdin);
scanf("%ld", &telefono);
fflush(stdin);
strcpy(registroNuevo->nombre, nombre);
registroNuevo->cedula = cedula;
registroNuevo->telefono = telefono;
registroNuevo->siguiente = NULL;
return registroNuevo;
}
//AQUI SE INSERTA EL NODO EN LA LISTA LUGEO DE SER CREADO POR LA FUNCION crearNodo
_nodo *insetarEnLista(char nombre[], long cedula, long telefono, _nodo *apuntador)
{
_nodo *registroNuevo, *apuntadorAuxiliar;
char respuesta,ch;
do
{
registroNuevo=crearNodo(nombre, cedula, telefono);
if (listaVacia(apuntador)) apuntador = registroNuevo;
else
{
apuntadorAuxiliar = apuntador;
while (apuntadorAuxiliar->siguiente != NULL)
apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
apuntadorAuxiliar->siguiente = registroNuevo;
}
printf("\nPARA INGRESAR A OTRO ALUMNO MARQUE... 1");
printf("\nPARA SALIR MARQUE... '0'\n");
while((ch = getchar()) != EOF && ch != '\n');
scanf("%c", &respuesta);
fflush(stdin);
printf("RESPUESTA = %c", respuesta);
}while (strcmp(&respuesta, "1")==0);
return apuntador;
}
//IMPRIMIR LOS NODOS DE LA LISTA
void imprimirLista (_nodo *apuntador)
{
_nodo *apuntadorAuxiliar;
apuntadorAuxiliar = apuntador;
if (apuntador == NULL)
printf("NO HAY ELEMENTOS EN LA LISTA \n");
else
{
while(apuntador != NULL)
{
printf(" \n------------NODO-------------- ");
printf("\nNOMBRE: %s \n\n", apuntadorAuxiliar->nombre);
printf("\n\nCEDULA: %d \n", apuntadorAuxiliar->cedula);
printf("\nTELEFONO: %d \n", apuntadorAuxiliar->telefono);
apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
}
}
return;
}
int main()
{
/*printf("INTRODUZCA LOS NUMEROS DE CEDULA QUE DESEA IMPRIMIR \n");*/
_nodo *inicioLista;
int cedula;
int telefono;
char nombre[20];
inicioLista = crearLista(inicioLista);
inicioLista = insetarEnLista(nombre, cedula, telefono, inicioLista);
imprimirLista(inicioLista);
return 0;
}
How can I do to fix the problem.
You should step through the code in a debugger and look at the variables at each step to determine the line of code that is causing the issue.
Here is one issue, there may be others.
In this line
}while (strcmp(&respuesta, "1")==0);
you are using strcmp with a variable (respuesta) that contains a single character. strcmp is expecting a null terminated string (an array of characters with a zero byte at the end). As you may not have a zero byte after the variable, this may cause strcmp to read memory that it shouldn't (this is a buffer overrun)
Much simpler to just use:
}while (respuesta == '1');