I was lost in this project, which focuses on using an array of structures. We just found out about them. I think I learned about this after going through some of our laboratories and studying related issues:
StackOverflow
Problem with passing an array of structures an
array of pointers to structures
How to initialize an array of structures with?
Arrays in structures
Creating an array of structures in C ++
How to initialize an array of structures in C ++?
Cplusplus
Declaring an array of structures.
Array of software tools. C ++ Program. Help.
Problem with a dynamic array of structures.
I would REALLY appreciate any advice or help anyone can give. We are allowed to use this old laboratory (it was appointed immediately after we finished this laboratory, and now we have already passed 3 laboratories) as a starting point:
Dynamic arrays
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
bool isvowel (char aletter);
struct worddata
{
worddata ();
string word;
int vowels;
int consonants;
int digits;
int specialchars;
};
int ReadFile (ifstream & input, worddata * & Words);
void WriteReport(ostream & output, worddata Words [], int count);
void Swap (worddata & a, worddata & b);
void WordSort (worddata W [], int N);
int main (int argc, char * argv [])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <filename>\n";
exit (1);
}
ifstream input (argv[1]);
if (input.fail())
{
cout << "File: " << argv[1] << " not found\n";
exit (2);
}
worddata * WordArray;
int count = ReadFile (input, WordArray);
WordSort (WordArray, count);
WriteReport (cout, WordArray, count);
return 0;
}
worddata::worddata ()
{
vowels = 0;
consonants = 0;
digits = 0;
specialchars = 0;
}
int ReadFile (ifstream & input, worddata * & Words)
{
int count = 0;
string oneword;
while (input >> oneword)
count++;
Words = new worddata [count];
input.clear();
input.seekg (0, ios::beg);
count = 0;
while (input >> Words[count].word)
{
string aword = Words[count].word;
int l = 0;
while (l < 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++;
}
input.close ();
return count;
}
void WriteReport (ostream & output, worddata Words [], int count)
{
worddata totals;
totals.vowels, totals.consonants = 0;
totals.digits, totals.specialchars = 0;
output << setw (14) << left << "Word";
output << setw (8) << right << "Vowels";
output << setw (8) << "Const.";
output << setw (8) << "Digits";
output << setw (8) << "Special" << endl;;
for(int i = 0; i < count; i++)
{
output << setw (14) << left << Words[i].word;
output << setw (8) << right << Words[i].vowels;
totals.vowels += Words[i].vowels;
output << setw (8) << Words[i].consonants;
totals.consonants += Words[i].consonants;
output << setw (8) << Words[i].digits;
totals.digits += Words[i].digits;
output << setw (8) << Words[i].specialchars << endl;
totals.specialchars += Words[i].specialchars;
}
{
output << setw (14) << left << " ";
output << setw (8) << right << "---";
output << setw (8) << "---";
output << setw (8) << "---";
output << setw (8) << "---" << endl;
output << setw (14) << left << "Totals";
output << setw (8) << right << totals.vowels;
output << setw (8) << totals.consonants;
output << setw (8) << totals.digits;
output << setw (8) << totals.specialchars << endl;
}
}
void Swap (worddata & a, worddata & b)
{
worddata t = a;
a = b;
b = t;
}
void WordSort (worddata W [], int N)
{
int i = 1;
while(i < N)
{
int j = i;
while(j > 0 && W[j].word < W[j-1].word)
{
Swap(W[j], W[j-1]);
j--;
}
i++;
}
}
bool isvowel (char aletter)
{
char upcase = toupper (aletter);
if (upcase == 'A' || upcase == 'E' || upcase == 'I' || upcase == 'O' || upcase == 'U')
return true;
return false;
}
Project link
This is what I managed to write so far without losing or falling into oblivion
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <cstdlib>
using namespace std;
struct Assignment {
char atype;
string date;
float received;
int possible;
string title;
};
int ReadFile(ifstream& input, Assignmnent list[], int listSize);
void WriteReport(ostream & output, Assignment list [], int numRecords);
void Swap (Assignment & a, Assignment & b);
void CategorySort (Assignment C [], int N);
int main() {
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <filename>\n";
exit (1);
}
ifstream input (argv[1]);
if (input.fail())
{
cout << "File: " << argv[1] << " not found\n";
exit (2);
}
int numRecords = ReadFile(input, Assignmnent list[], int listSize);
if(numRecords > ListSize+1)
{
cout << "Too Many Records for this program to read" << endl;
system("read");
return -1;
}
if(numRecords == 0)
{
cout << "Empty File" << endl;
system("read");
return -1;
}
}
I also know that I will probably use getline. This is about it. I have a feeling when I have a plan on how I want to go up to ReadFile and declare some of these areas. Everything will be good. I am just very careful and don't know how to start this. Also, if the prototypes seem strange, I based them on a laboratory where I looked from another class to another school, so I have no idea if they work in this context.