This would explain a lot about my program's behavior. Upon modification, I realized that some values, despite being outputted, were not properly being stored. I changed the array output loop at the end to
1 2 3
|
cout<<"\nArray contents:\n";
for (int i=0;i<11;i++)
cout<<word[i]<<"("<<i<<")"<<endl;
|
hoping to display each array position's string. Not only did some entries output to multiple lines, but some values such as the first zip code, were never stored. If you output
it is the CA from the first address, yet when outputting
it only displays Adam.
I tried making changes as suggested above, but it only made things worse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
#include <vector>
using namespace std;
int main()
{
string testline;
vector<string> word;
ifstream Test ( "Data.txt" );
if (!Test)
{
cout << "There was a problem opening the file. Press any key to close.\n";
getch();
return 0;
}
//store words in array while outputting, skipping blank lines
while( Test>>testline )
{
getline ( Test, testline, ' ');
cout<<testline;
word.push_back(testline);
}
//output whole array with spaces between each word
cout<<"\nArray contents:\n";
for (int i=0;i<word.size();i++)
cout<<word[i]<<"("<<i<<")"<<endl;
return 0;
}
|
1234
Ventura,
Adam
356
Calabasas,92136
Array contents:
(0)
1234(1)
(2)
Ventura,(3)
(4)
Adam(5)
356(6)
(7)
Calabasas,(8)
(9)
92136(10) |
Changing the condition of the while loop to merely be
Test
on line 25 results in closer to the desired output, yet still displays some oddities.
Steven
Seagal
1234
Post
Drive
Ventura,
CA
90734
Adam
Sandler
356
Golf
Street
Calabasas,
CA
92136
92136
Array contents:
Steven(0)
Seagal
1234(1)
Post(2)
Drive
Ventura,(3)
CA(4)
90734
Adam(5)
Sandler
356(6)
Golf(7)
Street
Calabasas,(8)
CA(9)
92136(10)
92136(11)
|
I have two questions:
#1. Why does it output and store the last zip code twice when reading it in?
#2. Why are multiple strings being combined into single entries? (I assume it has something to do with newline characters, but am unsure). Outputting
gives me
now.
Yet I wish to store the information as 16 differing entries, not 12, and without the redundancy of storing zip codes twice.