1 Attachment(s)
1-D Array From A Text File
Hello, I was given an assignment on outputting the number of upper and lower case vowels from a text file. I was provided guidelines, but I'm still very new to arrays. Could someone explain to me how:
1. The values would be sorted into the array
2. To correctly output the array.
3. What information seems to be missing. :sick:
Attachment 32591
I apologize if my description is not very clear.
Re: 1-D Array From A Text File
Its better to post code directly here than providing a link.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
ifstream fin("conrad.txt");
char c;
int i; //???
for (int i = 0; i < 128; ++i)
int array[i] = 0; //
fin >> c;
while (!fin.eof())
{
switch (c)
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u': array[int(c)]++;
}
fin >> c;
}
system("pause");
return 0;
}
Re: 1-D Array From A Text File
Actually, I really appreciate that. I'm new to posting in forums, so I wasn't sure how to post the way most people do.
Re: 1-D Array From A Text File
An array is defined by specifying its type and how many elements it has which need to be able to be determined by the compiler at compile time. To define an array of 128 elements of type int use
The elements can be accessed by
Note that this is the 7th element as array access starts at 0 (not 1).
An array is not initialised when it is defined. If you want every element to be set to the same value when it is defined probably the easiest way is
Code:
int myarray[128] = {0};
which sets each element of the array to 0. Note that if 0 is replaced by 1 then only the first element is set to 1, the others are set to 0.
Hope this helps with your assignment. If you modify your code and repost we'll provide further guidance. Also see
http://www.tutorialspoint.com/cplusplus/cpp_arrays.htm
http://www.learncpp.com/cpp-tutorial/61-arrays-part-i/
http://www.cplusplus.com/doc/tutorial/arrays/
and http://forums.codeguru.com/showthrea...ork-assignment
Re: 1-D Array From A Text File
Quote:
Originally Posted by
rgmac1994
Hello, I was given an assignment on outputting the number of upper and lower case vowels from a text file. I was provided guidelines, but I'm still very new to arrays. Could someone explain to me how:
1. The values would be sorted into the array
2. To correctly output the array.
3. What information seems to be missing. :sick:
Attachment 32591
I apologize if my description is not very clear.
1) The easiest way to sort an array is to use the STL sort. See
http://www.cplusplus.com/reference/algorithm/sort/
2) To output an array one way is to just use a loop to access the array elements and display their values. A more advanced way would be use the STL copy with ostream_iterator.
See http://www.cplusplus.com/reference/algorithm/copy/
Re: 1-D Array From A Text File
Thank you for putting in so much effort to help me understand this; I greatly appreciate it. I'll repost later today, once I've gotten closer to resolve. For now, however, I need a decent break to sleep.
Re: 1-D Array From A Text File
Also, could you explain why my instructor told us to use:
int array[i] = 0;
The issue that comes up is that [i] is not constant, so the compiler complains that it is a variable-sized object.
Re: 1-D Array From A Text File
Quote:
Originally Posted by
rgmac1994
Also, could you explain why my instructor told us to use:
int array[i] = 0;
The issue that comes up is that [i] is not constant, so the compiler complains that it is a variable-sized object.
No as
is not valid ANSI c++ code. For i to be used like this it needs to be of type const. Also an array is not initialised like this. In ANSI c++ this would be
Code:
const int i = 128;
int array[i] = {0};
which defines an array of type int with 128 elements all initialised to 0.
Re: 1-D Array From A Text File
By the way, it is generally not a good idea to use a call to eof() to control a loop since eof() would return false after a failed read, i.e., even after end of file has been reached, there would need to be one more attempted read for eof() to return false. In your case, you did do it correctly because you first read into c once before the loop, and then you read into c at the end of the loop body, but you could also have gone with cin >> c in a single place to control the loop, e.g.,
Re: 1-D Array From A Text File
Actually, I'm glad you mentioned that. I was more inclined to use,
Code:
while (fin.is_open())
and I had noticed that only one cin >> c should be necessary, but I tried to keep it as accurate to what my instructor had shown us (which seems to have some issues.) Thanks for bringing these things up; now, I have revising to do.
Re: 1-D Array From A Text File
Quote:
to what my instructor had shown us (which seems to have some issues.)
Hmm... :rolleyes:
Code:
while (fin.is_open())
Unless the file is closed during the loop, this condition will always be true if the file has been successfully opened.
Bascially, the state of the stream should be tested after any stream operation which might change the state of the stream. There are 4 stream states: good - eveything is ok; eof - end-of-file reached; fail - often set because of trying to read wrong type of input (try to read an int but stream contains chars etc); bad - panic! often caused by hardware issues etc. So there are more states than just eof to deal with. See http://www.cplusplus.com/reference/i..._base/iostate/
When a stream function/operator returns a stream reference, then if the operation was good then this is a valid stream reference (ie the value is non-NULL). If the operation could not be completed then the return value if NULL. Hence code like this
Code:
while (cin >> c)
{
//operation good so do processing
}
//loop terminates when issue with obtaining input
if (cin >> c)
{
//operation good
} else {
//operation bad
}
Some stream functions do not return a stream reference, then the stream state needs to be tested. See http://www.cplusplus.com/reference/istream/istream/get/ for an example.
You can also do something like this
Code:
while (ch = is.get(), is.good())
{
//stream good and ch valid
}
eg
Code:
while (ch = cin.get(), cin.good())
cout << ch;
This works because statements seperated by the comma operator are executed from left to right with the value of the entire expression being the value of the rightmost statement. See http://msdn.microsoft.com/en-us/library/zs06xbxh.aspx
Note that if the stream state is not good, the state needs to be reset before further input. ie if set to fail from reading a bad int then the state needs to be reset before another int is tried to be read. If the state is not reset then all further reads fail. See http://www.cplusplus.com/reference/ios/ios/clear/