Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7870265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:34:07+00:00 2026-06-03T01:34:07+00:00

Here is my code. It’s homework but i have done it all. But the

  • 0

Here is my code. It’s homework but i have done it all. But the search function does not work. I know Im doing something wrong in it. The user has to enter a name to search and it should display the whole entry. There is an error at the void search function which says “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;
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:34:08+00:00Added an answer on June 3, 2026 at 1:34 am

    One problem I can see is:

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

    The function fstream::open is declared as:

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

    as you can see its return type is void, so you cannot use it in the if expression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

enter code here Hi All, I have a simple windows service application that connects
Here is code from MSDN . I don't understand why the work isn't just
enter code here I have a table on SQL server 2005 with bigint primary
The code here is X++. I know very little about it, though I am
enter code here I have a ListView that have a different Layout XML for
have a php code like this,going to convert it in to C#. function isValid($n){
I know this sounds very dumb, but we have a client that sends out
I have UiTextView where I want change font size (increase), here code -(void)biggerFont:(UIBarButtonItem *)
Here code of my project. I use direct Tcp connection But i get Could
Why is substringFromIndex not working for my NSMutableString ? Here code similar to what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.