gpt4 book ai didi

c++ - 搜索功能不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:16:47 25 4
gpt4 key购买 nike

这是我的代码。这是家庭作业,但我已经全部完成了。但是搜索功能不起作用。我知道我做错了什么。用户必须输入名称才能搜索,它应该会显示整个条目。 void 搜索函数出现错误,显示“error C2120: 'void' illegal with all types”

            // Awholenew world.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<process.h>
#include<iomanip>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<sstream>
#include<iostream>
#include<conio.h>
#include<stdlib.h>

using namespace std;

struct main
{
char FName[20];
char Last[20];
char Address[30];
long Phone;
int ID;
long CNIC;
}obj;

class Main
{

public:
virtual void init()
{
cin.getline(obj.FName,20);
cout << "Enter Name: ";
cin.getline(obj.Last,20);

cout << "Enter Address: ";
cin.getline(obj.Address, 30);
cout << "Enter Phone: ";
cin>>obj.Phone;

}
void view()
{
cout << "Name: " << obj.Last<< endl;
cout << "Address: " << obj.Address << endl;
cout << "Phone: " << obj.Phone << endl;
cout << "CNIC: " << obj.CNIC << endl;
cout << "ID: " << obj.ID << endl;

}




virtual void search()
{
char choice4;
char Target[20];
int Found=0;

fstream fin;
if (fin.open("Main.txt", ios::in| ios::out) == NULL)
cout<<"File is empty" << endl;
else
cout<<"Enter Name: " << endl;
cin.getline(Target, 20);

while(!fin.eof() && Found ==0)
{
fin<< endl << obj.Last << endl <<obj.Address <<endl <<obj.Phone << endl << obj.CNIC << endl << obj.ID;
if (strcmp(Target, obj.Last) == 0)
Found =1;
}
if(Found)
{
Main::view();

}
else if (!Found)
printf("**There is no such Entry**\n");
fin.close();

}



void display()
{

char BUFFER[100];
ifstream fin("Main.txt");


while (!fin.eof())
{
fin.getline(BUFFER, 100);
cout << BUFFER << endl;

}
}

};

class Teacher : public Main
{
public:

void tinit()
{
ofstream fin("Main.txt", ios::app);
Main::init();
cout << "Enter CNIC of Teacher" << endl;
cin>>obj.CNIC;
fin<< endl << obj.Last << endl << obj.Address << endl << obj.Phone << endl << "Teacher CNIC: " << obj.CNIC << endl;

}

};

class Student : public Main
{
public:
void sinit()
{
ofstream fin("Main.txt", ios::app);
Main::init();
cout << "Enter ID of Student" << endl;
cin>>obj.Phone;
fin<< endl << obj.Last <<endl << obj.Address << endl << obj.Phone << endl << "Student ID" << obj.ID << endl;
}

};

class Employee : public Main
{
public:
void einit()
{

ofstream fin("Main.txt", ios::app);
Main::init();
cout << "Enter Employee CNIC" << endl;
cin>>obj.CNIC;
fin << endl << obj.Last <<endl << obj.Address << endl << obj.Phone << endl << "Employee CNIC: " << obj.CNIC << endl;
}

};
int _tmain(int argc, _TCHAR* argv[])
{
Main* myarr[100];
Teacher* tearr[100];
Student* starr[100];
Employee* emarr[100];
int var=0;
int choice;
char input;

start:
printf("===============MAIN MENU===============");
printf("\n[1]Insert\n[2]Display All\n[3]Search\n[4]Exit\n");
printf("=======================================");
printf("\n\nEnter Your Choice: ");

cin >> choice;

switch(choice)
{

case 1:
label:
cout<< "Enter Choice s/t/e" << endl;
cin >> input;


if (input == 't')
tearr[var]->tinit();
if (input == 's')
starr[var]->sinit();
if (input == 'e')
emarr[var]->einit();

cout << " Enter another (y/n)? ";
cin >> input;

if (input == 'y')
goto label;
else
goto start;
break;
case 2:
myarr[var]->display();
break;
case 3:
myarr[var]->search();
break;
case 4:
cout << "Are you sure? y/n" << endl;
char in;
cin>>in;
if (in=='y')
getch();
else goto start;
break;
default:
return 0;
break;

return 0;
}
}

最佳答案

我能看到的一个问题是:

fstream fin;
if (fin.open("Main.txt", ios::in| ios::out) == NULL)

函数 fstream::open 声明为:

void open ( const char * filename, 
ios_base:openmode mode = ios_base::in | ios_base::out );

如您所见,它的返回类型是 void,因此您不能在 if 表达式中使用它。

关于c++ - 搜索功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10425757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com