-2

I code my programe for showing student profile

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

int main()
{
    int array_size = 500;
    char * array = new char[array_size];
    int position = 0;

    ifstream fin("data.txt");

    if(fin.is_open())
    {
        while(!fin.eof() && position < array_size)
        {
            fin.get(array[position]);
            position++;
        }
        array[position-1] = '\0';

        for(int i = 0; array[i] != '\0'; i++)
        {
            cout << array[i];
        }
    }
    else
    {
        cout << "File could not be opened." << endl;
    }
    return 0;
}

and here is data.txt

Name : Napat Naraprapang
ID : 45608

Subject                             Credit      Grade

Thai                                 1.0          4 
Mathematics                          1.0          4
Social Studies                       1.0          4
Health and Physical Education        0.5          4
Thai Dance                           0.5          3
Vocational Education and Tecnology   0.5          4
English                              1.0          4
Advance Mathematics                  2.0          4
Physics                              1.5          3.5       
Chemistry                            1.5          3
Biology                              1.5          4
Handball                             0.5          4
Computer Project                     1.0          4
English Reading Analysis             1.0          4
English for Information              0.5          4

I want to calculate gpa and show it below, should I do? Actually, first time I created function to calculate gpa before

for(int i = 0; array[i] != '\0'; i++)
            {
                cout << array[i];
            }
        }
        else

And when I complied it, everything was lose, I dont know what did I do wrong? and what should I do? Thank you for your all replies!

Miel
  • 3
  • 4
  • [Please do not use `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Also, it's always a good idea to paste the compilation error you're getting into the question. – orlp Mar 14 '15 at 16:34
  • A few things: don't tag with objective-c when posting about c++, and, I know it's difficult but try to read the compilation errors, it gets easier as you learn. And last but not least, post your errors (but only after reading and failing to understand them!). I didn't understand your last question, was it how to write the entire program? If so, that's a bit too broad. – keyser Mar 14 '15 at 16:38
  • There is absolutely nothing "OOP" about your code, if we don't count the fact that standard I/O streams use an object-oriented design. – Christian Hackl Mar 14 '15 at 16:56
  • @orlp why shouldn't I use using namespace std? – Miel Mar 14 '15 at 18:08
  • @Miel Click on the link I gave you in my original comment. – orlp Mar 14 '15 at 18:10

2 Answers2

1
  1. Firstly, /n is not a single character. \n is the newline character which you require.
  2. getline(myfile.line) is an invalid statement, as myfile, a , an object of ifstream, has no member line.

    Change it to:

    std::getline(myfile,line);
    
shauryachats
  • 9,975
  • 4
  • 35
  • 48
0

I'm not certain if this is the only error, but I do know that this line:

getline(myfile.line);

Should be:

std::getline(myfile, line);

And '/n' should be '\n', assuming you want to separate by lines.

orlp
  • 112,504
  • 36
  • 218
  • 315