Plzz Help

i was trying to write a program
that prints out on screen the name of the first student with his average.

Data are stored in (data.txt) file as below

Bill 87:77:80
Mike 88:90:92
Micheal 70:78:75
......

but when i wrote the code and ran my program there was an error with the avg.
iam sure there is an error with my code..but i dont know it.

here is my code



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

int main()
{
ifstream indata;
ifstream infile;
string name;
int m1,m2,m3;
double avg,sum;

indata.open("data.txt");
infile.open("data.txt");

indata>>name;
infile>>m1>>m2>>m3;
sum=m1+m2+m3;
avg= sum/3.0;

cout<<"name is :"<<name<<endl;
cout<<"AV. : "<<avg<<endl;

indata.close();
infile.close();
return 0;
}


Last edited on
There's no need to use two separate ifstreams, get rid of one of them.

When reading the numbers, you need to also read (and discard) the ':' character.

One way would be to define two char variables, such as
 
    char a, b;


and then:
1
2
	indata >> name;
	indata >> m1 >> a >>  m2 >> b >> m3;
Last edited on
Thankss man.. it has worked.

but may i ask another questions?
why i must remove one of the ifstreams??
whats the purpose of that?

and what if i want to calculate the avg and print the name of the second guy?!
how can i do that??
closed account (Dy7SLyTq)
you were writing to the same file with two streams, which doesnt work to well. imagine that you and your friend tried to write on the same paper
why i must remove one of the ifstreams??
whats the purpose of that?

Well, when I first saw the code, I had my own question, I saw two streams, and asked myself, "what is the purpose of that?" - so maybe you could explain why you thought it was necessary.

But let me give a proper explanation. Each stream has a special pointer which gives the current position within the file. When the stream is first opened, the position is at the very first character, that is, it is positioned at the letter 'B'
Bill 87:77:80
After doing this, indata>>name; the position has moved forward, just past the end of the word "Bill". So now you are ready to read the next item, which is the number "87".

But if instead you now use the other ifstream, that is still positioned at the very start of the file, on the letter 'B'. So you try to read the number, but instead get the name "Bill". That will be very much incorrect.
and what if i want to calculate the avg and print the name of the second guy?


Well, you need to repeat this block of instructions:
1
2
3
4
5
6
7
8
    indata >> name;
    indata >> m1 >> a >>  m2 >> b >> m3;

    sum=m1+m2+m3;
    avg= sum/3.0;

    cout<<"name is :"<<name<<endl;
    cout<<"AV. : "<<avg<<endl;


You could literally do that, add the same code all over again. But a better way is to use a loop. Now a loop repeats the same instructions over and over again until we tell it to stop. How do we know when to stop? In this case, when there is no more data. So you could do something like this:
1
2
3
4
5
6
7
8
9
10
    while (indata >> name)
    {
        indata >> m1 >> a >>  m2 >> b >> m3;

        sum=m1+m2+m3;
        avg= sum/3.0;

        cout<<"name is :"<<name<<endl;
        cout<<"AV. : "<<avg<<endl;
    }


The first line here does several things:
while (indata >> name)
First, it reads the name from the file. Next it checks the status of the stream indata. This evaluates to give a logical true or false. If the file was successfully read, the condition is true, so the body of the loop will execute. But if there is no more data, the read fails and the condition is false. Then the loop will terminate.
thanks man you are amazing..
i dont know how to thank you.!:) :)

but when i asked you about the second guy..!
what if i want to print his name and his avg

with out printing the name of the first and third guy with their averages ?!
is it possible ??
and one more question .. how do you do this rectangle .. i mean in your comment
your write C++ codes in a rectangle
Of course it's possible.

There may be lots of different answers, depending on where you plan to take this next.

But to give an example, you could read the data using a loop as I suggested, and then test if (name == "Mike") before calculating and printing the output.

Another way would be to use a counter, an integer value which starts at zero. Each time you read another row of data, add 1 to the count. Then use if (count == 2)

I'm sure there could be other ways too.
Last edited on
your write C++ codes in a rectangle

Look at the Format options on the right-hand side. Choose the <> button.
The result is like this:
[code]your code here[/code]
Thanks alot dude
Topic archived. No new replies allowed.