Hi This is how my input file looks like and i while loop is sorta messed up, i'm a newbie, can someone give me a lil help?

Input file:

Iron Man 90 80 70 -1
Captain American 90 80 70 60 -1
Spider Man -90
Phil O'Sophy 72 81 63 -40

I need to list them out one by one and find their averages, so far this is what i have:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
   ifstream in;

   char Data   = char();
   char Ender	= char();
   string First = string();
   string Last  = string();
   int Grade  = int();
   string Test = string();


  in.open("C:\\Input.txt");

   if(!in) 
   {
      cout << "Error: Input File Cannot Be Oppened" << endl;
      
   }
  in >> Data;
   while (!in.eof())
   {
      in >> First >> Last >> Grade;
	  while ( Ender != '-' ) 
		{
		cout << First << Last << Grade;

		}
   
   }
   in.close();
   cout << "End-of-file reached.." << endl;
 

   cin >> Test; 
}

Recommended Answers

All 4 Replies

It will not be able to know the difference between the names and the actual numbers when you read the files and set them to something. fyi

char Data = char(); What is char()? string()? int()?
These lines I assume give you an error, therefore you have no idea if your while is messed up. in >> Data What are you expecting to read with this line? while (!in.eof()) Since .eof() is the same as feof(), read this.

That should get you started.

lol i feel so stupid, thanks for the hints guys. we i kinda improved my code to this

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

int main()
{
		string First =			string();
		string Last  =			string();
		string Test  =			string();
		int Counter =			int();
		int Data =				int();
		int Grade =				int();
		int Total =				int();
		int Average =			int();
	



ifstream in;

in.open("C:\\Input.txt");


if (!in)
	{
		cout << " Fail To Retrieve Data From Input File " << endl;
}

else if (in)
{
	while (!in.eof())
	{
		Counter = 0;
		getline (in , First);
		getline (in , Last);
	
			while (Data > 0)
			{
				in >> Grade;
				
		++Counter;
			}
			cout << First << Last << Data << Counter << endl;
			
	}

}

cin >> Test;



}

now i'm successfull to retrieve all the data, but i'm having trouble getting the total & average.

lol i feel so stupid, thanks for the hints guys. we i kinda improved my code to this ...

Adding TABs to mung up the formatting is not taking a hint. Two of the three things I mentioned in my previous post was completely ignored.

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.