Hi this is tetsu here
I need a little help regarding input and output. I am trying to write a program to read information from a text file as long as there is names and numbers next to them (test score numbers). For each name read, a sequence of test scores is read, but stops reading when it hits a negative number. When it stop at the negative numbers it will calculate the numbers it read so far in to an average number. Well Im stuck at the negative number and getting how many numbers there is. I read the <fstream> Tutorials but I still dont understand it.

Well this is what I have now, it isnt much.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

string avgtext = "Your average is ";
string grade = "and your letter grade is ";

int main()
{
	int score1 = int();
	int score2 = int();
	int score3 = int();
	int score4 = int();
	int score5 = int();
	int i = int();
	string fName = string();
	string lName = string();
	

	ifstream inFile;
inFile.open("c:\\input.txt");
	

	inFile >> fName >> lName >> score1 >> score2;
	cout << "Your Name is : " + fName << " " << lName << endl;
	cout << "Your scores are : " << score1 << " " << score2 << " ";
	cout << score3 << " " << score4 << " " << score5 << endl;

	for(int i=0; i<5; ++i)
	while(i<2)
	{
		inFile >> fName >> lName >> score1 >> score2 >> score3 >> score4 >> score5;
		++i;
	}
	/*
if (x > 90)
	{
		cout << avgtext << x << grade << "A" << endl;
	}
	else if (x > 80 && x <= 90)
	{
		cout << avgtext << x << grade << "B" << endl;
	}
	else if (x > 67.5 && x <= 80)
	{
		cout << avgtext << x << grade << "C" << endl;
	}
	else if (x > 55 && x <= 67.5)
	{
		cout << avgtext << x << grade << "D" << endl;
	}
	else (x >= 0 && x <= 55)
	{
		cout << avgtext << x << grade << "F" << endl;
	}*/
	inFile.close();
	return 0; 
}

text file

Tetsu Itou 84 73 83 -3
Mimi Itou 87 76 90 -70 65

I'm not quite sure what you're asking. Do you want to stop reading scores for that name at the first negative number? If so, why does your example have a score after a negative number and not a name? If not, what is a negative score supposed to mean? Given your example file, I would assume this to be what you want:

#include <string>
#include <vector>

using namespace std;

string first, last;

while ( in>> first >> last ) { // Make sure there's a record
  vector<int> score;
  int hold;

  while ( in>> hold )
    score.push_back ( hold );

  in.clear(); // Relied on an error condition to stop the loop

  // Print the record
}

Of course, I could be wrong since your question wasn't entirely clear.

inFile >> fName >> lName >> score1 >> score2;
cout << "Your scores are : " << score1 << " " << score2 << " ";
	cout << score3 << " " << score4 << " " << score5 << endl;

You are asking for 3, 4, and 5 scores and did not define them.

inFile >> fName >> lName >> score1 >> score2 >> score3 >> score4 >> score5;

I think you just forgot to add them, if I understand your question right. It will now read all the scores (including negative).

Hi thanks for the replys
Im sorry if I wasnt clear. This is what it says in the book.
"This project will give you an introduction to the use of the WHILE repetition statement. Your Program is to process information as long as there are more names in the file. For each name read, a sequence of test scores is read, terninated by a negative number. After the reading of numbers is terminated, your program will
- report the test average or
--explain why that can't be done."
----------------------------------------------------------------
A sample of inputs from file.
Toots Sweet
84 73 83 -3

Phil O'Sophy
-5
----------------------------------------------------------------
Should produce the following

Your name is: Toots Sweet
Your scores are : 84 73 83 -3
Toots your average is 80

Your name is: Phil O'Sophy
Your scores are : -5
Phil O'Sophy you didnt enter any score
---------------------------------------------------
I sort of rewrote the code. I think I just endup confusing myself more when I rewrote it.

this is what i have now

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

string avgtext = "Your average is ";
string grade = "and your letter grade is ";

int main()
{
	int peoplecount;
	double avg;
	int i = int();
	string fName = string();
	string lName = string();
	double totalForperson;
	int thisscore;
	int count;
	

	ifstream inFile;
	ofstream outFile;

	inFile.open("c:\\input.txt");
	


   if (!inFile) 	// Opening failed
   {
		 cout << "File opening error. Program terminated.";       
		 return  1;
   }
	cout << "Averaging score program" << endl;
	peoplecount = 0;
	inFile >> fName >> lName; 

	while(inFile)        // Last read successful
    {
        peoplecount++;  
		cout << fName << lName << endl;
	      totalForperson = 0;
        count = 0;
        while(thisscore > 0 )
        {
            inFile  >> thisscore;
			if (thisscore > 0)
			{
            totalForperson = totalForperson  + thisscore;
			count ++;
		}
        }

        avg =  (totalForperson)/ peoplecount;
        cout  <<  avg << endl; 
            
        inFile  >> fName >> lName;
    }


	/*
if (x > 90)
	{
		cout << avgtext << x << grade << "A" << endl;
	}
	else if (x > 80 && x <= 90)
	{
		cout << avgtext << x << grade << "B" << endl;
	}
	else if (x > 67.5 && x <= 80)
	{
		cout << avgtext << x << grade << "C" << endl;
	}
	else if (x > 55 && x <= 67.5)
	{
		cout << avgtext << x << grade << "D" << endl;
	}
	else (x >= 0 && x <= 55)
	{
		cout << avgtext << x << grade << "F" << endl;
	}*/
	inFile.close();
	return 0; 
}
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.