这是我的代码。这是家庭作业,但我已经做到了。但搜索功能不起作用。我知道我在做错事。用户必须输入要搜索的名称,并且应显示整个条目。在void搜索功能中出现错误,该错误表示“错误C2120:'void'对所有类型都是非法的”
// 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;
}
}
答案 0 :(得分:0)
我注意到的最明显的事情是,obj
变量中存储的'main'类型的结构看起来像是在任何特定的类实例之外。这意味着Main,Employee,Student或Teacher类型的每个对象只写入自己范围之外的单个obj。所有员工,教师,学生,“主要”将分享您上次写的任何FName,Last,Address值。
你可能想要的是Main的每个实例都有自己的FName,Last,Address等......
class Main
{
struct main
{
char FName[20];
char Last[20];
char Address[30];
long Phone;
int ID;
long CNIC;
}obj;
public:
virtual void init()
{
... rest of code here
答案 1 :(得分: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表达式中使用它。
答案 2 :(得分:0)
请将您的帖子减少到主要问题。如果你只是在一个“代码”块中列出所有内容。没人能理解你的所作所为。
1:不要使用“转到”!(!!!!!)
2:你真的应该使用bool而不是int for flags
3:不要将main / Main用于结构或类名。它实际上是为mainmathod保留的(在某些系统上)并且让一个类和一个具有相同名称的结构令人困惑(忽略大小写)
4:您正在访问Main :: view()函数;在您的搜索方法中。但是在哪个对象上?通常,您应该将数据加载到对象并通过this-&gt; view()访问它;
5:整个概念看似有线。为什么使用文本文件和多态类层次结构?文本文件转储层次结构。也许你应该重新考虑你的阶级概念。