#include #include #include using namespace std; int main() { //Declaring constan
ID: 3800955 • Letter: #
Question
#include
#include
#include
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 {
//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 {
//displaying the data in the arrays
cout << names[i]<<" "< }
//Closing the input stream
inputFile.close();
return 0;
}
________
does not work - giving me two long numbers like it is convering the input
have tried several files with a string and a number
just gives two longs numbers ??????????????????? c++
update??????
read from an input text file /name and grade/ into parallel arrays - output simple right? but I am geetting two long numbers - no name no number
Explanation / Answer
Answer
The reuirement can be achieved by the below code::
#include <iostream.h>
#include <string.h>
#include <fstream.h>
int main()
{
ifstream file("D:\text.txt");
int i;
string names[100];
string age[100]; //numbers are also taken as string
//after reading from the file this can be converted into integer and
//stored in integer array
if(file.is_open())
{
for(i = 0; i < 100; ++i)
{
file >> names[i]>>age[i];
}
}
for(i = 0; i < 100; ++i)
{
cout<<names[i]<<" "<<age[i];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.