Not compiling all data - c++

Unsure what I am doing wrong, I am only getting the value for the first while statement.
The value is calculated correctly, so I do not know what I did wrong.
Maybe someone can see something I cannot. I know you do not have the file it is reading from, but there should not be a need for this.
Any help is appreciated. I always seem to have issues with while loops.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string inputFileName;
string name, gender, college;
double score = 0, totalCC = 0, totalUN = 0, totalFemalesScore = 0, totalMalesScore = 0;
int ccCount = 0, unCount = 0, fCount = 0, mCount = 0;
ifstream inputFile;
cout << "Input file name: ";
getline(cin, inputFileName);
inputFile.open(inputFileName);
if (!inputFile.is_open())
{
cout << "Unable to open input file." << endl;
exit(1);
}
while (inputFile.peek() != EOF)
{
inputFile >> name >> gender >> college >> score;
cout << left << setw(17) << name << setw(4) << gender << setw(4) << college << setw(4) << score << endl;
}
cout << "\nEnd of file reached\n" << endl;
inputFile.clear();
inputFile.seekg(0);
while (inputFile >> name)
{
inputFile >> gender >> college >> score;
if (gender == "F")
{
totalFemalesScore += score;
fCount++;
}
}
while (inputFile >> name)
{
inputFile >> gender >> college >> score;
if (gender == "M")
{
totalMalesScore += score;
mCount++;
}
}
while (inputFile >> name)
{
inputFile >> gender >> college >> score;
if (college == "CC")
{
totalCC += score;
ccCount++;
}
}
while (inputFile >> name)
{
inputFile >> gender >> college >> score;
if (college == "UN")
{
totalUN += score;
unCount++;
}
}
cout << "\nEnd of file reached\n" << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Average for females = " << totalFemalesScore / fCount << endl;
cout << "Average for males = " << totalMalesScore / mCount << endl;
cout << "Average of CC students = " << totalCC / ccCount << endl;
cout << "Average of UN students = " << totalUN / unCount << endl;
inputFile.close();
system("pause");
return 0;
}

Since you are accessing the values in these lines of code:
while (inputFile.peek() != EOF)
{
inputFile >> name >> gender >> college >> score;
cout << left << setw(17) << name << setw(4) << gender << setw(4) << college << setw(4) << score << endl;
}
Why not do the computation inside that loop:
while (inputFile.peek() != EOF)
{
inputFile >> name >> gender >> college >> score;
if (gender == "F")
{
totalFemalesScore += score;
fCount++;
} else {
totalMalesScore += score;
mCount++;
}
if (college == "CC")
{
totalCC += score;
ccCount++;
} else if (college == "UN") {
totalUN += score;
unCount++;
}
cout << left << setw(17) << name << setw(4) << gender << setw(4) << college << setw(4) << score << endl;
}

Related

I keep getting 0x488144 and another one before my output

