使用强制用户在继续之前输入anthore值的函数在C ++中读取txt文件

时间:2017-09-14 23:20:30

标签: c++ persistence text-parsing qtextdocument

我希望代码能够读取创建的文件,如果他们为学生ID或学生名称找到相同的数据字符串,它将使用户在继续之前输入另一个值。我能够创建和存储数据,以便它在此时保持不变,但我还没有弄清楚如何通过文本文件进行代码搜索,并找到用户输入的重复项,同时强制用户输入新的价值。

我无法想到一种准确性的方法,可以显示包含整个程序的所有代码

 #include<iostream>
#include<fstream>
#include <cstdlib>
#include <string>
#include "student.h"
#include "course.h"
#include "session.h"
 using namespace std;


void fillStudents(student arg[], int size)
{
 for (int i = 0; i < size; i++)
 {
      cout <<"Student #"<<i+1<<endl;
      cout <<"=========="<<endl;
      cout <<"Enter a student ID (i.e 97626): ";
      cin >>arg[i].stuID;
      cout <<"Enter the first name (i.e Ryan): ";
      cin >>arg[i].fname;
      cout <<"Enter the last name (i.e Brown): ";
      cin >>arg[i].lname;
     system("CLS");
 }
 } 

  void fillCourses(course c[], int size)
 {
   for (int i = 0; i < size; i++)
   {
      cout <<"Course #"<<i+1<<endl;
      cout <<"=========="<<endl;
      cout <<"Enter course name (i.e Data-Structures (put - instead of spaces)): ";
     cin >>c[i].CourseName;
     cout <<"Enter course ID (i.e CS-230 (put - instead of spaces)): ";
     cin >>c[i].CourseID;
     cout <<"Enter number of credits (i.e 3): ";
     cin >>c[i].numberOfCredits;
     system("CLS");
 }
}

string CourseN(int choice, course ch[])
{
switch(choice)
{
    case 1:
        return ch[0].CourseID+" "+ch[0].CourseName;
        break;
    case 2:
        return ch[1].CourseID+" "+ch[1].CourseName;
        break;
    case 3:
        return ch[2].CourseID+" "+ch[2].CourseName;
        break;
    case 4:
        return ch[3].CourseID+" "+ch[3].CourseName;
        break;
    case 5:
        return ch[4].CourseID+" "+ch[4].CourseName;
        break;
    case 6:
        return ch[5].CourseID+" "+ch[5].CourseName;
        break;
    case 7:
        return ch[6].CourseID+" "+ch[6].CourseName;
        break;
    case 8:
        return ch[7].CourseID+" "+ch[7].CourseName;
        break;
    case 9:
        return ch[8].CourseID+" "+ch[8].CourseName;
        break;
    case 10:
        return ch[9].CourseID+" "+ch[9].CourseName;
        break;
    default:
        cout<<"Invalid decison!! Good Bye!"<<endl;
        exit(0);
}
}

int creditTotal(int sel, course ch[])
{
switch(sel)
{
    case 1:
        return ch[0].numberOfCredits;
        break;
    case 2:
        return ch[1].numberOfCredits;
        break;
    case 3:
        return ch[2].numberOfCredits;
        break;
    case 4:
        return ch[3].numberOfCredits;
        break;
    case 5:
        return ch[4].numberOfCredits;
        break;
    case 6:
        return ch[5].numberOfCredits;
        break;
    case 7:
        return ch[6].numberOfCredits;
        break;
    case 8:
        return ch[7].numberOfCredits;
        break;
    case 9:
        return ch[8].numberOfCredits;
        break;
    case 10:
        return ch[9].numberOfCredits;
        break;
    default:
        cout<<"Invalid Decision!! Good Bye!"<<endl;
        exit(0);
    }
}

