Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am getting an error on the creation of the varible ss and the << >> operators.

ID: 3765223 • Letter: I

Question

I am getting an error on the creation of the varible ss and the << >> operators. Bolded where I am getting the errors. I am just having trouble making the username part of my program. It should be initials and the id number combines together. I'm not sure why this didn't work.

Example:

LastName FirstName ID userName Test 1 Test 2 Test 3 Test 4 Test 5 Average Grade

Bonebrake Nicole 1111 bn1111 90 85 50 78 85 77.60 C

  

Student* readIn(int total, string username)
{
   ifstream dataIn;
   dataIn.open("D://ravi/Cheg//grade.txt");
   Student* SL;
   SL = new Student[total];
   for (int i = 0; i < total; i++)
   {
       dataIn >> SL[i].lname;
       dataIn >> SL[i].fname;
       dataIn >> SL[i].Id;
       dataIn >> SL[i].testScore[0]
           >> SL[i].testScore[1]
           >> SL[i].testScore[2]
           >> SL[i].testScore[3]
           >> SL[i].testScore[4];

       SL[i].fullName = SL[i].lname + ", " + SL[i].fname;
       {
           char ch1 = tolower(SL[i].fname.at(0));
           char ch2 = tolower(SL[i].lname.at(0));
           string s3 = SL[i].Id;
           stringstream ss;
           ss << ch1;
           ss << ch2;
           ss >> username;
           username += s3; //(ch1 << ch2 << s3[3]);
           SL[i].usernameL = username;
       }
   }
   return SL;
}

Full Program

#include <fstream>
#include <iomanip>
#include <iostream>
#include <cctype>
#include <string>
#include <string.h>

using namespace std;

struct Student
{
   string fname;
   string lname;
   string Id;
   string fullName;
   string usernameL;
   int testScore[5];
   float average;
   char grade;
};

void findAverage(Student*, int);
void findGrade(Student*, int);
void nameSort(Student*, int);
void consolePrint(Student*, int, string);
int search(Student*, int, string);

Student* readIn(int, string);


int main()
{
   Student* SL;
   string username;
   int total = 8;
   SL = readIn(total, username);
   findAverage(SL, total);
   findGrade(SL, total);
   consolePrint(SL, total, username);
   nameSort(SL, total);
   search(SL, total, username);
   return 0;

}

Student* readIn(int total, string username)
{
   ifstream dataIn;
   dataIn.open("D://ravi/Cheg//grade.txt");
   Student* SL;
   SL = new Student[total];
   for (int i = 0; i < total; i++)
   {
       dataIn >> SL[i].lname;
       dataIn >> SL[i].fname;
       dataIn >> SL[i].Id;
       dataIn >> SL[i].testScore[0]
           >> SL[i].testScore[1]
           >> SL[i].testScore[2]
           >> SL[i].testScore[3]
           >> SL[i].testScore[4];

       SL[i].fullName = SL[i].lname + ", " + SL[i].fname;
       {
           char ch1 = tolower(SL[i].fname.at(0));
           char ch2 = tolower(SL[i].lname.at(0));
           string s3 = SL[i].Id;
           stringstream ss;
           ss << ch1;
           ss << ch2;
           ss >> username;
           username += s3; //(ch1 << ch2 << s3[3]);
           SL[i].usernameL = username;
       }
   }
   return SL;
}

void findAverage(Student *SL, int size)
{
   for (int i = 0; i < size; i++)
   {
       float sum = 0;
       for (int t = 0; t < 5; t++)
       {
           if (SL[i].testScore[t] >= 0 && SL[i].testScore[t] <= 100)
           {
               sum = sum + SL[i].testScore[t];
           }
           else
           {
               sum = 0;
               break;
           }
       }
       if (sum == 0)
       {
           SL[i].average = 0;
       }
       else
           SL[i].average = (sum / 5);
   }
}

void findGrade(Student *SL, int size)
{

   for (int i = 0; i < size; i++)
   {
       if (SL[i].average == 0)
           SL[i].grade = 'I';
       else if (SL[i].average >= 90)
           SL[i].grade = 'A';
       else if (SL[i].average < 90 && SL[i].average >= 80)
           SL[i].grade = 'B';
       else if (SL[i].average < 80 && SL[i].average >= 70)
           SL[i].grade = 'C';
       else if (SL[i].average < 70 && SL[i].average >= 60)
           SL[i].grade = 'D';
       else
           SL[i].grade = 'F';
   }
}

