Question: #include #include #include using namespace std; string mergeOneLine(int id,string last_name, string first_name, string phone_number, float quizzes[14], float homeworks[10], float labs[7], float tests[3], float project, float
#include
#include
#include
using namespace std;
string mergeOneLine(int id,string last_name, string first_name, string phone_number, float quizzes[14], float homeworks[10], float labs[7], float tests[3], float project, float discussion, float teamWork, float extra, float sumScore = 0.0, float percentage = 0.0, char letterGrade = 'X')
{
//Merges all assignments and information into one string and returns it
string oneLine;
oneLine += to_string(id) + "," + last_name + "," + first_name + "," + phone_number + "-";
for (int i = 0; i < 14; i++)
oneLine += (to_string(quizzes[i]) + ",");
oneLine += "-";
for (int i = 0; i < 10; i++)
oneLine += (to_string(homeworks[i]) + ",");
oneLine += "-";
for (int i = 0; i < 7; i++)
oneLine += (to_string(labs[i]) + ",");
oneLine += "-";
for (int i = 0; i < 3; i++)
oneLine += (to_string(tests[i]) + ",");
oneLine += to_string(project) + "-" + to_string(discussion) + "-" + to_string(teamWork) + "-" + to_string(extra) + "-" + to_string(sumScore) + "-" + to_string(percentage) + "-" + to_string(letterGrade);
return oneLine;
}
void splitOneLine(string line, int& id, string& last, string& first, string& phone, float quizzes[], float homeworks[], float labs[], float tests[], float& project, float& discussion, float& teamWork, float& extra, float& sumScore, float& percentage, char& letterGrade)
{
string temp;
int dash, comma;
comma = line.find(",");
id = stoi(line.substr(0, comma));
line = line.substr(comma + 1);
comma = line.find(",");
last = line.substr(0, comma);
line = line.substr(comma + 1);
comma = line.find(",");
first = line.substr(0, comma);
line = line.substr(comma + 1);
dash = line.find("-");
phone = line.substr(0, dash);
line = line.substr(dash + 1);
dash = line.find("-");
temp = line.substr(0, dash);
for (int i = 0; i < QUIZ_SIZE; i++)
{
comma = temp.find(",");
quizzes[i] = (float)stod(temp.substr(0, comma));
temp = temp.substr(comma + 1);
}
line = line.substr(dash + 1);
dash = line.find("-");
temp = line.substr(0, dash);
for (int i = 0; i < HOMEWORK_SIZE; i++)
{
comma = temp.find(",");
homeworks[i] = (float)stod(temp.substr(0, comma));
temp = temp.substr(comma + 1);
}
line = line.substr(dash + 1);
dash = line.find("-");
temp = line.substr(0, dash);
for (int i = 0; i < LAB_SIZE; i++)
{
comma = temp.find(",");
labs[i] = (float)stod(temp.substr(0, comma));
temp = temp.substr(comma + 1);
}
line = line.substr(dash + 1);
dash = line.find("-");
temp = line.substr(0, dash);
for (int i = 0; i < TEST_SIZE; i++)
{
comma = temp.find(",");
tests[i] = (float)stod(temp.substr(0, comma));
temp = temp.substr(comma + 1);
}
line = line.substr(dash + 1);
dash = line.find("-");
project = (float)stod(line.substr(0, dash));//project
cout << "project: " << project << endl;
line = line.substr(dash + 1);
dash = line.find("-");
discussion = (float)stod(line.substr(0, dash));//discussion
cout << "discussion: " << discussion << endl;
line = line.substr(dash + 1);
dash = line.find("-");
teamWork = (float)stod(line.substr(0, dash));//team work
cout << "team work: " << teamWork << endl;
line = line.substr(dash + 1);
dash = line.find("-");
extra = (float)stod(line.substr(0, dash));//extra
cout << "extra: " << extra << endl;
line = line.substr(dash + 1);
dash = line.find("-");
sumScore = (float)stod(line.substr(0, dash)); //sumScore
line = line.substr(dash + 1);
dash = line.find("-");
percentage = (float)stod(line.substr(0, dash)); //average
letterGrade = line.substr(dash + 1)[0];
//Function to perform task2
string first_name, last_name, phone_number, oneLine;
float quizzes[14] = {}, homeworks[10] = {}, labs[7] = {}, tests[3] = {}, project = 0, discussion = 0, teamWork = 0, extra = 0, sumScore = 0, percentage = 0;//Arrays and variables to store scores
ifstream f("student.txt");//File to read from
ofstream o("tempFile.txt");
char choice, letterGrade = 'X';
int option, id;
do
{
f >> id;
getline(f, last_name, ',');
getline(f, first_name, ',');
getline(f, phone_number, ' ');
cout << "You want to enter the score of student with id: " << id << " (Y/N)";
cin >> choice;
cin.ignore();
if (choice == 'y' || choice == 'Y')
{
do
{
cout << "1. Quizzes" << endl;
cout << "2. Homeworks" << endl;
cout << "3. Labs" << endl;
cout << "4. Tests" << endl;
cout << "5. Projects" << endl;
cout << "6. Discussion" << endl;
cout << "7. Team Work" << endl;
cout << "8. Extra credit" << endl;
cout << "0. exit" << endl;
cout << "Enter your choice: " << endl;
cin >> option;
switch (option)
{
case 1: cout << "Enter the scores of quizzes seperate by space: ";
for (int i = 0; i < 14; i++)
{
cin >> quizzes[i];
}
break;
case 2: cout << "Enter the scores of Homeworks seperate by space: ";
for (int i = 0; i < 10; i++)
{
cin >> homeworks[i];
}
break;
case 3: cout << "Enter the scores of labs seperate by space: ";
for (int i = 0; i < 7; i++)
{
cin >> labs[i];
}
break;
case 4: cout << "Enter the scores of tests seperate by space: ";
for (int i = 0; i < 3; i++)
{
cin >> tests[i];
}
break;
case 5: cout << "Enter the score of project: ";
cin >> project;
break;
case 6: cout << "Enter the score of discussion: ";
cin >> discussion;
break;
case 7: cout << "Enter the score of teamwork: ";
cin >> teamWork;
break;
case 8: cout << "Enter the score of extra credit: ";
cin >> extra;
break;
case 0: break;
default: cout << "Wrong input";
}
} while (option != 0);
oneLine = mergeOneLine(id, last_name, first_name, phone_number, quizzes, homeworks, labs, tests, project, discussion, teamWork, extra, sumScore, percentage, letterGrade);
o << oneLine << endl;
}
} while (choice == 'Y' || choice == 'y' || !f.eof());
f.close();
o.close();
}
int main()
{
char more;
string id, first_name, last_name, phone_number, text;
fstream file;
file.open("student.txt", ios::out);
do
{
cout << "Enter data of student." << endl;
cout << "Enter student id : ";
getline(cin, id);
cout << "Enter student last_name : ";
getline(cin, last_name);
cout << "Enter student first_name : ";
getline(cin, first_name);
cout << "Enter student phone number : ";
getline(cin, phone_number);
file << id << "," << last_name << "," << first_name << "," << phone_number << endl;
cout << "do you want to enter more ?(y/n) : ";
cin >> more;
cin.ignore();
} while (more == 'y');
cout << " Student Data : ";
string filename = "student.txt";
file.close();
file.open(filename.c_str(), ios::in);
while (file >> text)
cout << text << endl;
file.close();
//Calling task2 function
return 0;
string mergeOneLine(int id, string last, string first, string phone, float quizzes[], float homeworks[], float labs[], float tests[], float project, float discussion, float teamWork, float extra, float sumScore, float percentage, char letterGrade)
{
stringstream stream;
stream << fixed << setprecision(2) << to_string(id) + "," + last + "," + first + "," + phone + "-";
//quizzes
for (int i = 0; i < QUIZ_SIZE - 1; i++)
{
stream << fixed << setprecision(2) << quizzes[i] << ",";
}
stream << quizzes[QUIZ_SIZE - 1] << "-";
//homework
for (int i = 0; i < HOMEWORK_SIZE - 1; i++)
{
stream << homeworks[i] << ",";
}
stream << homeworks[HOMEWORK_SIZE - 1] << "-";
//labs
for (int i = 0; i < LAB_SIZE - 1; i++)
{
stream << labs[i] << ",";
}
stream << labs[LAB_SIZE - 1] << "-";
//tests
for (int i = 0; i < TEST_SIZE - 1; i++)
{
stream << tests[i] << ",";
}
stream << tests[TEST_SIZE - 1] << "-";
//project
stream << project << "-";
//discussion
stream << discussion << "-";
//team work
stream << teamWork << "-";
//extra
stream << extra << "-";
//sumScore
stream << sumScore << "-";
//percentage
stream << percentage << "-";
//letterGrade
stream << letterGrade;
return stream.str();
}
Need help with code errors my program isnt running.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
