Error message pops up, not sure what I'm doing wrong

I'm not quite sure what's not working, but when I go to debug this program (I'm using Visual Studio Express for Windows) I get an error message that says "unable to start program."

Never seen this message before and not sure what I'm doing wrong.

Basically I'm trying to write course average and a gpa average to an external file so that the output looks something like this:

Student GPA Special Note
1001 3.516 Honors
1002 1.036 Warning
1003 2.458
1004 3.268

Course 1 Average: 2.533
Course 2 Average: 3.452
Course 3 Average: 2.564

Here's the code:

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

int main ()
{
	double gpa;
	double grade[4]; // grades for student
	double coursegrade[10]; //course grade (down by the column)
	double courseavg[10]; //course average (grade summation divided by number of students in course)
	int id[10]; // id number of student
	int numstd; // number of students
	
	ifstream inputFile;
	string filename;

	int i = 0;
	for (int counter = 0; 1 < 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:\\externalfiles\\gpaout.txt", ios::out);

	if (!outfile)
	{
		cerr << "Output file could not be opened." << endl;
		exit(1);
	}

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

		int t = 0;
		t++;

		gpa = (grade[t]) / 4;

		if (gpa >= 3.5)
			outfile << "\t\t" << id << setw(10) << gpa << setw(20) << "Honors" << endl;

		if (gpa < 1.5)
			outfile << "\t\t" << id << setw(10) << gpa << setw(20) << "Warning" << endl;

		if (gpa <= 3.5)
			outfile << "\t\t" << id << setw(10) << gpa << setw(20) << endl;
	}

	int j = 0;
	int n = 0;
	numstd = 0;
	numstd++;
	courseavg[n] = coursegrade[j] / numstd;

	outfile << setw(15) << "Course" << n << "Average: ";
	outfile << setw(15) << courseavg[n] << endl;

	outfile.close();

	return 0;
 

Last edited on
What #include files are you using?

Does the code compile without any errors or warnings?

I'm use iostream, iomanip, and fstream

The only error that pops up, that I'm trying to figure out is in line 19 about the ">>". It says that no operator has been found.
The >> operator for strings is in the <string> library. You need to include that to use it.
You also need to #include the <string> header file as well.

Also are you using the "using namespace std;" clause? If not then you must properly scope all of the std:: functions and classes.

Next look at this snippet:
1
2
3
4
5
6
	string filename;  // Is this line 19?

	int i = 0;
	for (int counter = 0; 1 < 3; ++i)
	{
		std::string filename;


Do you realize that you are creating two different variables named filename in two different scopes? And that that second variable will go out of scope when that for loop ends?

Thanks tallyman! That made the error go away and my program was able to compile, but something must still be wrong in the code because it keeps asking me to input the file name..

Back to the drawing board!
hmm, I am using the "using namespace std;" I didn't realize that I was creating two different variables for filename..

Should I get rid of the filename variable before the for loop? I think I need to reread a chapter of my book..

What I'm trying to do is be able to input three different external files with different sets of data and use a for loop for the iterations. I'm not supposed to hardwire the filename into the code.. I think that's why I want filename to go out of scoop when the for loop ends?
Should I get rid of the filename variable before the for loop? I think I need to reread a chapter of my book..

If you get rid of the variable before the loop, the variable will cease to exist when the loop ends. Is that what you want?

Also because you used a for() loop, you're going to ask for the filename three times. Is that what you want?

I have three different files that I want to pull data from. So that why I set the for loop up that way.
closed account (E0p9LyTq)
sep11 wrote:
Should I get rid of the filename variable before the for loop? I think I need to reread a chapter of my book..


NO! Delete the variable definition inside your for loop. You use filename to open the file after the loop.

BTW, why are you using a loop to ask for a single file name multiple times? If you are trying to open multiple input files you need to create a string array.
Last edited on
FurryGuy wrote:
BTW, why are you using a loop to ask for a single file name multiple times? If you are trying to open multiple input files you need to create a string array.


I'm trying to do it that way because that's how my professor wants us to code this.

The assignment says:

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. This must be done interactively
within the for() loop.
It looks like you need to execute most of the code in your program three times, not just open three different files.

I you are are allowed to use functions I would do it like this:
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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int MAX_FILES = 3;

void ProcessFile(string filename);

void main ()
{
  string filename;

  for (int i = 0; i < MAX_FILES; i++)
  {
    cout << "Filename " << i + 1 << " ";
    cin >> filename;
    ProcessFile(filename);
  }

  cin.get();
}

void ProcessFile(string filename)
{
  //cout << "\nFilename: " << filename << endl;
  // your code here
}
Topic archived. No new replies allowed.