Good morning, everyone. As part of an assignment, I am tasked with using the header file <cstring> and the cin member function, getline, to extract file input and store it in character arrays (c-strings). The assignment prohibits the use of sstream, the header file <string> and the use of extraction operators (>>) to read from the input file. A single line from the input file may look like the following:
Mario Delagarza 437734 78 89 96 85 77 98
Unlike the line above, the line from the input file may contain variable amounts of whitespace between its contents. How can the input be read using getline and then each piece of information be manipulated so as to ignore the variable amounts of whitespace between those contents that are seperated by more than one whitespace?
#include <iostream>
#include <fstream>
usingnamespace std;
int main(int argc, char** argv) {
// Read from file
string line;
line="Mario Delagarza 437734 78 89 96 85 77 98";
string output;
for (int i=0; i < line.size(); i++)
{
if (line[i] == ' ') // if space is found
{
cout << output << endl;
output = "";
}
else // if no space is found
{output=output+line[i];}
}
return 0;
}
Thank you for your time. Unfortunately, the assignment asks that we use getline so we have to manipulate the c-string, or character array instead of a string. I've got the following so far and at the moment the latter is not even doing anything. I don't receive an error or any value returned. The console window opens and just sits there until I exit out of it using the exit button. The former is closer but for whatever reason, it isn't writing to an output file on my computer while on a friend's it does:
Good morning, Thomas1965. I am unfamiliar with sscanf but if I did know how to use it, it wouldn't matter for this assignment because we are supposed to use getline to get each record from an input file and then manipulate the c-string, meaning that each character would be part of a single character array including the numbers and from that c-string, I would need to sort through each element and determine whether it is part of the first name, last name, id number, and each individual grade. The records are not hard-coded into any variables or entered on the console. They are to be retrieved from an input file. I apologize if I didn't clarify that in the beginning sufficiently.
OP: I'm sorry but your assignment is just ridiculous and poorly thought out. Did your instructor explain what s/he wants you to learn out of these absurd set of restrictions?
Also, I'm not sure whether you need to convert the numeric data from string to integer form? If so there are functions for that. Doing the conversion yourself is possible too, but would add complexity to an already messy set of requirements.
Good morning, gunnerfunner. I understand it is frustrating. I have tried about three different algorithms and there is some issue with all of them. I think my instructor wants us to learn the most basic functions and how they are used prior to using more advanced techniques so we know how to build on the older and more basic functions or eventually learn why objects work the way they do.