Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
#include <string>
#include <conio.h>
#include<fstream>
#include<iomanip>
 
using namespace std;

int main ()
{
	char ch;
	start:
    string userName;
    string userPassword;

    
    {
    	cout << "Welcom to Asian Institute of Computer studies\n\n";
        cout << "User name: ";
        cin >> userName;
        cout << "Password: ";
        ch = getch();
 
        while (ch !=13)
        {
		userPassword.push_back(ch);
		cout << '*';
		ch = getch();
		}

        if (userName == "Admin" && userPassword == "pulmano052802")
        {
        	cout << "\n\n";
        	cout << "ACCESS GRANTED\n";
            cout << "Welcome!\n";
            return 0;
        }
        else
        {
        	cout << "\n\n";
            cout << "ACCESS DENIED\n" << '\n';
            cout << "Invalid login attempt. Please try again.\n" << '\n';
            goto start;
        }
    }

    cout << "Thank you for logging in.\n";
    return 0;
}
class student
{
	int idnum;
	char name[50];
	int attendance, quiz, recitation, project, exam;
	double per,avg;
	char grade;
	void calculate();	
public:
	void getdata();		
	void showdata() const;	
	void show_tabular() const;
	int getIDNum() const;
}; 
void student::calculate()
{
	per = (attendance + quiz + recitation + project + exam )/5.0;
	avg = per;
	if(avg>=75.0 && avg <=100.0)
	cout<<"Grade rating: Passed\n\n\n";
	else if(avg>=1.0 && avg<=74.9)
    cout<<"Grade rating: Failed\n\n\n";
    
}
 
 

void student::getdata()
{
	cout<<"\nEnter The ID number of the student ";
	cin>>idnum;
	cout<<"\n\nEnter student's Name: ";
	cin.ignore();
	cin.getline(name,50);
	cout<<"\nEnter attendance grade: ";
	cin>>attendance;
	cout<<"\nEnter quiz grade: ";
	cin>>quiz;
	cout<<"\nEnter recitation grade: ";
	cin>>recitation;
	cout<<"\nEnter project grade: ";
	cin>>project;
	cout<<"\nEnter exam grade: ";
	cin>>exam;
	calculate();
}
 
void student::showdata() const
{
	cout<<"\nID Number: "<<idnum;
	cout<<"\nName: "<<name;
	cout<<"\nattendance: "<<attendance;
	cout<<"\nquiz: "<<quiz;
	cout<<"\nrecitation: "<<recitation;
	cout<<"\nproject: "<<project;
	cout<<"\nexam: "<<exam;
	cout<<"\nPercentage: "<<per;
	cout<<"\nGrade rating: "<<avg;

}
 
void student::show_tabular() const
{
	cout<<idnum<<setw(6)<<" "<<name<<setw(10)<<attendance<<setw(4)<<quiz<<setw(4)<<recitation<<setw(4)
		<<project<<setw(4)<<exam<<setw(8)<<per<<setw(6)<<avg<<endl;
}
 
int  student::getIDNum() const
{
	return idnum;
}
 
 
 
void SaveStudent();	
void displayAll();	
void Searchdisplay(int);	
void modifyStudent(int);	
void deleteStudent(int);	
void DisplayClassResult();	
void DisplayResult();			
	
 
 
 
 
void write_student()
{
	student st;
	ofstream outFile;
	outFile.open("student.dat",ios::binary|ios::app);
	st.getdata();
	outFile.write(reinterpret_cast<char *> (&st), sizeof(student));
	outFile.close();
    	cout<<"\n\nStudent record Has Been Created ";
	cin.ignore();
	cin.get();
}
 
 
 
void display_all()
{
	student st;
	ifstream inFile;
	inFile.open("student.dat",ios::binary);
	if(!inFile)
	{
		cout<<"File could not be open !! Press any Key...";
		cin.ignore();
		cin.get();
		return;
	}
	cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";
	while(inFile.read(reinterpret_cast<char *> (&st), sizeof(student)))
	{
		st.showdata();
		cout<<"\n\n====================================\n";
	}
	inFile.close();
	cin.ignore();
	cin.get();
}
 
 
 
