How to fill an array with strings

Hello,
I have an assignment it is rather difficult for me. I have to read from a file one word at a time and fill and array of strings. I have no clue how to do this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void fillString(fstream &fin)
{
	string str= "";
	const int size = 100;
	
	
	for(int i= 0; size > i; i++)
	{	

		string inputarray[size];
		fin.getline(str, ' ') = size;
	}

}

This is my attempt which as you can see does not do anywhere. I figure if I can read from the file word by word then I can have an array that I can manipulate later to get rid of unwanted things and such.

Any ideas please
Thanks.
size > i
should be
size < i surely?

fin.getline(str, ' ') = size;
This is just nonsense. To read one word at a time:
fin >> inputarray[size];
and create the array outside the loop.
Actually that should be:
i < size
size being the array limit.
And
 
fin >> inputarray[i];


@AbstractionAnon

As a for-loop termination condition

i < size

behaves the same as

size > i

They are just restatements of the same relationship.

i is less than size <=> size is greater than i

So you can use either.

You start off with is less than size. You increase i a bit on the each iteration. The loop stops when i has been increased enough that i is equal to or greater than size, or size is now less than or equal to i.

Andy
Last edited on
This is my code it isn't working I'm just trying to see if the array was actually filled.


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iomanip>
#include <istream>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream fin;


// Function to test for Empty file
bool BadFile(ifstream &fin)
{	
	int filelength;
	
	//To determine file length 
	fin.seekg(0, ios::end);
	filelength = fin.tellg();
	fin.seekg(0, ios::beg);
	// Selection of return value
	if(filelength == 0)
		return true;
	else 
		return false;
}



// To Print Heading
void print_heading()
{
	bool isEmpty;
	
	//Formatting for Heading 
	cout << "Letter " << '\t' << "Count " << '\t'<< "Frequency " << endl;
	cout << setw(6) << setfill('=') << '='  <<'\t' << 
	setw(5) << setfill ('=') << '=' << '\t' << setw(9) 
	<< setfill('=') << '=' << endl;

	// Function to test for Badfile
	isEmpty=BadFile(fin);
	
	// Message for if File is empty
	switch( isEmpty)
	{
	case true: cout << "No data exists. " << endl;
		break;
	default : break;
	}	

}

//To fill array full of strings
void fillString(fstream &fin)
{
	string str= "";
	const int size = 100;
	string inputarray[size];
	
	for(int i= 0; size < i; i++)
	{	

		fin >> inputarray[size];
		
	} 
	for(int j=0; j < size; j++)
	cout << inputarray[size];
}


void main()
{	
	

	fin.open("mp6input.txt");
	print_heading();
	
	fillString(fin);
	
system("pause");


}


I get an error
1>c:\users\jordan\documents\visual studio 2010\projects\mp6\mp6\mp6arrays.cpp(78): error C2664: 'fillString' : cannot convert parameter 1 from 'std::ifstream' to 'std::fstream &'


Thx
I was responding to Moschops, that stated the terminator should have been size < i . i.e. 100 < i would never be true.
Oop. Sorry. I'm so used to the condition just using the same token as the initialiser (i.e for (int i=0; i < someValue ...) that I didn't even notice it wasn't. Good catch.
Use ifstream rather than fstream for fillString() as you do for BadFile().

And line 60 is wrong

Use either

for(int i= 0; size > i; i++)

or

for(int i= 0; i < size; i++)

i starts of smaller than size, and increased until it is equal or greater size. Your current condition (size < i) is evaluating to false immediately (well, i=0 and size=100) so the loop never even starts. Both size > i and i < size evaluate to true for the initial conditions, and do so until i catches up with size.

And line 63 should be

fin >> inputarray[i];

You loop index is i, not size

Similarly for line 67 (with j as the index in this case).

Andy

PS You should prob. move ifstream fin; into main() and pass fin as a parameter to print_heading(), as you already do for both BadFile() and fillString(). That way you can eliminate the evil global. And BadFile() could do with a better name (the name should tell you what it does!).
Last edited on
Just a quick glance, but at line 67 you are outputting an element that is actually beyond the array. Size = 100, which means the elements of the array number from 0 to 99. inputarray[100] is actually outside of the array. Besides that, you have the output in a loop. What I think you meant to write, and which is standard practice, is actually cout << inputarray[j].

Actually, I see the same error at line 63 as well. Also like was stated, if you want to iterate through the array the middle of the first for in fillString on line 60 should read i < size. And, while it's a minor formatting thing, you use different brace styles for each of the for loops. This makes it a bit harder to read. While the compiler will read both, you only need braces if your statement inside the loop is actually more than 1 statement. Otherwise the for loop at line 66 is fine without them since it's only 1 line (IE, you only need 1 semicolon) Also, you don't have to use int j for the second for loop, you can actually reuse int i since variables declared in the initialization expression only have a local scope of that loop. So after execution of the first for loop int i no longer exists and can be declared and initialized again in the second one. Only time you really have to use different variable names is if you have nested loops.

And if you are worried about empty files, you might as well add an error check to see if the file opened at all. Something like this works:

1
2
if (!(fin.is_open()))
exit(EXIT_FAILURE);


@Andy you would be right, but that's not what he wrote. What he has at line 60 is size < i, or i > size, which will never be true with i being initialized at 0 to begin with.
Last edited on
And now back to the OP's problem:


I get an error
1>c:\users\jordan\documents\visual studio 2010\projects\mp6\mp6\mp6arrays.cpp(78): error C2664: 'fillString' : cannot convert parameter 1 from 'std::ifstream' to 'std::fstream &'


You've declared fin as an ifstream, but fillString is expecting an fstream.
Change fillString to expect an ifstream.
@Raezzor

In the original post, the condition was right (on line 7). It was then mis-corrected in the longer post further down.

@AbstractionAnon

Sorry! As you didn't @ anyone, I mis-took your post to be a response to the original poster.
Last edited on
Oops, sorry Andy! I guess we were both right. :)
BadA*S Thanks for help people. Much appreciated as always.
Jordan
Topic archived. No new replies allowed.