void consolePrint(Student* SL, int size, string username)
{
   string categories[] = { "Full Name", "ID", "UN", "Test 1", "Test 2", "Test 3", "Test 4", "Test 5", "Average", "Grade" };
   cout << endl;
   cout << "Student Grade Report" << endl << endl;
   cout << "________________________________________________________________________________________" << endl;
   for (int x = 0; x < 10; x++)
       cout << categories[x] << " ";
   cout << endl;
   cout << "________________________________________________________________________________________" << endl;
   for (int i = 0; i < size; i++)
   {
       cout << SL[i].fullName << " " << SL[i].Id << " " << SL[i].usernameL << " " << SL[i].testScore[0] << " " << SL[i].testScore[1] << " " <<
           SL[i].testScore[2] << " " << SL[i].testScore[3] << " " << SL[i].testScore[4] << " " <<
           SL[i].average << " " << SL[i].grade << endl;
   }
   cout << "________________________________________________________________________________________" << endl;

   float totalAverage = (SL[0].average + SL[1].average + SL[2].average + SL[3].average + SL[4].average) / 5;
   cout << "The class average for CS 5 tests is " << totalAverage << endl;
   cout << "________________________________________________________________________________________" << endl;
   int counta = 0;
   int countb = 0;
   int countc = 0;
   int countd = 0;
   int countf = 0;
   for (int i = 0; i < size; i++)
   {
       if (SL[i].average >= 90)
           counta++;
       if (SL[i].average < 90 && SL[i].average >= 80)
           countb++;
       if (SL[i].average < 80 && SL[i].average >= 70)
           countc++;
       if (SL[i].average < 70 && SL[i].average >= 60)
           countd++;
       if (SL[i].average < 60 && SL[i].average >= 0)
           countf++;
   }
   cout << "The total number of students with grade A is " << counta << endl;
   cout << "The total number of students with grade B is " << countb << endl;
   cout << "The total number of students with grade C is " << countc << endl;
   cout << "The total number of students with grade D is " << countd << endl;
   cout << "The total number of students with grade F is " << countf << endl;

   cout << "________________________________________________________________________________________" << endl;
}

void nameSort(Student *SL, int elem)
{
   char choice;
   cout << "Please input sorting choices: A: ascending and D: decending: ";
   cin >> choice;
   int seek;
   int minCount;
   int tempScore = elem + 1;
   int minValue;
   std::string minValue2, minValue3;
   if (choice == 'A' || choice == 'a')
   {
       for (seek = 0; seek < (elem - 1); seek++)
       {
           minCount = seek;
           minValue = SL[seek].grade;
           minValue2 = SL[seek].fullName;
           minValue3 = SL[seek].Id;
           for (int index = seek + 1; index < elem; index++)
           {
               if (SL[index].grade < minValue)
               {
                   minValue = SL[index].grade;
                   minValue2 = SL[index].fullName;
                   minValue3 = SL[index].Id;
                   minCount = index;
               }
           }
           SL[minCount].fullName = SL[seek].fullName;
           SL[minCount].Id = SL[seek].Id;
           SL[minCount].grade = SL[seek].grade;
           SL[seek].fullName = minValue2;
           SL[seek].Id = minValue3;
           SL[seek].grade = minValue;
           cout << SL[seek].fullName << " " << SL[seek].Id << " " << SL[seek].grade << endl;
       }
       cout << SL[seek].fullName << " " << SL[seek].Id << " " << SL[seek].grade << endl;
   }
   else if (choice == 'D' || choice == 'd')
   {
       for (seek = 0; seek < (elem - 1); seek++)
       {
           minCount = seek;
           minValue = SL[seek].grade;
           minValue2 = SL[seek].fullName;
           minValue3 = SL[seek].Id;
           for (int index = seek + 1; index < elem; index++)
           {
               if (SL[index].grade > minValue)
               {
                   minValue = SL[index].grade;
                   minValue2 = SL[index].fullName;
                   minValue3 = SL[index].Id;
                   minCount = index;
               }
           }
           SL[minCount].fullName = SL[seek].fullName;
           SL[minCount].Id = SL[seek].Id;
           SL[minCount].grade = SL[seek].grade;
           SL[seek].fullName = minValue2;
           SL[seek].Id = minValue3;
           SL[seek].grade = minValue;
           cout << SL[seek].fullName << " " << SL[seek].Id << " " << SL[seek].grade << endl;
       }
       cout << SL[seek].fullName << " " << SL[seek].Id << " " << SL[seek].grade << endl;
   }
}

int search(Student *SL, int elems, string username)
{
   cout << "________________________________________________________________________________________" << endl;
   cout << "Please input the student user name that you want to search (-1 to exit)" << endl;
   cin >> username;
   int index = 0;
   int position = -1;
   bool found = false;

       while (index < elems && !found)
       {
       //   if (SL[index] == username)
       //   {
       //       found = true;
       //       position = index;
       //   }
           cout << "Student with user name " << username << " is " << SL[index].fullName << " who has a grade of '" << SL[index].grade << "'." << endl;
           index++;
           return position;
       }
       return position;
}

Explanation / Answer

use std::stringstream ss;

instead of stringstream ss;

dataIn >> SL[i].lname;
       dataIn >> SL[i].fname;
       dataIn >> SL[i].Id;
       dataIn >> SL[i].testScore[0]
dataIn >> SL[i].testScore[1]
dataIn >> SL[i].testScore[2]
dataIn >> SL[i].testScore[3]
dataIn >> SL[i].testScore[4];