C++ Reading From Text File

i am not getting the contents from an input file.

Page 1 of 1

6 Replies - 6682 Views - Last Post: 27 May 2010 - 03:52 AM Rate Topic: -----

#1 Guest_sandy*


Reputation:

C++ Reading From Text File

Posted 26 May 2010 - 03:06 PM

A particular talent competition has five judges each of whom awards a score between 0 and 10 each. Fractional scores are allowed. A performer's final score is determined by dropping the highest and lowest score received then averaging the three remaining scores. Write a program that uses this method to calculate a contestant's scores.

Note: read the contestants name and scores from an input file
There will be 5 contestants

The program will need the following functions:

void getJudgeData() reads the name of the contestant and his or her scores. The name and scores should be saved in a reference parameters and should be validated (between 0 and 10)

void calcScore() should calculate and display the name and average of the three scores that remain after dropping the highest and lowest scores the performer received. This function should be passed the five scores.

The last two functions will be called by calcScore which uses the returned information to determine which of the scores to drop

int findLowest() should find and return the lowest of the five scores passed to it.

int findHighest() should find and return the highest of the five scores passed to it.

Finally, write a function winner, that takes the contestants average scores and prints the name and average score of the winner

this is my program and i have to read the inputs form file. input file containing the name and scores.
i am stuck on reading name and scores form file can any one help me?

this is my code:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<string>

using namespace std;

void getJudgeData(char&,char&,double&,double&,double&,double&,double&);

int main()
{
	char fName[20];
	char lName[20];
	double marks1=0.0,marks2=0.0,marks3=0.0,marks4=0.0,marks5=0.0;
	getJudgeData(fName,lName,marks1,marks2,marks3,marks4,marks5);
	return 0;
}
void getJudgeData(char &firstName,char &lastName,double &m1,double &m2,double &m3,double &m4,double &m5)
{
	
	ifstream infile;
	infile.open("stars.txt");
	if(!infile)
	{
	 cout<<"Error!File not found.";
	 exit(1);
	}
	cout<<setprecision(2)<<showpoint<<fixed;
	
	for(int i=1;i<=5;i++)
	{
		infile>>firstName;
		infile>>lastName;
		infile>>m1>>m2>>m3>>m4>>m5;
	}
	for(int i=1;i<=5;i++)
	{
		cout<<firstName;
		cout<<lastName;
		cout<<m1<<m2<<m3<<m4<<m5;
	}
			
		
}

This post has been edited by JackOfAllTrades: 26 May 2010 - 03:08 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0

Replies To: C++ Reading From Text File

#2 Guest_sandy*


Reputation:

Re: C++ Reading From Text File

Posted 26 May 2010 - 03:09 PM

please someone help me how to read this file. i can't use array. i can only use loop.
my file contents are:
johnny weir 8.0 7.5 8.8 9.0 7.2
lindsey vonn 9.2 9.4 8.2 8.8 8.9
shaun white 9.3 9.5 8.2 8.9 8.9
apolo ohno 9.5 9.3 8.1 8.3 8.8
angela ruggiero 8.0 8.8 9.0 9.9 9.8
Was This Post Helpful? 0

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,486
  • Joined: 23-August 08

Re: C++ Reading From Text File

Posted 26 May 2010 - 03:12 PM

We have a tutorial on reading text files that might be helpful.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_sandy*


Reputation:

Re: C++ Reading From Text File

Posted 26 May 2010 - 03:35 PM

View Postsandy, on 26 May 2010 - 02:06 PM, said:

A particular talent competition has five judges each of whom awards a score between 0 and 10 each. Fractional scores are allowed. A performer's final score is determined by dropping the highest and lowest score received then averaging the three remaining scores. Write a program that uses this method to calculate a contestant's scores.

Note: read the contestants name and scores from an input file
There will be 5 contestants

The program will need the following functions:

void getJudgeData() reads the name of the contestant and his or her scores. The name and scores should be saved in a reference parameters and should be validated (between 0 and 10)

void calcScore() should calculate and display the name and average of the three scores that remain after dropping the highest and lowest scores the performer received. This function should be passed the five scores.

The last two functions will be called by calcScore which uses the returned information to determine which of the scores to drop

int findLowest() should find and return the lowest of the five scores passed to it.

int findHighest() should find and return the highest of the five scores passed to it.

Finally, write a function winner, that takes the contestants average scores and prints the name and average score of the winner

this is my program and i have to read the inputs form file. input file containing the name and scores.
i am stuck on reading name and scores form file can any one help me?

this is my code:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<string>

using namespace std;

void getJudgeData(char&,char&,double&,double&,double&,double&,double&);

int main()
{
	char fName[20];
	char lName[20];
	double marks1=0.0,marks2=0.0,marks3=0.0,marks4=0.0,marks5=0.0;
	getJudgeData(fName,lName,marks1,marks2,marks3,marks4,marks5);
	return 0;
}
void getJudgeData(char &firstName,char &lastName,double &m1,double &m2,double &m3,double &m4,double &m5)
{
	
	ifstream infile;
	infile.open("stars.txt");
	if(!infile)
	{
	 cout<<"Error!File not found.";
	 exit(1);
	}
	cout<<setprecision(2)<<showpoint<<fixed;
	
	for(int i=1;i<=5;i++)
	{
		infile>>firstName;
		infile>>lastName;
		infile>>m1>>m2>>m3>>m4>>m5;
	}
	for(int i=1;i<=5;i++)
	{
		cout<<firstName;
		cout<<lastName;
		cout<<m1<<m2<<m3<<m4<<m5;
	}
			
		
}


i ma having problem with how to pass character array as reference parameter? can anyone help me?
i can't use vector.
Was This Post Helpful? 0

#5 Guest_sandy*


Reputation:

Re: C++ Reading From Text File

Posted 26 May 2010 - 03:38 PM

View PostJackOfAllTrades, on 26 May 2010 - 02:12 PM, said:

We have a tutorial on reading text files that might be helpful.



i ca't use vectors. can you please tell me how to read the name and scores form an input file. how does the function prototype looks like?
how can i pass the char array as reference parameter?
Was This Post Helpful? 0

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,486
  • Joined: 23-August 08

Re: C++ Reading From Text File

Posted 26 May 2010 - 04:03 PM

An array *IS* a reference parameter.

For example:
void getIntArray(int array[], const int len)
{
    for (int i = 0; i < len; ++i)
        array[i] = i + 1;
}

int main(void)
{
    int my_array[10] = { 0 }; // initialize to all 0s
    getIntArray(my_array, 10);
  
    for (int i = 0; i < 10; ++i)
    {
        std::cout << "Array index " << i << " = " << my_array[i] << std::endl;
    }
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ Reading From Text File

Posted 27 May 2010 - 03:52 AM

Have a read here:
http://www.cplusplus...tutorial/files/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1