void display_sp(int n)
{
	student st;
	ifstream inFile;
	inFile.open("student.dat",ios::binary);
	if(!inFile)
	{
		cout<<"File could not be open !! Press any Key...";
		cin.ignore();
		cin.get();
		return;
	}
	bool flag=false;
	while(inFile.read(reinterpret_cast<char *> (&st), sizeof(student)))
	{
		if(st.getIDNum()==n)
		{
	  		 st.showdata();
			 flag=true;
		}
	}
	inFile.close();
	if(flag==false)
		cout<<"\n\nrecord not exist";
	cin.ignore();
	cin.get();
}
 
 
void modify_student(int n)
{
	bool found=false;
	student st;
	fstream File;
	File.open("student.dat",ios::binary|ios::in|ios::out);
	if(!File)
	{
		cout<<"File could not be open !! Press any Key...";
		cin.ignore();
		cin.get();
		return;
	}
    	while(!File.eof() && found==false)
	{
 
		File.read(reinterpret_cast<char *> (&st), sizeof(student));
		if(st.getIDNum()==n)
		{
			st.showdata();
			cout<<"\n\nPlease Enter The New Details of student"<<endl;
			st.getdata();
		    	int pos=(-1)*static_cast<int>(sizeof(st));
		    	File.seekp(pos,ios::cur);
		    	File.write(reinterpret_cast<char *> (&st), sizeof(student));
		    	cout<<"\n\n\t Record Updated";
		    	found=true;
		}
	}
	File.close();
	if(found==false)
		cout<<"\n\n Record Not Found ";
	cin.ignore();
	cin.get();
}
 
 
 
void delete_student(int n)
{
	student st;
	ifstream inFile;
	inFile.open("student.dat",ios::binary);
	if(!inFile)
	{
		cout<<"File could not be open !! Press any Key...";
		cin.ignore();
		cin.get();
		return;
	}
	ofstream outFile;
	outFile.open("Temp.dat",ios::out);
	inFile.seekg(0,ios::beg);
	while(inFile.read(reinterpret_cast<char *> (&st), sizeof(student)))
	{
		if(st.getIDNum()!=n)
		{
			outFile.write(reinterpret_cast<char *> (&st), sizeof(student));
		}
	}
	outFile.close();
	inFile.close();
	remove("student.dat");
	rename("Temp.dat","student.dat");
	cout<<"\n\n\tRecord Deleted ..";
	cin.ignore();
	cin.get();
}
 
 
void class_result()
{
	student st;
	ifstream inFile;
	inFile.open("student.dat",ios::binary);
	if(!inFile)
	{
		cout<<"File could not be open !! Press any Key...";
		cin.ignore();
		cin.get();
		return;
	}
	cout<<"\n\n\t\tALL STUDENTS RESULT \n\n";
	cout<<"==========================================================\n";
	cout<<"R.No       Name        P   C   M   E   CS   avg% "<<endl;
	cout<<"==========================================================\n";
	while(inFile.read(reinterpret_cast<char *> (&st), sizeof(student)))
	{
		st.show_tabular();
	}
	cin.ignore();
	cin.get();
	inFile.close();
}

{
	{
	char ch;
	int num;
	cout.setf(ios::fixed|ios::showpoint);
	cout<<setprecision(2); 
	do
	{
	system("cls");
	cout<<"\t**********************************************";
	cout<<"\n\n\t1.CREATE STUDENT RECORD";
	cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORDS";
	cout<<"\n\n\t3.SEARCH STUDENT RECORD ";
	cout<<"\n\n\t4.MODIFY STUDENT RECORD";
	cout<<"\n\n\t5.DELETE STUDENT RECORD";
	cout<<"\n\n\t6. DISPLAY CLASS RESULT";
	cout<<"\n\n\t7. EXIT";
	cout<<"\n\n\t*****************************************";
	cout<<"\n\n\tPlease Enter Your Choice (1-7): ";
	cin>>ch;
	system("cls");
	switch(ch)
	{
	case '1':	write_student(); break;
	case '2':	display_all(); break;
	case '3':	cout<<"\n\n\tPlease Enter Student's ID number: "; cin>>num;
				display_sp(num); break;
	case '4':	cout<<"\n\n\tPlease Enter Student's ID number: "; cin>>num;
			modify_student(num);break;
	case '5':	cout<<"\n\n\tPlease Enter Student's ID number: "; cin>>num;
			delete_student(num);break;
	case '6' :	class_result(); break;
	case '7':	exit(0);;
	default:	cout<<"\a"; 
		
    }
	}while(ch!='7');
 
	return 0;
}
}


What I have tried:

Can someone how to fix the error of this code
the error is
[Error] expected unqualified-id before '{' token
Posted
Updated 5-Nov-21 13:06pm

