Need help with code pleasee

I am working on my machine problem for my intro to programming. I can't figure out why my code will not read the Total applicants, for both programs, at the very end. We are required to run this program 14 times with 14 different applicants and different scores by the teacher. If you have any comments corrections or anything to help me finish I would greatly appreciate it!






#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <iomanip>
using namespace std;

////////////////////////////////////////////////////////////// //Here I indenitfied the different variables I will be using in specificity of what it is I want each of them to do or be interpreted as by the compiler

int appNum = 0;
int totalApplicants, totalMusicAccept, totalLibaccept;
int LibaccptNum = 0;
int MusicaccptNum = 0;
string Accept, txt, scores;
char EOL = '\n';
string School, Music, la, alumnus, reason;
string s1, s2, s3;
ifstream inputfile;
float GPA, math, verb;
bool almn, BoolAccpt;
string strAccpt, printSchool;


void getAppData(string &School, float &GPA, float &math, float &verb, string &alumnus) {

/////////////////////////////////////////////////////////////////////////////////// //Here I display my questions so I or the user can easily follow along.

cout << "Enter school" << endl; //L || M
cin >> School;
cout << "Enter GPA" << endl; //enter GPA
cin >> GPA;
cout << "Enter Math SAT score" << endl; //000
cin >> math;
cout << "Enter Verbal SAT score" << endl; //enter Verbal SAT score
cin >> verb;
cout << "Enter Alumnus status" << endl; //Alumnus - Y || N
cin >> alumnus;
cout << "*******************************" << endl;

///////////////////////////////////////////////////////////////////////////////////

(getline(inputfile, txt));
istringstream iss(txt);

while (iss >> School >> s1 >> s2 >> s3 >> alumnus)
{
try {
GPA = stof(s1);
math = stof(s2);
verb = stof(s3);
}
catch (const exception&)
{
continue;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
cout << "Acceptance to College by Michael Matteis" << '\n' << '\n';
// Here I use a while statement followed by multiple if statements to ensure that each response is returned and either read as true or false in regards to this assignments specific required catagories for each school and applicant

inputfile.open("mp3accept.txt");

while (!inputfile.eof())
{
appNum++;
getAppData(School, GPA, math, verb, alumnus);


if ((School == "L") && (LibaccptNum >= 5)) { BoolAccpt = false; reason = " Admissions full, Maximum applicants admitted"; }
else if ((School == "L") && (alumnus == "Y") && (math + verb < 1000)) { BoolAccpt = false; reason = " - SAT scores are too low"; }
else if ((School == "L") && (alumnus == "N") && (math + verb < 1200)){BoolAccpt = false; reason = " - SAT scores are too low"; }
else if ((School == "L") && (alumnus == "N") && (GPA < 3.5)) { BoolAccpt = false; reason = " - GPA is too low"; }
else if ((School == "L") && (alumnus == "Y") && (GPA < 3.0)) { BoolAccpt = false; reason = " - GPA is too low"; }
else if ((School == "L") && (BoolAccpt != true)) strAccpt = " Rejected ";

if ((School == "L") && (alumnus == "Y") && (GPA >= 3.0) && ((math + verb) >= 1000) && (LibaccptNum < 5))
{
BoolAccpt = true; strAccpt = "Congratulations you have been accepted into Liberal Arts! ";
LibaccptNum++;
}
if ((School == "L") && (alumnus == "N") && (GPA >= 3.5) && ((math + verb) >= 1200) && (LibaccptNum < 5))
{
BoolAccpt = true; strAccpt = " Congratulations you have been accepted into Liberal Arts! ";
LibaccptNum++;
}


if ((School == "M") && (MusicaccptNum < 3) && (math >= 500 && verb >= 500))
{
BoolAccpt = true; strAccpt = " Congratulations you have been accepted into Music!";
MusicaccptNum++;
}


if ((School == "M") && ((math < 500) || (verb < 500) || (MusicaccptNum >= 3) ))
{
BoolAccpt = false; strAccpt = " Rejected ";
if ((School == "M") && (BoolAccpt != true) && (math < 500) || (verb < 500)) reason = " SAT scores are too low ";
if ((School == "M") && (BoolAccpt != true) && MusicaccptNum >= 3) reason = " Unfortunately admissions are full at this time";
}

if (School == "L") printSchool = "***Applying to Liberal Arts";
if (School == "M") printSchool = "***Applying to School of Music";

//////////////////////////////////////////////////////////////////////////////////////////////////
// the following statements will read/output the data that was inputed by I or the user after it has ran through the if statements to provide the correct format expected of this assignment

cout << "Applicant #: " << appNum << '\n';
cout << "School = " << School << ' ';
cout << fixed << setprecision(1);
cout << "GPA = " << GPA << setprecision(0);
cout << " Math = "<< math << " Verbal = " << verb << " Alumnus = " << alumnus << '\n';
cout << printSchool << '\n'
<< strAccpt << reason << '\n' << "*******************************" << '\n' << endl;

reason.clear();
strAccpt.clear();
}

///////////////////////////////////////////////////////////////////////////////////////
//Below will read/output the final totals including; applicants, those accepted into Music or Lberal Arts.

cout << '\n' << '\n' << "Total Applicants: " << appNum << '\n';
cout << "Total accepted into music: " << MusicaccptNum << '\n';
cout << "Total accepted into Liberal Arts: " << LibaccptNum << '\n' << '\n';

return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////



Hello Noobie26,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I am as far as I can get testing your program with out the "mp3accept.txt" file. I have nothing to read. Could you post the "mp3accept.txt" file or at lest a short sample so I can test the program.

Question: Why all the global variables? Also you have left most of your variables uninitialized. Do not count on these global variable being default initialized. It is always a good practice to initialize all variables when defined. From C++11 you can use {} after the variable name for initialization.

Not wise to use a while condition tied to "eof". This does not work the way you might think. When I ran the program the file did not open so the "eof" state bit will never be set and the while loop becomes an endless loop because nothing can be read from the input file and "eof" will never be set. And the other way the while loop will try to process a read when it does reach "eof". This usually produces a duplicate at the end whatever the information goes to.

I find this code to be useful when dealing with files, at least for learning. Once you understand it you can shorten the code to use less lines.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string iFileName{ "" };  // <--- Put file name here.

std::ifstream inFile;

inFile.open(iFileName);

if (inFile.is_open())
{
	std::cout << "\n File " << iFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
}
else
{
	std::cout << "\n File " << iFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
	exit(1);  // <--- Because there is no reason to continue.
}


Since the file never opened calling "getAppData" does not work properly.

Still have some checking to do.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 mp3accept.txt

L 4.0 600 650 N
M 3.9 610 520 N
L 3.8 590 600 N
L 3.0 600 600 Y
L 3.4 600 600 N
L 3.0 500 490 Y
L 2.9 500 500 Y
M 3.5 500 490 Y
M 3.9 490 600 Y
L 3.5 700 500 N
L 3.1 600 400 Y
L 3.0 490 510 Y
L 4.0 800 800 Y
M 3.2 500 500 N 
Hello Noobie26,

Thank you for the input file.

Now that everything works I found that when calling the "getAppData" function you ask the user for input then read the file and over wright the data that was just enter.

When I realized what is happening I started to question what the "mp3accept.txt" file is for and how you intend for it to be used. Is it information about people enrolled in the schools or information about potential applicants? Should the file be read first and evaluated before new data is entered?

I feel as though the program flow is in the wrong order.

Right now the program works, but I feel that it is not the way that you want.

I made a few slight changes, nothing spectacular. This is the only big change that I had to make:

 
	std::getline(inputfile, txt);  // <--- Removed the outer (). 


Had to a "std::" for the way I write a program.

Hope that helps,

Andy
Last edited on
Registered users can post here. Sign in or register to post.