Picture of error when input is jst L
#include <iostream>
using namespace std;
int main()
{
string student_name;
cout << "Enter student name\n";
cin >> student_name;
string student_surname;
cout << "Enter Student surname\n";
cin >> student_surname;
string student_id;
cout << "Enter Student ID\n";
cin >> student_id;
string student_group;
cout << "Enter Student Group\n";
cin >> student_group;
string Module_code;
cout << "Enter Module Code\n";
cin >> Module_code;
float test1;
cout << "Enter Test 1 mark\n";
cin >> test1;
float test2;
cout << "Enter Test 2 mark\n";
cin >> test2;
float final_mark;
final_mark = (test1 + test2) / 2;
cin >> final_mark;
string grade;
if (final_mark >= 80) {
grade = "A";
cin >> grade;
}
else if (final_mark >= 70) {
cout << "B";
cin >> grade;
}
else if (final_mark >= 60) {
grade = "C";
cin >> grade;
}
else if (final_mark >= 50) {
grade = "D";
cin >> grade;
}
else if (final_mark >= 40) {
grade = "E";
cin >> grade;
}
else (final_mark <= 30);
{
grade = "F";
cin >> grade;
} /*final code*/ cout << "LOMKOWKING UNIVERSITY\n";
cout << "ESWATINI CAMPUS\n";
cout << "STUDENT RESULT\n";
cout << "STUDENT NAME :" << cout << student_surname << student_name << endl;
cout << "STUDENT ID :" << cout << student_id << endl;
cout << "STUDENT GROUP :" << cout << student_group << endl;
cout << "MODULE CODE :" << cout << Module_code << endl;
cout << "Test 1 :" << cout << test1;
cout << "Test 2" << cout << test2 << endl;
cout << "Final Mark :" << cout << final_mark;
cout << "Grade :" << cout << grade << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
string student_name;
cout<< "Enter student name\n";
cin>>student_name;
string student_surname;
cout<<"Enter Student surname\n";
cin>>student_surname;
string student_id;
cout<<"Enter Student ID\n";
cin>>student_id;
string student_group;
cout<<"Enter Student Group\n";
cin>>student_group;
string Module_code;
cout<<"Enter Module Code\n";
cin>>Module_code;
float test1;
cout<<"Enter Test 1 mark\n";
cin>>test1;
float test2;
cout<<"Enter Test 2 mark\n";
cin>>test2;
float avg;
avg = (test1 + test2) / 2;
string grade;
if(avg<=40){
grade = "F";
}
else if(avg<=50 && avg>40){
grade = "E";
}
else if(avg<=60 && avg>50){
grade = "D";
}
else if(avg<=70 && avg>60){
grade = "C";
}
else if(avg<80 && avg>70){
grade = "B";
}
else if(avg>=80){
grade = "A";
}
/*final code*/
cout<< "LOMKOWKING UNIVERSITY\n";
cout<<"ESWATINI CAMPUS\n";
cout<< "STUDENT RESULT\n";
cout<< "STUDENT NAME :" <<student_surname << " " <<student_name << endl;
cout<< "STUDENT ID :" <<student_id << endl;
cout<< "STUDENT GROUP :"<<student_group << endl;
cout<< "MODULE CODE :"<<Module_code << endl;
cout<< "Test 1 :" <<test1<<endl;
cout<< "Test 2: "<<test2 << endl;
cout<< "Final score :"<<avg << endl;
cout<< "Grade :"<<grade<<endl;
return 0;
}
please, do not input "L" as a value to float variables (i.e. test1 and test2)

How to delete record [C++ student management system]