At the end of the file, you have this:
C++
{
	{
		char ch;
		int num;
		cout.setf(ios::fixed | ios::showpoint);
		cout << setprecision(2);
		do
		{
			system("cls");
			cout << "\t**********************************************";
			cout << "\n\n\t1.CREATE STUDENT RECORD";
			cout << "\n\n\t2.DISPLAY ALL STUDENTS RECORDS";
			cout << "\n\n\t3.SEARCH STUDENT RECORD ";
			cout << "\n\n\t4.MODIFY STUDENT RECORD";
			cout << "\n\n\t5.DELETE STUDENT RECORD";
			cout << "\n\n\t6. DISPLAY CLASS RESULT";
			cout << "\n\n\t7. EXIT";
			cout << "\n\n\t*****************************************";
			cout << "\n\n\tPlease Enter Your Choice (1-7): ";
			cin >> ch;
			system("cls");
			switch (ch)
			{
			case '1':	write_student(); break;
			case '2':	display_all(); break;
			case '3':	cout << "\n\n\tPlease Enter Student's ID number: "; cin >> num;
				display_sp(num); break;
			case '4':	cout << "\n\n\tPlease Enter Student's ID number: "; cin >> num;
				modify_student(num); break;
			case '5':	cout << "\n\n\tPlease Enter Student's ID number: "; cin >> num;
				delete_student(num); break;
			case '6':	class_result(); break;
			case '7':	exit(0);;
			default:	cout << "\a";

			}
		} while (ch != '7');

		return 0;
	}
}

There is no function header at all and the second set of curly braces isn't needed either. You have that same problem up in the main function. This code needs a function header and you need to get rid of the redundant braces:
C++
int WhatShouldThisFunctionBeCalled()
{
    char ch;
    int num;

    cout.setf(ios::fixed | ios::showpoint);
    cout << setprecision(2);
    do
    {
        system("cls");
        cout << "\t**********************************************";
        cout << "\n\n\t1.CREATE STUDENT RECORD";
        cout << "\n\n\t2.DISPLAY ALL STUDENTS RECORDS";
        cout << "\n\n\t3.SEARCH STUDENT RECORD ";
        cout << "\n\n\t4.MODIFY STUDENT RECORD";
        cout << "\n\n\t5.DELETE STUDENT RECORD";
        cout << "\n\n\t6. DISPLAY CLASS RESULT";
        cout << "\n\n\t7. EXIT";
        cout << "\n\n\t*****************************************";
        cout << "\n\n\tPlease Enter Your Choice (1-7): ";
        cin >> ch;
        system("cls");
        switch (ch)
        {
            case '1':	write_student(); break;
            case '2':	display_all(); break;
            case '3':	cout << "\n\n\tPlease Enter Student's ID number: "; cin >> num;
                display_sp(num); break;
            case '4':	cout << "\n\n\tPlease Enter Student's ID number: "; cin >> num;
                modify_student(num); break;
            case '5':	cout << "\n\n\tPlease Enter Student's ID number: "; cin >> num;
                delete_student(num); break;
            case '6':	class_result(); break;
            case '7':	exit(0);;
            default:	cout << "\a";
        }
    } while (ch != '7');

    return 0;
}


In your main method, you have the same redundant braces problem, and a "start:" label that needs to be removed. Yu can replace the "start:/goto start" with a more appropriate structure, like a do/while.
 
Share this answer
 
v3
Dave is exactly right. There is no need for a goto statement and they should be avoid at (nearly) all costs. Especially with c++. You risk the possibility of introducing memory leaks because automatic objects will not be destroyed if the goto causes execution to jump out of their scope.

Here is one way you can write that loop :
C++
 bool granted = false;
 do
 {
     cout << "Welcom to Asian Institute of Computer studies\n\n";
     cout << "User name: ";
     cin >> userName;
     cout << "Password: ";
     ch = getch();

     while (ch !=13)
     {
         userPassword.push_back(ch);
         cout << '*';
         ch = getch();
     }

     if (userName == "Admin" && userPassword == "pulmano052802")
     {
         cout << "\n\n";
         cout << "ACCESS GRANTED\n";
         cout << "Welcome!\n";
         granted = true;
     }
     else
     {
         cout << "\n\n";
         cout << "ACCESS DENIED\n" << '\n';
         cout << "Invalid login attempt. Please try again.\n" << '\n';
     }
} while( ! granted );
 
Share this answer
 
Comments
merano99 5-Nov-21 19:03pm    
Yes, and in addition, it is better to move all of this to a subroutine.
Rick and Dave have already found and explained the errors. Here again the short version and notes:
C++
error C2447: "{": Function header missing

Although nothing has been really called anywhere below main, the error arises because a function name is missing.
--- snip --
C++
	inFile.close();
}

int unusedmenu(){  // added name here
//	{
		char ch;
...
		return 0;
//	}
}
-- snip ---
In addition, a pair of brackets seems to be unnecessary.

Using goto is not a good style of programming and should be avoided.

Note: I get this warnings at ch = getch().
warning C4996: 'getch': The POSIX name for this item is deprecated.
Instead, use the ISO C and C++ conformant name: _getch.

The function definitions for these declarations do not exist.
C++
void SaveStudent();
void displayAll();
void Searchdisplay(int);
void modifyStudent(int);
void deleteStudent(int);
void DisplayClassResult();
void DisplayResult();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900