1

Ok, so here's the deal. This is a project for school, and we can't use #include < string >. Basically, for any strings we'll be dealing with, we have to use cstrings, or char arrays that end with a null terminator. Basically the same thing right? Well I'm having a little bit of trouble. I have to read in a first name, last name, a student id, and a minimum of 5 grades but a maximum of 6 grades from an input file. To see what that looks like is below, but there is a catch. There can be an arbitrary amount of spaces in between each of those details, with the maximum length of the line being 250. So an example of the input is below.

Adam Zeller 452231     78    86 91   64 90       76
Barbara Young 274253 88   77  91    66  82    
Carl Wilson 112231       87    77    76   78  77    82

Notice, how there are random amounts of spaces in between the details. Basically, I need to get the names (both first name and last name can vary in length), read the student id into an int, and then read all the rest of their grades (preferably into an int array). Also, they can have either 5 or 6 grades,the program should be able to handle either. How in the world do I go about sorting this data? I thought maybe I could getline() into a cstring char array of the whole line, and then seperate each bit accordingly into each array, but I just don't know how to go about this. Indefinitely, I don't want anyone to give me any code, but maybe point me in the right direction of how I could go about this. Sorting a line of data into different variables, while also accounting for either 5 or 6 grades without effecting the data, and also the list could be up to 60 lines long (meaning have up to 60 students on it but no more than that). This is only a portion of the project, but seems to be the one part I can't get past. Again, I don't want any code or direct answers, maybe just point me in the right direction of a way I could go about this. Thanks so much!

Eric Diviney
  • 327
  • 2
  • 5
  • 16

3 Answers3

2

I'm not going to post any code (as requested), but consider filtering each line through strtok. strtok splits the string into tokens, where they can be arranged or stored however you like. See here for more: http://www.cplusplus.com/reference/cstring/strtok/

coln
  • 123
  • 7
2

I guess you may follow the below steps:

read a line
get the words from the line
first two words are first and last names
rest of the words are number, use atoi to get the numbers from string
continue the above till EOF

How do you get the words ? May be "isspace" c library function will help

Nipun Talukdar
  • 4,975
  • 6
  • 30
  • 42
1

Technically, this is a trick project/question.

The hardest part of parsing the lines in the file is as you said: dealing with the random amount of spaces.

Since you requested no code, I'll give you a few hints based on what you suggested:

You know that the size of each line is a maximum of 250 characters (including the newline character?) - this is the length of each line, and as such, since it occurs with such regularity, you can read this many characters at a time with regular file functions, or, using fstreams (as deduced from your tag).

The only issue really, is storing these tokens. If you know ahead of time how many you will store, you can define an array of c-strings (as you can't use <string>) comprised of a maximum number that you think will occur. However, as that is both unreliable and a bit inefficient (as it's a waste of memory if you choose too much lines to store), you can make it dynamic. In that regard you have the option of using a C++ container like <vector>, for ease of access and storage.

After that, figuring out the values of the data on each line is relatively easy:

First, look at your data, what do you observe?

  • Each individual piece of data (a token in parsing nomenclature) is delimited by at least one space character.
  • Also, the names are the first two tokens of any line, and do not contain digits in them.

Hence anything that has not a space or a number belongs to a string, in this case: part of a name.

All you have to do is then iterate over the container/data structure you've used to store the c-strings and parse them using the criteria described above.

jrd1
  • 10,358
  • 4
  • 34
  • 51