Hi...I am not able to input text file correctly as desired......
data.txt
--------
Jim A
James A+
Smith
Cathy D
Dazy
Peter C

I am suppose to input the above file and fill the above data in two seperate arrays.
For the second column, I am suppose fill NULL value in the array for the missing values corresponding to Smith and Dazy.

here is my code.

string name, grade;
ifstream infile;

infile.open("data.txt",ios::in);

if(infile.fail())
{
   cout << "Error opening file";
}
else
{
   for(int i=0;i<6;i++)
   {
     infle >> name[i] >> grade[i];
     cout << name[i] << "\t" << grade[i];
   }
infile.close();
   
}

Can somebody please help me out. The output I get is wrong. I don't know how fill NULL in grade array for the missing values.

Recommended Answers

All 6 Replies

Depends on what you need help with. Care to tell us what's going wrong?

I get output something like below. Where I want NULL, the name gets filled there. I an new to C++.I simply have no idea how I am going to fill NULL for missing grade Values

Jim A
James A+
Smith Cathy
D Dazy
Peter C

Lanor, NULL is a non-display value meaning that it cannot be seen even when it's be written to the file. In the case where you want to display the word 'NULL', you should check for null value in the grade array and explicitly display 'NULL'. Here's an example

infle >> name[i] >> grade[i];
if(grade[i] == '\0')   // NULL is represented by backslash zero
{
   cout << name[i] << "\tNULL";
}
else
{
   cout << name[i] << "\t" << grade[i];
}

The way my code reads the file is not ok...Place where there is a missing value....I'm suppose to fill something, say null in the array......so that I can later update the null value with actual grade.

I am to fill the name in name array and fill the grade with the grade array. If there were no missing grade value, then there would be no problems, but since there are missing values, I am not able to fill the two arrays with the appropriate values. I just don't have the brains to do so. Help Me out guys...Pliz

Because of the missing value...The next name gets filled into the grade array instead of the name array.

Two possibilities:
1) Change the file so that the missing grades are, say, hyphens:
Jim A
James A+
Smith -
Cathy D
Dazy -
Peter C

2) Read the entire line ( getline() ) and check to see if there is a grade. Then load the name and grade from what you read.

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.