Hello I'm using visual studio
I need help making a program where a txt file will be read.
my program should read all the test scores in the line and will stop when -1 is entered

example input file

joshua
90 89 -1
michael
76 80 89 -1
Shawn
78 100 90 80 -1

my problem is how should i get the test scores in the line to calculate average of it
Im thinking using getline(Fin,Scores) but Im not sure if that is the right method

Recommended Answers

All 10 Replies

[boilerplate_help_info]

Posing requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[list=1]
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]To [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[/list]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.

[/boilerplate_help_info]

This is my code

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

using namespace std ;
int main() 
{
	
	int Test_Number= 0;
	float  Average, Sum= 0, Score;
	string Name, File_Name;
	ifstream Fin;
	ofstream Fout;

Fin>>FName;
Fout<<Name;
while(!Fin.eof())
	{
		do{	
		Fin>>First_Name>>Last_Name;
		Fout<<First_Name<<" "<<Last_Name;
		Fin>>Score;
		}
	  while(Score !=-1) ;
	  Fin>>(Score);
	  Sum += Score ;
	  Test_Number++ ;
	}
	Average = Sum/Test_Number;

fout<<Test_number<<Average

this is what my output file shows

joshua 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 to infinity

What happened to point 2? It specifically mentioned CODE Tags. And proper formatting. You used neither.

As for your code, so many problems!

ifstream Fin;
	ofstream Fout;

Fin>>FName;     // Where are you inputting from?
Fout<<Name;     // 1: Where are you outputting to?
                // 2: What value are you outputting?
while(!Fin.eof())
	{
		do{	
		Fin>>First_Name>>Last_Name; 
		Fout<<First_Name<<" "<<Last_Name;
		Fin>>Score;         // Are you doing anything with this score?
		}
	  while(Score !=-1) ;  // What is this DO-WHILE really doing?  Work it out.
	  Fin>>(Score);
	  Sum += Score ;
	  Test_Number++ ;
	}

this is what i got right now

ifstream Fin;
	ofstream Fout;

	cout<<"Please input file name:  ";
	getline(cin,File_Name);
	Fin.open(File_Name.c_str());
	if (Fin.fail())
		cout<<"\n\t\tBad file name, process terminated.\n";
	else
	{
	cout<<"Please output file name: ";
	getline(cin,File_Name);
	Fout.open(File_Name.c_str());
	if(Fout.fail())
	{
		cout<<"\n\t\tCould not open output file. Program terminated/\n";
		return 0;
	}
while(!Fin.eof())
	{
			
		Fin>>First_Name>>Last_Name;
		Fout<<First_Name<<" "<<Last_Name;
		
	while(Score!=-1){  /* I Think there is a problem in this logic but im not sure*/
	  Fin>>Score;
	  Sum+=Score;
	  Test_Number++ ;
	}
if (Test_Number == 0)
	Fout<<"\t"<<Test_Number<<setprecision(4)<<right<<"Error there were no scores for this student"<<endl;
	else
		Fout<<"\t"<<Test_Number<<setw(14)<<fixed<<setprecision(1)<<right<<Average<<endl;
}
	Fout<<endl;
	}
	Fout.close();
    Fin.close();

    return 0;
}

And what is your problem now? And please fix your formatting for the next post. The code is very difficult to follow.

i want the output to be
joshua 2 89.5
michael 3 81.7
Shawn 4 87

but im getting

joshua 3 -0.3
3 -0.3
michael 3 -0.3
76 80 89 -1 3 -0.3
Shawn 3 -0.3
78 100 90 80 -1 3 -0.3

It's not jumping out at me (although I have my suspicions). What I'd do is
1) change the outputs from Fout into cout so you can actually watch the program as it executes.
2) add more cout statements and display values as they are read and calculated. See if the values are correct as the program progresses.

double avgl (double x)
{
static int n=0;
static double s = 0;
s+=x;
n++;
s/n;
lavg = s/n;
return lavg;
}

commented: Answer: 4 -4

You are correct, you have a logic error (more than one) in your while() loop from lines 25-29. How do you start the loop if you haven't yet read a score? Once you -do- read a score, what happens if it's -1?

I Change it now to

while(!Fin.eof())
{


Fin>>First_Name>>Last_Name;
Fout<<First_Name<<" "<<Last_Name;
Fin>>Score;
while(Score!=-1){  /* I Think there is a problem in this logic but im not sure*/
Fin>>Score;
Sum+=Score;
Test_Number++ ;
}
if (Test_Number == 0)
Fout<<"\t"<<Test_Number<<setprecision(4)<<right<<"Error there were no scores for this student"<<endl;
else
Fout<<"\t"<<Test_Number<<setw(14)<<fixed<<setprecision(1)<<right<<Average<<endl;

the output file now shows

joshua 2 89.5
joshua 2 89.5
joshua 2 89.5
joshua 2 89.5 

to infinity

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.