void fillSession(student arg[], course c[], session s[])
{
int c1, c2, c3, c4;
string ch1, ch2, ch3, ch4;
string startDate, endDate;
int cr1, cr2, cr3, cr4;

   for (int i = 0; i < 4; i++)
   {
    for(int j = 0; j < 10; j++)
    {
        cout<<"class #"<<j+1<<" \t\t"<<c[j].CourseID<<" \t\t"<<c[j].CourseName<<" \t\t\t"<<"Credits: "<<c[j].numberOfCredits<<"\n\n";
    }

    cout<<"Enter your 1st class choice: ";
    cin>>c1;
    ch1=CourseN(c1,c);
    cout<<"Enter your 2nd class choice: ";
    cin>>c2;
    ch2=CourseN(c2,c);
    cout<<"Enter your 3rd class choice: ";
    cin>>c3;
    ch3=CourseN(c3,c);
    cout<<"Enter your 4th class choice: ";
    cin>>c4;
    ch4=CourseN(c4,c);

    s[i].courseID=ch1+"\n"+ch2+"\n"+ch3+"\n"+ch4;
    cout<<"Enter a start date for your classes (i.e mm/dd/yyyy): ";
    cin>>startDate;
    s[i].startDate=startDate;
    cout<<"Enter a end date for your classes (i.e mm/dd/yyyy): ";
    cin>>endDate;
    s[i].endDate=endDate;

    cr1=creditTotal(c1, c);
    cr2=creditTotal(c2, c);
    cr3=creditTotal(c3, c);
    cr4=creditTotal(c4, c);

    s[i].totalCredits=cr1+cr2+cr3+cr4;

    c1=0;
    c2=0;
    c3=0;
    c4=0;

    system("CLS");
  }
 }

void display_report(student s[], course c[], session se[])
{
  for (int i = 0; i < 4; i++)
  {
    cout<<"Student ID: "<<s[i].stuID<<"\n"<<endl;
    cout<<"Student Name: "<<s[i].lname<<", "<<s[i].fname<<"\n"<<endl;
    cout<<"Course IDs: \n"<<se[i].courseID<<"\n"<<endl;
    cout<<"Start Date: \n"<<se[i].startDate<<"\n"<<endl;
    cout<<"End Date: \n"<<se[i].endDate<<"\n"<<endl;
    cout<<"Total Credits: "<<se[i].totalCredits<<"\n"<<endl;
    cout<<"\n"<<endl;
   }
}

这是我用来创建文件并连续存储用户输入信息的功能。在这个函数中,我想尝试搜索重复的单词

 /**
 * Writes session data to file.
 * @param   {session}   sess   sessions array.
 */
void writeSessionsToFile(session* sess, student* stude)
 {
  ofstream file_out("sessions.txt", ios::app);
  // for now let us just assume there'll always be just 4 sessions.
   for (size_t i = 0; i < 4; ++i)
  {
   file_out << stude[i].lname << ", "<<stude[i].fname<<"\n";
   file_out << stude[i].stuID << "\n";
   file_out << sess[i].courseID << "\n";
   file_out << sess[i].startDate << "\n";
   file_out << sess[i].endDate << "\n";
   file_out << sess[i].totalCredits << "\n";
   // empty line in the end of each entry.
   file_out << endl;
 }
}


int main()
{
cout<<"Welcome to the CS 230 Data Structures class add program!"<<endl;
cout<<"Please follow the prompt to gain a report of your courses for the year: "<<endl<<endl;
//declare and initialize 4 students
student stu[4];
fillStudents(stu, 4);

//declare and initialize 10 courses
course courses[10];
fillCourses(courses, 10);

//assign 4 courses for each student
cout<<"Enter a class selection based on the chart above (1-10), please do not choose the same class twice: "<<endl;
cout<<"Each student goes by how their information was added"<<endl<<endl;

session sessions[4];
fillSession(stu, courses, sessions);

writeSessionsToFile(sessions, stu);

//display a report showing each student with his/her courses
//show the total number of credits for each student
cout<<"Please find your name and other important information below to see your classes and credit totals: \n\n"<<endl;
cout <<"=========="<<endl;
display_report(stu, courses, sessions);
cout <<"=========="<<endl;


// instead of system(pause), pause doesn't work on linux.
cin.get();
return 0;
}

我已经搜索了互联网以及这个网站,我找到了计算我正在搜索的单词行的函数,以及找到并强制用户输入另一个值的函数但我还没有找到一个我可以在我的代码中实现的,这将允许我继续我想到的目标

0 个答案:

没有答案