-
April 26th, 2014, 07:10 AM
#1
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.
Question1.cpp
I apologize if my description is not very clear.
-
April 26th, 2014, 07:50 AM
#2
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;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 26th, 2014, 07:56 AM
#3
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.
-
April 26th, 2014, 07:58 AM
#4
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 26th, 2014, 08:08 AM
#5
Re: 1-D Array From A Text File
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.
Question1.cpp
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/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 26th, 2014, 08:27 AM
#6
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.
-
April 26th, 2014, 08:37 AM
#7
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.
-
April 26th, 2014, 09:03 AM
#8
Re: 1-D Array From A Text File
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 26th, 2014, 10:00 AM
#9
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.,
-
April 26th, 2014, 05:07 PM
#10
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.
-
April 27th, 2014, 08:01 AM
#11
Re: 1-D Array From A Text File
to what my instructor had shown us (which seems to have some issues.)
Hmm...
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/
Last edited by 2kaud; April 27th, 2014 at 08:05 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|