ifstream wont open

Unfinished code but cannot move forward until the ifstream is corrected


[code]
#include <iostream>
#include <fstream>


using namespace std;

int main()
{

int x;
char studentID, grade1, grade2, grade3, grade4;
int total = 0;
char again = 'Y';

ofstream file;
file.open("grade.txt");

while ( again == 'Y' || again == 'y')
{
for (x = 0; x < 1; x++)
{
//enter grades
cout << "Enter student ID#:" << endl;
cin >> studentID;

cout << "Enter grade: " << endl;
cin >> grade1;

cout << "Enter grade: " << endl;
cin >> grade2;

cout << "Enter grade: " << endl;
cin >> grade3;

cout << "Enter grade: " << endl;
cin >> grade4;

cout << endl;
}


//file imput
file << studentID << " " << grade1 << " " << grade2 << " " << grade3 << " " << grade4 << endl;
}
//stop or continue?
cout << "Enter Y/N to enter another student: " << endl;
cin >> again;

file.close();


}//while loop ends



ifstream file ("grade.txt");

while (file >> studentID >> grade1 >> grade2 >> grade3 >> grade4)
{
cout << studentID << grade1 << grade2 << grade3 << grade4 << endl;
total += grade1 + grade2 + grade3 + grade4;

for (x = 0; x < total; x++)
{

}
cout << "Average is: " << total / 4 << endl;

}
return 0;
}

Last edited on
> ifstream wont open
code doesn't compile

> }//while loop ends
no, main ends.
This is a code that I used for ifstream in the past, maybe it comes in handy for you?

1
2
3
4
5
6
7
8
9
10
11
12
   string filename = "";
   cout << "Enter file name: ";
   cin >> filename;

   
   ifstream myfile;
   myfile.open(filename.c_str());
   if (myfile.fail())
   {
      cerr << "Could not open file: " << filename << endl;
      return;
   }
Just to give you an idea...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//My assumption is you will write the student ID
//with their four grades to a file.Then, you will
//read the student ID and their four grades and 
//determine the average.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cctype>

void writeFile(std::ofstream&, std::string, double, double[]);
void readFile(std::ifstream&, std::string, double, double[]);


int main()
{
    
    std::ofstream outFile;
    std::string studentID{ " " };
    const int totalNumOfGrades{ 4 };
    double grades[totalNumOfGrades] {0.0};

    writeFile(outFile, studentID, totalNumOfGrades, grades);

    std::ifstream inFile;

    readFile(inFile, studentID, totalNumOfGrades, grades);


    return 0;
}

void writeFile(std::ofstream &outputFile, std::string stuIdentification, double sizeofGrades, double arrayGrades[])
{
    outputFile.open("grade.txt", std::ios::out);

    bool addRecord{ true };
    while (addRecord)
    {
	   std::cout << "Enter the Student ID: ";
	   std::getline(std::cin, stuIdentification);
	   while (stuIdentification.length() > 8)
	   {
		  std::cout << "Sorry, but the student ID cannot be more than 8 characters.\n";
		  std::cout << "Enter the Student ID: ";
		  std::getline(std::cin, stuIdentification);
	   }
	   for (unsigned int count = 0; count < stuIdentification.length(); count++)
	   {
		  if (stuIdentification[count] == ' ')
		  {
			 stuIdentification[count] = '_';
		  }
	   }
	   outputFile << stuIdentification << "\t\t";

	   std::cout << "Enter " << stuIdentification << "'s " << sizeofGrades << " grades\n";
	   for (int count = 0; count < sizeofGrades; count++)
	   {
		  std::cout << stuIdentification << "'s grade #" << count + 1 << " :";
		  std::cin >> arrayGrades[count];
		  outputFile << arrayGrades[count] << "\t";
	   }

	   outputFile << std::endl;

	   char addOneMore{ ' ' };
	   int countWrongInput{ 3 };
	   std::cout << "Would you like to add another student?: ";
	   std::cin >> addOneMore;
	   while (toupper(addOneMore) != 'Y' && (toupper(addOneMore) != 'N'))
	   {
		  std::cout << "Sorry, but your answer is not valid\n";
		  std::cout << "You have " << countWrongInput << " tries.\n";
		  std::cout << "Would you like to add another student?: ";
		  std::cin >> addOneMore;

		  countWrongInput--;
		  if (countWrongInput == 0)
			 addOneMore = 'N';
	   }

	   if (toupper(addOneMore) == 'Y')
	   {
		  addRecord = true;
		  std::cin.ignore();
	   }

	   else if (toupper(addOneMore) == 'N')
	   {
		  addRecord = false;
		  outputFile.close();
	   }

    }

}

void readFile(std::ifstream& inputFile, std::string stuIdentification, double sizeofGrades, double arrayGrades[])
{
    inputFile.open("grade.txt", std::ios::in);

    if (inputFile)
    {

	   while (inputFile >> stuIdentification >> arrayGrades[0] >> arrayGrades[1]
		  >> arrayGrades[2] >> arrayGrades[3])
	   {
		  double total = 0.0;
		  for (int count = 0; count < sizeofGrades; count++)
			 total += arrayGrades[count];

		  std::cout << "The average for " << stuIdentification << " is " <<
			 std::fixed << std::setprecision(2) << total / sizeofGrades << std::endl;
	   }

    }
    else
    {
	   std::cerr << "The file could not be open.\n";
	   std::cerr << "The program will close...\n";
    }


    inputFile.close();
}
Last edited on
TY, what I did was call both file when I learned I had to name one file2 everything worked out ok. Sometimes its just the smallest things ...
Topic archived. No new replies allowed.