1-D Array From A Text File
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: 1-D Array From A Text File

  1. #1
    Join Date
    Apr 2014
    Posts
    5

    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.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2014
    Posts
    5

    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.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    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
    Code:
    int myarray[128];
    The elements can be accessed by
    Code:
    myarray[6];
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    Re: 1-D Array From A Text File

    Quote Originally Posted by rgmac1994 View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 2014
    Posts
    5

    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.

  7. #7
    Join Date
    Apr 2014
    Posts
    5

    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.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    Re: 1-D Array From A Text File

    Quote Originally Posted by rgmac1994 View Post
    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
    Code:
    int array[i] = 0;
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    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.,
    Code:
    while (cin >> c)
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Apr 2014
    Posts
    5

    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.

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center