Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating using array of structs?
    primarykey
    data
    text
    <p>I have been pretty lost on this project that focuses on using array of structures. We just learned about them. I think I get the basics of it after going over some of our labs and looking at related questions:</p> <p><strong>StackOverflow</strong><br> <a href="https://stackoverflow.com/questions/5526415/trouble-passing-an-array-of-structs">Trouble passing an array of structs</a><br> <a href="https://stackoverflow.com/questions/11564336/array-of-pointers-to-structures">array of pointers to structures</a><br> <a href="https://stackoverflow.com/questions/8570707/how-to-initialize-array-of-structures-with">How to initialize array of structures with?</a><br> <a href="https://stackoverflow.com/questions/5071410/arrays-in-stuctures">Arrays in stuctures</a><br> <a href="https://stackoverflow.com/questions/7239220/creating-an-array-of-structs-in-c">Creating an array of structs in C++</a><br> <a href="https://stackoverflow.com/questions/8534526/how-to-initialize-an-array-of-struct-in-c">How to initialize an array of struct in C++?</a></p> <p><strong>Cplusplus</strong><br> <a href="http://cplusplus.com/forum/beginner/15042/" rel="nofollow noreferrer">Declaring an array of structures</a><br> <a href="http://cplusplus.com/forum/beginner/82097/" rel="nofollow noreferrer">Array of Structs C++ Program Help</a><br> <a href="http://cplusplus.com/forum/general/5385/" rel="nofollow noreferrer">Problem with a Dynamic Array of Structs</a></p> <p>I'd REALLY appreciate any advice or help anyone can give. We are allowed to use this old lab (it was assigned right after we finished this lab and now we are 3 labs past it) as a starting point:</p> <p><strong>Dynamic Arrays</strong></p> <pre><code>// Assignment : Lab // File : Lab.cpp // Description : This program will take a text file called words.txt and then swap // its individual (word)strings. Finally, it will calculate the vowels, consonants, // digits, and special characters in each string. #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;iomanip&gt; #include &lt;cstdlib&gt; using namespace std; bool isvowel (char aletter); // const int MAXWORDS = 100; struct worddata { worddata (); string word; int vowels; int consonants; int digits; int specialchars; }; int ReadFile (ifstream &amp; input, worddata * &amp; Words); void WriteReport(ostream &amp; output, worddata Words [], int count); void Swap (worddata &amp; a, worddata &amp; b); void WordSort (worddata W [], int N); int main (int argc, char * argv []) { // Check to see if the user entered a file name // Exit if no file name if (argc &lt; 2) { cout &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " &lt;filename&gt;\n"; exit (1); } // Open the input file ifstream input (argv[1]); if (input.fail()) { cout &lt;&lt; "File: " &lt;&lt; argv[1] &lt;&lt; " not found\n"; exit (2); } // Declare a pointer to an array of worddata objects // to hold the words and their vowel, consonant, digit, and // special character counts. // worddata WordArray [MAXWORDS]; worddata * WordArray; // Call the ReadFile function to read the file, store the // words in the array and return the number of words read // from the file. int count = ReadFile (input, WordArray); // Call the WordSort function to sort the words into // alphabetical order. WordSort (WordArray, count); // Call the WriteReport function to write the data // stored in the array in a formatted fashion. WriteReport (cout, WordArray, count); return 0; } worddata::worddata () { vowels = 0; consonants = 0; digits = 0; specialchars = 0; } int ReadFile (ifstream &amp; input, worddata * &amp; Words) { int count = 0; string oneword; // Read and count the words in the file while (input &gt;&gt; oneword) count++; // Allocate space for the number of words counted by the // previous loop Words = new worddata [count]; // Clear the fail flag input.clear(); // Reposition the file pointer to the beginning of the file input.seekg (0, ios::beg); count = 0; // Read the words from the file into the array while (input &gt;&gt; Words[count].word) { // Count the number of vowels, consonants, digits // and special characters in the word and store them // in the object Words [count] string aword = Words[count].word; // Number of letters in word int l = 0; while (l &lt; aword.length()) { if (isvowel(aword[l])) Words[count].vowels++; else if (isalpha(aword[l])) Words[count].consonants++; else if (isdigit(aword[l])) Words[count].digits++; else Words[count].specialchars++; l++; } count++; } // Close the file input.close (); // Return the size of the Words array return count; } void WriteReport (ostream &amp; output, worddata Words [], int count) { worddata totals; totals.vowels, totals.consonants = 0; totals.digits, totals.specialchars = 0; output &lt;&lt; setw (14) &lt;&lt; left &lt;&lt; "Word"; output &lt;&lt; setw (8) &lt;&lt; right &lt;&lt; "Vowels"; output &lt;&lt; setw (8) &lt;&lt; "Const."; output &lt;&lt; setw (8) &lt;&lt; "Digits"; output &lt;&lt; setw (8) &lt;&lt; "Special" &lt;&lt; endl;; for(int i = 0; i &lt; count; i++) { output &lt;&lt; setw (14) &lt;&lt; left &lt;&lt; Words[i].word; output &lt;&lt; setw (8) &lt;&lt; right &lt;&lt; Words[i].vowels; totals.vowels += Words[i].vowels; output &lt;&lt; setw (8) &lt;&lt; Words[i].consonants; totals.consonants += Words[i].consonants; output &lt;&lt; setw (8) &lt;&lt; Words[i].digits; totals.digits += Words[i].digits; output &lt;&lt; setw (8) &lt;&lt; Words[i].specialchars &lt;&lt; endl; totals.specialchars += Words[i].specialchars; } { output &lt;&lt; setw (14) &lt;&lt; left &lt;&lt; " "; output &lt;&lt; setw (8) &lt;&lt; right &lt;&lt; "---"; output &lt;&lt; setw (8) &lt;&lt; "---"; output &lt;&lt; setw (8) &lt;&lt; "---"; output &lt;&lt; setw (8) &lt;&lt; "---" &lt;&lt; endl; output &lt;&lt; setw (14) &lt;&lt; left &lt;&lt; "Totals"; output &lt;&lt; setw (8) &lt;&lt; right &lt;&lt; totals.vowels; output &lt;&lt; setw (8) &lt;&lt; totals.consonants; output &lt;&lt; setw (8) &lt;&lt; totals.digits; output &lt;&lt; setw (8) &lt;&lt; totals.specialchars &lt;&lt; endl; } } void Swap (worddata &amp; a, worddata &amp; b) { worddata t = a; a = b; b = t; } void WordSort (worddata W [], int N) { int i = 1; while(i &lt; N) { int j = i; while(j &gt; 0 &amp;&amp; W[j].word &lt; W[j-1].word) { Swap(W[j], W[j-1]); j--; } i++; } } // Returns true/false depeninding if a letter in a word is a vowel or not bool isvowel (char aletter) { char upcase = toupper (aletter); if (upcase == 'A' || upcase == 'E' || upcase == 'I' || upcase == 'O' || upcase == 'U') return true; return false; } </code></pre> <p><a href="http://watts.cs.sonoma.edu/cs215s13/Project1.pdf" rel="nofollow noreferrer">Project link</a></p> <p><strong>This is what I've managed to write so far without getting lost or it breaking into oblivion</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;iomanip&gt; #include &lt;fstream&gt; #include &lt;vector&gt; #include &lt;sstream&gt; #include &lt;cstdlib&gt; using namespace std; // const int ListSize = 50; struct Assignment { char atype; string date; float received; int possible; string title; }; // functions used by main int ReadFile(ifstream&amp; input, Assignmnent list[], int listSize); void WriteReport(ostream &amp; output, Assignment list [], int numRecords); void Swap (Assignment &amp; a, Assignment &amp; b); void CategorySort (Assignment C [], int N); int main() { // Check to see if the user entered a file name // Exit if no file name if (argc &lt; 2) { cout &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " &lt;filename&gt;\n"; exit (1); } // Open the input file ifstream input (argv[1]); if (input.fail()) { cout &lt;&lt; "File: " &lt;&lt; argv[1] &lt;&lt; " not found\n"; exit (2); } int numRecords = ReadFile(input, Assignmnent list[], int listSize); if(numRecords &gt; ListSize+1) { cout &lt;&lt; "Too Many Records for this program to read" &lt;&lt; endl; system("read"); return -1; } // no records? if(numRecords == 0) { cout &lt;&lt; "Empty File" &lt;&lt; endl; system("read"); return -1; } } </code></pre> <p>I also know that I'll probably be using getline. That's about it. I have a feeling once I have an outline for how I want to approach ReadFile and declare some of these scopes I'll be fine. I am just really cautious and unsure about starting that off. Also, if the prototypes seem strange I based it on a lab I looked at from another class at another school so I have no idea if they work in this context. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload