2
嗨我是新編程與iostream和指針我想知道爲什麼在我的函數buscar();在我想要打印的所有內容中,我在「]」之前得到主表達式錯誤。在另一個函數中,alta();我使用了函數指針,所以當我在函數buscar()中調用相同的信息時;我是否使用函數alta()中的指針,我的結構的名稱或對象的名稱?「]」之前的主表達式錯誤「
謝謝你的支持!
#include<iostream>
#include<string.h>
#include<fstream>
#include<cstring>
using namespace std;
typedef struct libro {
int registro;
char nombre[50];
char autor[50];
char editorial[50];
int year;
int volumen;
}lib;
int n;
int numper=0;
int ye;
char nom[25];
int i=0;
void alta(libro*puntero);
void buscar();
void alta(libro*p){
for(i;i<n;i++){
cout<<"Ingrese el nombre del libro:"<<endl;
fflush(stdin);gets(p[i].nombre);
cout<<"ingrese su registro"<<endl;
cin>>p[i].registro;
cout<<"ingrese el autor"<<endl;
fflush(stdin);gets(p[i].autor);
cout<<"ingrese el nombre del editorial"<<endl;
fflush(stdin);gets(p[i].editorial);
cout<<"ingrese el year del libro"<<endl;
cin>>p[i].year;
cout<<"ingrese la cantidad de volumenes que quiere ingresar"<<endl;
cin>>p[i].volumen;
}
}
void buscar(){
int i, j, op;
int encontrado= 0;
cout<< "desea buscar por 1:nombre 2:telefono\n";
cin>>op;
switch (op)
{
case 1:
cout<<"escriba el nombre que va buscar\n";
cin>>nom;
for(i=0;i<numper;i++)
if (strcmp(libro[i].nombre,nom)==0)
{
if (libro[i].year!=0){
cout<< "numero de registro: "<<libro[i].registro;
cout<<"Nombre : "<<libro[i].nombre;
cout<<"Editorial: "<<libro[i].editorial;
cout<<"Year: "<<libro[i].year;
cout<<"Volumenes: "<<libro[i].volumen;
cout<<"------------------------------"<<endl;
j=1;
system ("pause");
system("cls");
}
}
if(j!=1){
cout<<"no existe ese libro"<<endl;
system ("pause");
system ("cls");
}
break;
case 2:
cout<<"Ingrese el Year del libro:\n";
cin>>ye;
for(i=0;i<numper;i++)
{
if (libro[i].year==ye)
{
encontrado=1;
j=i;
}
}
break;
if(encontrado==1)
{
cout<< "numero de registro: "<<libro[i].registro;
cout<<"Nombre : "<<libro[i].nombre;
cout<<"Editorial: "<<libro[i].editorial;
cout<<"Year: "<<libro[i].year;
cout<<"Volumenes: "<<libro[i].volumen;
cout<<"------------------------------"<<endl;
}
else
cout<<"no existe un libro con ese year.";
system("pause");
system ("cls");
}//switch
}
int main(){
libro r[50];
int opc=0;
do{
cout<<"Selecione opcion:"<<endl;
cout<<"1) Alta"<<endl;
cout<<"2) BUSCAR"<<endl;
cout<<"3) SALIR"<<endl;
cin>>opc;
switch(opc){
case 1:{
cout<<"cuantos libros quiere dar de alta"<<endl;
cin>>n;
alta(r);
break;
case 2:
buscar(r);
} break;
}
} while(opc!=2);
system("pause");
return 0;
}
'libro'是一個結構體,所以你不能寫'libro [i]'。 – v7d8dpo4