I'm making a student management system. All is working well, except the Delete Student Info function. I'm totally new in studying the C++ language.
When I try to use the Delete function, it will result like this, like a loop:
And this is my delete code:
void student::deleted()
{
system("cls");
fstream file, file1;
int found = 0;
string snum;
cout<<"\t\t\t\t-------------------------------------------\t\t\t"<<endl;
cout<<"\t\t\t\t---------Delete Student Information--------\t\t\t"<<endl;
file.open("Records.txt", ios::in);
if (!file)
{
cout << "\n\t\t\tNo information is available.";
file.close();
}
else
{
cout <<"\nEnter Student Number you want to remove: ";
cin >> snum;
file1.open("Records1.txt", ios::app | ios::out);
file >> student_num >> name >> bday >> address >> gender >> degree >> year;
while (!file.eof())
{
if(snum != student_num)
{
file1 << " " << student_num << " " << name << " " << bday << " " << address << " " << gender << " " << degree << " " << year ;
} else
{
found==0;
cout <<"\n\t\t\tSuccessfully Deleted.";
}
}
file1 >> student_num >> name >> bday >>address >> gender >> degree >> year ;
if (found == 0)
{
cout <<"\n\t\t\tStudent Number not found.";
}
file1.close();
file.close();
remove("Records.txt");
rename("Records.txt", "NewRecords.txt");
}
All is working on my program, except this delete function. I hope you can enlighten me with knowledge I still not know.
Basically, your whole while loop is structured wrong. Try something more like this instead:
void student::deleted()
{
system("cls");
cout<<"\t\t\t\t-------------------------------------------\t\t\t"<<endl;
cout<<"\t\t\t\t---------Delete Student Information--------\t\t\t"<<endl;
ifstream inFile("Records.txt");
if (!inFile)
{
cout << "\n\t\t\tCan't open file.";
return;
}
ofstream outFile("NewRecords.txt");
if (!outFile)
{
cout << "\n\t\t\tCan't create new file.";
return;
}
cout << "\nEnter Student Number you want to remove: ";
string snum;
cin >> snum;
bool found = false;
while (inFile >> student_num >> name >> bday >> address >> gender >> degree >> year)
{
if (snum == student_num)
{
found = true;
}
else if (!(outFile << " " << student_num << " " << name << " " << bday << " " << address << " " << gender << " " << degree << " " << year)
{
cout << "\n\t\t\tCan't write to new file.";
outFile.close();
remove("NewRecords.txt");
return;
}
}
if (!inFile.eof())
{
cout << "\n\t\t\tCan't read information.";
outFile.close();
remove("NewRecords.txt");
return;
}
outFile.close();
inFile.close();
if (!found)
{
cout <<"\n\t\t\tStudent Number not found.";
remove("NewRecords.txt");
return;
}
if (rename("Records.txt", "OldRecords.txt") != 0)
{
cout <<"\n\t\t\tCan't backup old file.";
remove("NewRecords.txt");
return;
}
if (rename("NewRecords.txt", "Records.txt") != 0)
{
cout <<"\n\t\t\tCan't rename new file.";
rename("OldRecords.txt", "Records.txt");
return;
}
remove("OldRecords.txt");
cout <<"\n\t\t\tSuccessfully Deleted.";
}

vector compiler error for different variable type

The goal of this assignment for my class is to ask the user for how many students are in the class.
Using the Vector library create a vector of Strings to hold the students names.
Create a vector of type double to hold a students grade average.
I have a couple two compiler errors that are preventing my code from running. Line 73 & 83. The following errors I am given:
main.cpp: In function ‘void add_student()’: main.cpp:73:11: error: request for member ‘push_back’ in ‘grade’, which is of non-class type ‘double’
73 | grade.push_back(grade);
| ^~~~~~~~~
main.cpp: In function ‘void remove_student()’: main.cpp:83:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::__cxx11::basic_string<char> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
83 | for (int i = 0; i < students.size(); i++)
| ~~^~~~~~~~~~~~~~~~~**
Any help would be much appreciated. Below is my entire code
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
vector<string> students;
vector<double> grade;
void add_student();
void remove_student();
void menu();
void print_summary();
int main()
{
int numStudent;
char menuSelection;
cout << "Welcome to the Student roaster!" << endl;
cout << "How many students are in your class?:" << endl;
cin >> numStudent;
for (int i = 0; i < numStudent; i++)
{
add_student();
}
cout << "Thank you for entering class information!" << endl;
//calls menu for the user
menu();
while (1)
{
cout << "selection:" << endl;
cin >> menuSelection;
if (menuSelection == 'a')
{
add_student();
}
else if (menuSelection == 'r')
{
remove_student();
}
else if (menuSelection == 'p')
{
print_summary();
}
else if (menuSelection == 'm')
{
menu();
}
else if (menuSelection == 'q')
break;
else
cout << "Not a valid selection" << endl;
}
return 0;
}
void add_student()
{
string firstName, lastName;
double grade;
//ask for student info
cout << "Please enter student (Fisrt Last Grade) info: " << endl;
cin >> firstName >> lastName >> grade;
firstName += " ";
firstName += lastName;
//inserts new student
students.push_back(firstName);
grade.push_back(grade);
}
void remove_student()
{
string first, last;
cout << "Enter the student (First Last) to remove :\n";
cin >> first >> last;
first += " ";
first += last;
int loc = 0;
for (int i = 0; i < students.size(); i++)
{
if (students[i] == first)
{ // finding the location to erase
loc = i;
break;
}
}
students.erase(students.begin() + loc); //removing using erase function
grade.erase(grade.begin() + loc);
}
void menu()
{
cout << "Please choose one of the following options:\n";
cout << "a: add a student\n";
cout << "r: remove a student\n";
cout << "p: print the class summary\n";
cout << "m: print menu\n";
cout << "q: quit program\n";
}
void print_summary()
{
cout << "class summary" << endl;
cout << "--------------------------------" << endl;
cout << "Name" << setw(20) << "Grade" << endl;
cout << "-------" << setw(20) << "--------" << endl;
int n = students.size();
double total = 0;
//cycles through each student
for (int i = 0; i < n; i++)
{
int temp = students[i].size();
int loc = 20 - temp;
cout << students[i] << setw(loc) << grade[i] << endl;
total += grade[i];
}
cout << "Number of students: " << endl;
cout << "------------------" <<endl;
cout << n << " " << endl;
total = (total) / n;
cout << "Average Grade: " << endl;
cout << "--------------" << endl;
//limits the deciaml places to 2
cout << fixed << setprecision(2) << total << " " << endl;
}
The name of the local variable was the same as the name of the global variable.
So you should rename local variable.
62) double grade_;
66) cin >> firstName >> lastName >> grade_;
73) grade.push_back(grade_);

How to read from a specific line in a file (C++)

