counter controlled loop to open 3 files

Hello, Ive been taking an intro to C++ class and have been doing well until now. I have a problem with a "counter controlled loops". I get the idea of them, however I dont not know how to incorporate a way to prompt the user to open 1 out of 3 files.

Here is the actual question:

"This program is to be executed on three separate sets of
external file input data. Each set resides in a unique file.
Use a counter controlled for() loop to take care of the three
iterations. Within the loop you’ll have to implement an
interactive path-filename input routine, in order to
open-trap-process-close these external files. DO NOT “hardwire”
the path-filename in your code"

I hardwired the path and file name into my code to get it open.
I dont understand what would be counted to open up file 1, 2, or 3.

thanks
1
2
3
4
5
6
for(int counter = 0; i < 3; ++i) {
    std::string filename;
    std::cout << "Enter name of the file to open: ";
    std::cin >> filename;
    //There you should open file, process it and close.
}

DO NOT “hardwire” the path-filename in your code
[...]
I hardwired the path and file name into my code
*facepalm*
Last edited on
hey, what can I say, Im a nub in programming. I had to hardwire it first to just get the imput file to open.

I knew it was suppose to be like this, however I didnt know how to form it.

thanks
I got that working, but nothing prints out to the output file when I enter the filename (which is located in my projects folder). Im trying to get something like this. I hate to ask for help, but im really stuck on this one.

Student GPA Special Note

1001 2.625
1002 3.800 Honors
1003 1.050 Warning
1004 3.500 Honors

Course 1 average: 2.675
Course 2 average: 2.550
Course 3 average: 2.925
Course 4 average: 2.825

____________________________________________________________

using namespace std;

int main()
{
double gpa;
double grade[4];// grades for student each student
double cgrade[10]; // totals course grades (down)
double avegpa[10]; // total course grade divided by number of students
int idnumber[10]; //id number
int numstd; // number of student
ifstream inputFile;
string filename;





int i=0;
for(int counter = 0; i < 3; ++i)
{
std::string filename;
std::cout << "Enter name of the file to open: ";
std::cin >> filename;

}

inputFile.open(filename.c_str());

ofstream outfile;
outfile.open("C:\\Users\\joey zasa\\Documents\\Outputfile1.txt",ios::out);

if ( outfile.fail() )
{
cerr << "\a\aOutput file could not be opened" << endl;
exit(1);

}


if (inputFile)
{
outfile<<"\t\tStudent"<<setw(15)<<"GPA"<<setw(25)<<"Special Note\n"<<endl;

int t=0;
while(inputFile >> idnumber[t] >>grade[t])
{
int t=0;
t++;

gpa = (grade[t])/4;

if (gpa >= 3.5)
outfile << "\t\t"<< idnumber << setw(15)<< gpa << setw(25)<< "Honors" << endl;

if (gpa <= 1.5)
outfile << "\t\t"<< idnumber <<setw(15)<< gpa << setw(25)<< "Warning" << endl;


if (gpa <= 3.5)
outfile <<"\t\t"<< idnumber <<setw(15)<< gpa << setw(25) <<endl;
}

int j=0;
int n=0;
numstd = 0;
numstd ++;
avegpa[n]= cgrade[j]/numstd;


outfile << setw(20) << "Course Average:\n";
outfile << setw(20) << avegpa[n] << endl;



outfile.close();


}
return 0;

}





Last edited on
idnumber is an int, grade is a double. When you run into "Honors" string, you trying to enter string into either iny or double. Input stream error flag is set and everything went downstairs. Probably you should skip until the end of the line after reading id and grade to skip any notes you might encounter.
Topic archived. No new replies allowed.