-2

I don't know what happened to the post but it must have happened on my first edit.

I got this info to go in and work when it was in separate files. But I need it on one file.
I have looked in my text book and a bunch of other places but I can not find how to only get the text or characters out of the file. I have got all the info into a single array but it looks like I will need to pull out each group piece by piece and put it where I want it but that looks slow, tedious and very susceptible to errors.

Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 

If this looks familiar it is the same assignment as my previous post, now I just need to figure out how to parse this info and use it again.

2
  • I will even take getting this info into an array and copying cells into the right place -- Read your own question, and pretend you're someone looking at how this is worded. Does it portray any information as to what the structure is you're reading into? Commented Dec 1, 2019 at 2:19
  • @PaulMcKenzie. I have edited my question. It must have got messed up during submission. Commented Dec 1, 2019 at 13:53

2 Answers 2

0

Chances are, you'll need to take your input as string value and check through it to find the beginning of the numerical characters.

only after having separated the alphabetical part of the input string from it's numerical part, you start creating your target arrays.

this might help: How can I check if a string has special characters in C++ effectively?

/e: wording

Sign up to request clarification or add additional context in comments.

Comments

0

There are multiple ways of doing it. You could read it into a string and process it manually based on spaces. Or you could use stringstream to extract numerical values into array/vector. That however, still requires you to remove the name before you do it.

Here is a little code that reads the file content into an unordered_map which is essentially a dictionary as defined in other languages.

void read_file(const std::string& path) { std::ifstream in(path); // file stream to read file std::unordered_map<std::string, std::vector<double>> map; /* * map structure to hold data, you do not have to use this. * I am using it only for demonstration purposes. * map takes string (name) as KEY and vector<double> as VALUE * so given a NAME you can get the corresponding grades VECTOR * i.e.: map["Johnson"] --> [85, 83, 77, 91, 76] */ std::string line; while (std::getline(in, line)) { // read entire line if (line == "") continue; // ignore empty lines int last_alpha_idx = 0; // name ends when last alphabetic is encountered for (size_t i = 0; i < line.size(); i++) if (std::isalpha(line[i])) last_alpha_idx = i; // get index of last alpha std::string name = line.substr(0, last_alpha_idx + 1); // name is from index 0 to last_alpha_idx inclusive (hence +1) std::string numbers = line.substr(last_alpha_idx + 1); // array values the rest of the line after the name std::stringstream ss(numbers); // this is an easy way to convert whitespace delimated string to array of numbers double value; while (ss >> value) // each iteration stops after whitespace is encountered map[name].push_back(value); } } 

You could read it into an array, the code will not change dramatically. I chose string as KEY and vector<double> as VALUE to form KEY/VALUE pairs for the dictionary (map).

As you can see in the code, it looks for the last alphabetic character in each line and takes its index to extract the name from the read line. Then it takes the rest of the string (just the numbers) and puts them into a stringstream which will extract each number individually in its inner loop.

Note: the code above supports having full names (e.g. "Johnson Smith 85 83 77 91 76").

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.