I need to find enter a number and find the specific line in my text file to correspond with the number, and I am having a little trouble, heres what I have so far:
cout << "Please enter the ID number of the student, to view their grades: ";
cin >> number;
ifstream myfile;
myfile.open("grades.txt");
if (myfile)
{
cout << " ID exam1 exam2 exam3" << endl;
cout << "---------------------------------" << endl;
getline(myfile, number);
myfile >> number >> exam1 >> exam2 >> exam3;
cout << setw(5) << number << setw(9) << exam1
<< setw(9) << exam2 << setw(9) << exam3 << endl;
cout << "---------------------------------" << endl;
total = exam1 + exam2 + exam3;
cout << "TOTAL: " << setw(25) << total << endl << endl;
}
myfile.close();
return 0;
}
Yes, you use a for loop to loop until you reach the line number, while incrementing.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
// Line #
int line;
// File
std::ifstream f("_");
// Text
std::string s;
// Prompt
std::cout << "Line #: " << std::endl;
// Store line #
std::cin >> line;
// Loop, while less than line
for (int i = 1; i <= line; i++)
std::getline(f, s);
// Output text at line
std::cout << s;
return 0;
}
This will helps : )
#include<iostream>
#include<vector>
#include<fstream>
#include<string.h>
#include<iomanip>
using namespace std;
int main()
{
int number;
cout << "Please enter the ID number of the student, to view their grades: ";
cin >> number;
ifstream myfile;
myfile.open("grades.txt");
if (myfile)
{
string string_obj;
string delimiter = " ";
int id,exam1,exam2,exam3;
size_t pos = 0;
cout << " ID exam1 exam2 exam3" << endl;
cout << "---------------------------------" << endl;
getline(myfile, string_obj);
//Split string into tokens
string token[4];
int i=0;
while ((pos = string_obj.find(delimiter)) != string::npos)
{
token[i++] = string_obj.substr(0, string_obj.find(delimiter));
string_obj.erase(0, pos + delimiter.length()); // move string for next iteration
}
token[i] = string_obj.substr(0, string_obj.find(delimiter));
id = stoi(token[0]);
if (id == number)
{
exam1 = stoi(token[1]); // convert string into int
exam2 = stoi(token[2]);
exam3 = stoi(token[3]);
}
cout << setw(5) << number << setw(9) << exam1
<< setw(9) << exam2 << setw(9) << exam3 << endl;
cout << "---------------------------------" << endl;
int total = exam1 + exam2 + exam3;
cout << "TOTAL: " << setw(25) << total << endl << endl;
}
myfile.close();
return 0;
}

C++ console crashes after running

guys, i have a problem.
After i run the program, the console just crashes.
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::replace: __pos (which is 4294967295) > this->size() (which is 9)
I mean, you will see.In the program there is an option "Add person", and when i add one, its all okay.But when i enter the program to add second person it crashes.Please, help!
The source code of the programs is:
#include <iostream>
#include <windows.h>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
int main(){
system("chcp 1251 > nul");
system("title PeopleData");
string firstName;
string midName;
string lastName;
string fullName;
string line;
long ID = 27560000;
int age;
char gender;
cout << "1.Add person" << endl;
cout << "2.Read file" << endl;
cout << "3.Exit" << endl << endl;
cout << "Please, enter your choice: ";
int choice;
cin >> choice;
if(choice == 1)
{
system("cls");
ofstream myfile("Data.txt", ios::app);
ifstream file("Data.txt");
string IDLine;
int numberOfLines = 0;
if (myfile.is_open())
{
while (getline (file, line) )
{
numberOfLines ++;
}
file.close();
ifstream file("Data.txt");
int y = 0;
line = "";
while(getline(file, line))
{
IDLine = line;
if(y == numberOfLines - 5)
{
goto NextStep;
}
y++;
}
}
else
{
cout << "Unable to open file";
}
NextStep:
string LastID = IDLine;
if(LastID != "")
{
LastID.replace(LastID.find("ID: "), string("ID: ").length(), "");
ID = atoi(LastID.c_str()) + 1;
}
else
{
ID = 27560000;
}
cout << "First Name: ";
cin >> firstName;
cout << endl << endl;
cout << "Middle Name: ";
cin >> midName;
cout << endl << endl;
cout << "Last Name: ";
cin >> lastName;
cout << endl << endl;
cout << "Age: ";
cin >> age;
cout << endl << endl;
cout << "Gender (m / f): ";
cin >> gender;
fullName = firstName + " " + midName + " " + lastName;
myfile << "First Name: " << firstName << "\n";
myfile << "Middle Name: " << midName << "\n";
myfile << "Last Name: " << lastName << "\n";
myfile << "Full Name: " << fullName << "\n";
myfile << "Age: " << age << "\n";
myfile << "Gender: " << gender << "\n";
myfile << "ID: " << ID << "\n";
myfile << "\n---------------\n\n";
myfile.close();
file.close();
return 0;
}
if(choice == 2)
{
system("cls");
ifstream myfile ("Data.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else
{
setcolor(12);
cout << "Unable to open file";
}
}
if(choice == 3)
{
return 0;
}
system("pause > nul");
return 0;
}
`