I'm trying to take some information from a file and input it into an array so that it can be output on the screen, but I can't get this to work. Here's the kicker though....the file has to allow 100 test scores, but must end when it hits -999....any ideas?

File

53
55
55
65
78
78
78
80
85
85
92
92
92
95
95
95
95
-999
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void main()
{
	ifstream scores;
	
	int i, Quiz[100];
	string file;


	cout << "Enter name of file" << endl;
	cin >> file; file += ".txt";

	scores.open(file.c_str());

	if(scores)
	{
		for (i=0;i < 101 ;i++)
			if (Quiz[i] != -999)
			{
				cout << Quiz[i] << endl;
			}
			else
				cout << "Error";
	}
}

>any ideas?
Maybe break; the input loop on the sentinel value?

>any ideas?
Maybe break; the input loop on the sentinel value?

how would I do this? I've tried:

if (i = -999)
if (i=0;i != -999; I++ )

what should I do?

I'm imagining something kinda complicated:

for ( i = 0; i < 100; ++i )
      {
         scores >> Quiz[i]; // Read value from file.
         if ( Quiz[i] == -999 )
         {
            break;
         }
      }

[edit]Actually, I'd probably use a temporary for receiving the file input so I could avoid putting the sentinel value into the array.

I'm imagining something kinda complicated:

for ( i = 0;i < 100; ++i )
      {
         scores >> Quiz[i]; // Read value from file.
         if ( Quiz[i] == -999 )
         {
            break;
         }
      }

ok.... I was tinkering with this, but it was printing out the -999, which (of course) I don't want:

if(scores)
	{
		for (i=0;i < 101 ;i++)
			if (scores >> Quiz[i])
			{
				cout << Quiz[i] << endl;
			}
			else if (i = -999)
			{
				break;
			}
	}

Actually...when I try your code, it prints out the -999 value as well.....I'm not sure how to correct this....

Actually...when I try your code, it prints out the -999 value as well.....I'm not sure how to correct this....

Actually my code doesn't. Mine is not put together like yours (perhaps you didn't notice that I don't print on input, and I do the comparison correctly). I'm trying to see what you can manage for yourself. The edit of my last post may be of interest.

Actually my code doesn't. Mine is not put together like yours (perhaps you didn't notice that I don't print on input, and I do the comparison correctly). I'm trying to see what you can manage for yourself. The edit of my last post may be of interest.

I'm at a loss...I'm trying to figure it out, but I can't.....

I'm at a loss...I'm trying to figure it out, but I can't.....

Why not try posting your latest effort?

Perhaps think about what you had written in your last post. For example, if i is the loop counter that goes from 0 to 100 (which would be outside of array bounds), when will it equal -999? Well, if input failed in order to get you to the else, and if you had used the comparison operator instead of assignment.

[edit]Or perhaps consider what might happen if you indeed break out of the loop when -999 is encountered? What would i be? 100?

Don't just throw code at it, think about things a bit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.