trouble with char variables -- transfering them to another variable name

I have a program I am writing and it goes through a text file that has a name and then a number over an over again. I'm just trying to get the output to display the highest number and the name associated with it.

Example of the file:

Chris
40.32
32.33
David
22.24
55.76
Bob
79.35
44.32

Now my program goes through that and it can successfully compare the numbers but I cant get the names to come out because they are characters and I dont know how to do that part correctly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
char Name[20], HighName[20];
float Score1, Score2, Num, HighNum;
ifstream Infile;
Infile.open("output");

while (!Infile.eof())
{

Infile.getline(Name, 20);
Infile >> Score1 >> ws >> Score2 >> ws;

Num=Score1/Score2;
HighNum=0

if (Num > HighNum)
    {
        HighNum=Num;
        HighName=Name;
    }
}

cout << "Name of person with highest score: " << HighName << endl;
cout << "Score: " << HighNum << endl << endl;

Infile.close();
return 0;
}


Last edited on
I'm not familiair with reading files, but when i try to compile your code it gives an error in line 25: ISO C++ forbids assignment of arrays. I suggest you try type string for Name and HighName instead of char[].
A couple of things, like scipio said, you shouldn't use c style char name[20], instead use c++ strings, you can copy one to another directly.
you have to add
#include <string>
to your other includes, then initialize name

string Name=""; //make sure you use double quotes
string HighName="";

then your string copy will work

HighName=Name;





Last edited on
Topic archived. No new replies allowed.