I have this assignment in which we receive a file with at most 40 names( > 10 characters), last name ( > 12 characters), id # ( always 6 chars), and 6 grades (lowest one must be dropped) and I am restricted from using the string library.
so basically something like this (and yes, arbitrary amount of spaces and \\t in between the data):
Adam Zeller 452313 78 86 91 64 90 76
Barbara Young 274253 88 77 91 66 82 93
Carl Wilson 112235 87 99 76 15 95 94
Alec Burmeister 234689 45 76 98 54 12 8
and ultimately it'll need to look like:
Last_name First_name ID Average_Score Grade
Zeller Adam 452313 82.3 B
I currently have been struggling a lot with getline(), and get(), I can choose to use either one of these (I CANNOT use cin >> ... to receive the data)
I've attempted using both, but getline gives me too many bugs, and so far get() seems to be most promising, though I could be wrong.
I currently have this:
const int SIZE = 256;
char student_list[SIZE];
char input_filename[40] = "student_input.txt",
output_filename[40] = "student_results.txt";
Student student[40]; // I want to use a struct, but still trying to figure
// out how to make the characters into variables so
// that I can manipulate the values.
fstream fin;
fin.open(input_filename, ios::in);
if(fin.fail())
{
cout << "ERROR - COULD NOT FIND INPUT FILE \n\n";
return 1;
}
char c;
while (fin.get(c)) // loop getting single characters
{
if((c == ' '))
c = '\b';
else if(c == '\t')
c == '\b';
else
cout << c;
}
cout << "\n\nProgram successfully terminated!\n";
fin.close();
return 0;
I know this code is a little flawed, since this just gets me the output, but it works how I expect it too.. it just takes out all of the spaces and \\t. and ALL of my data is there (in comparison to getline which ate the last values in Adam Zellers' line... only his, and not anybody else's idk why.)
AdamZeller452313788691649076
BarbaraYoung274253887791668293
CarlWilson112235879976159594
AlecBurmeister23468945769854128
That's the output I get, but because i'm using char c rather than an array, I can't take the values and just output them outside. Plus, it seems even if I could use an array, it would not work because each name and last name is different length, so i can't hard code it for every student...
Anyway, Am i doing this more complicated than it should be, or am i in the right path? I can't figure out how to manipulate each individual char, or store each set (name, last, id, grades) of chars into variables, since they are all individual characters, rather than strings.
You could simplify your life by parsing based on space separation:
std::string first_name;
std::string last_name;
int values[7];
while (fin >> first_name)
{
fin >> last_name;
for (unsigned int i = 0U; i < 7; ++i)
{
fin >> values[i];
}
}
A better method is to place all these variables into a structure and overload operator>>
for the structure.
Also, change the array to std::vector
. The array can be useful if the text lines always have the same quantity of numbers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.