When using get line why arent my arrays picking up the value
When using get line why aren\'t my arrays picking up the values
int main () {
string line;
int numPeople = 0;
string person;
string arrPeople[20];
string tempString;
int i = 0;
ifstream peopleIn;
string lastname[20];
string firstname[20];
string SSN[20];
string address[20];
string addPeople;
string teststring = \"Adams\";
ifstream myfile (\"a1.txt\");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << \'\ \';
getline(peopleIn, lastname[i], \',\');
getline(peopleIn, firstname[i], \' \');
getline(peopleIn, SSN[i], \' \');
getline(peopleIn, address[i], \' \');
getline(peopleIn, tempString, \'\ \');
arrPeople[numPeople] = person;
++numPeople;
i++;
}
myfile.close();
}
for(i;i<20;i++)
{
cout << lastname[i];
//cout << firstname [i];
cout << SSN[i];
cout << address[i];
}
}
Here\'s whats in the a1.txt:
Robinson,Will 000000155 12 Lost in Space
Munster,Herman 000000120 150 1313 Mockingbird Lane, MockingBird Heights
Adams,Gomez 000000111 52 1313 Cementery Lane
Brady,Marcia 000000131 17 4222 Clinton Way, Los Angeles, CA
Cooper,Sheldon 000000165 33 2311 N. Los Robles Avenue, Pasadena, CA
HofStader,Leonard 000000102 33 2311 N. Los Robles Avenue, Pasadena, CA
Farah-Fowler,Amy 000000147 32 Pasadena, CA
Solution
// C++ code
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main ()
{
string line;
int numPeople = 0;
string person;
string arrPeople[20];
string tempString;
int i = 0;
//ifstream peopleIn;
string lastname[20];
string firstname[20];
string SSN[20];
string address[20];
string addPeople;
string teststring = \"Adams\";
ifstream myfile (\"a1.txt\");
if (myfile.is_open())
{
while ( 1 )
{
// read firstname
myfile >> lastname[i];
// ignore comma
myfile.ignore(1,\',\'); //ignores first comma
// read lastname
myfile >> firstname[i];
// read SSN
myfile >> SSN[i];
// read the address till the end of line
getline(myfile , address[i], \'\ \');
++numPeople;
i++;
if(myfile.eof())
break;
}
myfile.close();
}
// output values
for(i = 0;i<numPeople;i++)
{
cout << lastname[i] << \" \";
cout << firstname [i] << \" \";
cout << SSN[i] << \" \";
cout << address[i] << endl;
}
return 0;
}
/*
a1.txt:
Robinson,Will 000000155 12 Lost in Space
Munster,Herman 000000120 150 1313 Mockingbird Lane, MockingBird Heights
Adams,Gomez 000000111 52 1313 Cementery Lane
Brady,Marcia 000000131 17 4222 Clinton Way, Los Angeles, CA
Cooper,Sheldon 000000165 33 2311 N. Los Robles Avenue, Pasadena, CA
HofStader,Leonard 000000102 33 2311 N. Los Robles Avenue, Pasadena, CA
Farah-Fowler,Amy 000000147 32 Pasadena, CA
output:
Robinson,Will 000000155 12 Lost in Space
Munster,Herman 000000120 150 1313 Mockingbird Lane, MockingBird Heights
Adams,Gomez 000000111 52 1313 Cementery Lane
Brady,Marcia 000000131 17 4222 Clinton Way, Los Angeles, CA
Cooper,Sheldon 000000165 33 2311 N. Los Robles Avenue, Pasadena, CA
HofStader,Leonard 000000102 33 2311 N. Los Robles Avenue, Pasadena, CA
Farah-Fowler,Amy 000000147 32 Pasadena, CA
*/
![When using get line why aren\'t my arrays picking up the values int main () { string line; int numPeople = 0; string person; string arrPeople[20]; string tempSt When using get line why aren\'t my arrays picking up the values int main () { string line; int numPeople = 0; string person; string arrPeople[20]; string tempSt](/WebImages/35/when-using-get-line-why-arent-my-arrays-picking-up-the-value-1104998-1761584644-0.webp)
![When using get line why aren\'t my arrays picking up the values int main () { string line; int numPeople = 0; string person; string arrPeople[20]; string tempSt When using get line why aren\'t my arrays picking up the values int main () { string line; int numPeople = 0; string person; string arrPeople[20]; string tempSt](/WebImages/35/when-using-get-line-why-arent-my-arrays-picking-up-the-value-1104998-1761584644-1.webp)
![When using get line why aren\'t my arrays picking up the values int main () { string line; int numPeople = 0; string person; string arrPeople[20]; string tempSt When using get line why aren\'t my arrays picking up the values int main () { string line; int numPeople = 0; string person; string arrPeople[20]; string tempSt](/WebImages/35/when-using-get-line-why-arent-my-arrays-picking-up-the-value-1104998-1761584644-2.webp)