read a text file into two parrall arrays. The file has a name and a grade
here is what I am doing
not working
#include < iostream>
#include < fstream>
#include <string>
usind namespace std;
int main() {
int const size =2;
string names[size];
int age[size];
ifstream inputFile;
inputFile.open("text.txt");
for (int i = 0; i <size; i++)
{
inputFile >> names[i];
inputFile>> age[i];
cout << age[i] << endl;
cout << name[i] << endl;
}
inputFile.close();
return 0;
}
text.txt ( Save the file in the D Drive.Then the path of the file pointing to it is D:\\text.txt )
Williams 29
Johnson 32
__________________________
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//Declaring constant
int const size =2;
//Creating string type array
string names[size];
//Creating int type array
int age[size];
//defines an input stream for the data file
ifstream inputFile;
//Opening the input file
inputFile.open("D:\\text.txt");
//Getting the data from the file and populate those value into
arrays
for (int i = 0; i <size; i++)
{
//Getting the data from the file
inputFile >> names[i]>>age[i];
}
//This for loop will display the data in the arrays
for (int i = 0; i <size; i++)
{
//displaying the data in the arrays
cout << names[i]<<" "<<age[i] <<endl;
}
//Closing the input stream
inputFile.close();
return 0;
}
_______________________
Output:
_______________Thank You
Get Answers For Free
Most questions answered within 1 hours.