mercredi 28 juillet 2021

How do I get the program to work? It is a college manangement system project made from scratch by a noob

this is a college management system program, where student can sign up, sign in. The username password and id are stored in a file. which are used later by fstream to do certain tasks like printing information. There is also another feature where the students could ask a question(line 98) and the admin will be able to answer it(line 187), and then the student will be able to view the question and answer(line 81). My program gets stuck in a loop and also need to fix the errors which i definitely made. I built this program totally from scratch myself. And also I am new to programming. Thank you!

#include <iostream>
#include <string>
#include <fstream>
#include<stdlib.h>
#include<string.h>
#include<ctime>
using namespace std;
void signIn();
void signUp();
void admin();

int main(){
    int choicE;
    cout<<"====================================================="<<endl;
    cout<<"=============College Management System==============="<<endl;
    cout<<"====================================================="<<endl;
    cout<<endl<<endl;
    cout<<"1) Student sign in"<<endl;
    cout<<"2) Student sign up"<<endl;
    cout<<"3) Admin"<<endl<<endl;
    cout<<"Enter Your Choice: "<<choicE<<endl;

    switch(choicE)
        {
                case 1:
                        signIn();
                        break;
                case 2:
                        signUp();
                        break;
                case 3:
                        admin();
                        break;
                default:
                        system("cls");
                        cout<<"Wrong choice!\nTry again!"<<endl; 
                        main();
        }


    return 0;
}
void signIn(){
        int counter,id, acount;
        string user,pass,u,p,m;
        system("cls");
        cout<<"please enter the following details"<<endl;
        cout<<"USERNAME :";
        cin>>user;
        cout<<"PASSWORD :";
        cin>>pass;
        
        ifstream f("student.txt");
        while(f>>id>>u>>m>>p)
        {
                if(u==user && p==pass)
        
                {
                        counter=1;
                        system("cls");
                }else
                {
                cout<<"Either username or password is incorrect";
                main();
                }
        }
        f.close();
        if(counter==1)
        {
            int choiCe;
            string question;
                cout<<"\nWelcome"<<user<<"!"<<endl<<endl;
                cout<<"1) Check Messages"<<endl;
                cout<<"2) Ask Question"<<endl;
                cout<<"3) Account info"<<endl;
                cout<<"Enter anything else to exit."<<endl<<endl;
                cout<<"Enter your choice: "<<choiCe;
                if(choiCe==1){
                    string q,q2,a;
                    int count;
                    ifstream y("question.txt");
                    ifstream x("answer.txt");
                        while(y>>q && x>>q2>>a)
                        {
                                if(q==q2)
                                {
                                        count=1;
                                }
                        }
                        y.close();
                        x.close();
                        if(count==1){
                            cout<<q2<<a;
                        }
                }else if(choiCe==2){
                    system("cls");
                    cout<<"Enter Your Question: "<<endl;
                    getline(cin, question);
                    ofstream g("question.txt",ios::app);
                    g<<question<<endl;
                    cout<<"Question will be answered shortly. Come back later!"<<endl;
                }else if(choiCe==3){
                    int aid;
                    cout<<"Enter your ID"<<aid;
                    ifstream h("student.txt");
                    while(h>>id>>u>>m>>p){
                        if(id==aid){
                            acount=1;
                        }
                    }
                    h.close();
                    if(acount==1){
                        int no;
                        cout<<"Major: "<<m<<endl;
                        cout<<"Username: "<<u<<endl;
                        cout<<"Password: "<<p<<endl;
                        cout<<"Press 1 to exit: "<<no;
                        if(no==1){
                            main();
                        }
                    }
                }else{
                    main();      
                }
         
                
        }
        
}
void signUp(){
    string nuser,npass, nmajor;
    string fname, lname;
        system("cls");
        cout<<"Enter First Name: ";
        cin>>fname;
        cout<<"\nEnter Last Name: ";
        cin>>lname;
        cout<<"\nCreate a Username: ";
        cin>>nuser;
        cout<<"\nEnter Major: ";
        cin>>nmajor;
        cout<<"\nEnter the password: ";
        cin>>npass;
        int id=rand();
        ofstream i("student.txt",ios::app);
        i<<id<<' '<<nuser<<' '<<nmajor<<' '<<npass<<endl;
        system("cls");
        cout<<"\nRegistration Sucessful\n";
        main();
}
void admin()
{
    int adpass, choIce;
    cout<<"Enter Password: "<<adpass<<endl;
    if(adpass==1234)
    {
        cout<<"1) View Student Info"<<endl;
        cout<<"2) Answer Questions"<<endl;
        cout<<"3) Exit"<<endl<<endl;
        cout<<"Enter your choice: "<<choIce;
        
        if(choIce==1)
        {
            int aid,acount,id;
            string u,m,p;
            cout<<"Enter student ID"<<aid;
            ifstream j("student.txt");
            while(j>>id>>u>>m>>p){
                if(id==aid){
                    acount=1;
                }
            }
            j.close();
            if(acount==1){
                int no;
                cout<<"Major: "<<m<<endl;
                cout<<"Username: "<<u<<endl;
                cout<<"Password: "<<p<<endl;
                cout<<"Press 1 to exit: "<<no;
                if(no==1){
                    main();
                }
            }
        }else if(choIce==2)
        {
            fstream l("question.txt");
            
            if(l.is_open()){
                cout<<l.rdbuf();
            }
            l.close();
            string q,a;
            cout<<"Enter the question you would like to answer:"<<endl;
            getline(cin,q);
            cout<<"Enter the answer:"<<endl;
            getline(cin,a);
            ofstream z("answer.txt",ios::app);
            z<<q<<a<<endl;
        }else{
            main();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire