Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I'm afraid this cycle isn't perfectly correct

0 votes
asked Jun 5, 2021 by Natalia (1,080 points)
I've edited my code a bit. Now it compiles. I tried to add three drivers to each bus using a cycle. However, I doubt this cycle is correct, because it works only for the first few buses. Here's the link https://onlinegdb.com/nsHpcEyj6
related to an answer for: Expected primary expression before * token

1 Answer

+1 vote
answered Jun 7, 2021 by Peter Minarik (86,160 points)
edited Jun 8, 2021 by Peter Minarik
 
Best answer

I've done a cleanup pass on your code:

  • put the classes in their header and source files
  • created a constructor
  • used virtual functions instead of copy-pasting things around for displaying the classes

Here's the solution: https://onlinegdb.com/V83J6siZUe

The problem is not the assignment for you, but rather reading input from the file.

The way you read things, the new-line character is part of your input. So for instance the bus number "8887" is stored as "\n8887" in your system, hence you won't find it.

Same applies for the drivers: every name (except the first line) starts with a line break.

I'd suggest revisiting your input an make sure you skip the new-line characters.

You could read the entire line from your data file, then parse the content. E.g.:

std::string buffer;
getline(fbus, buffer);
std::string number = readNextString(buffer);
std::string color = readNextString(buffer);
std::string brand = readNextString(buffer);
int year = readNextInt(buffer);
float petrol = readNextDouble(buffer);
int seat = readNextInt(buffer);
std::string route = readNextString(buffer);

You can look at https://www.geeksforgeeks.org/how-to-split-a-string-in-cc-python-and-java/ for how to tokenize in C++ (look for section "Method 2: Using C++ find() and substr() APIs.")

Based on the linked article above, you can come up with a class or just methods like readNextString, readNextInt, etc that read the buffer from the last position and get the next string/int/float token.

Good luck!

Update

I was bored out of my mind at work, so I wrote the code for you. First try to do it yourself before looking at my version.

(Have you written your code first?! XD)

If you open the linked project (https://onlinegdb.com/V83J6siZUe), you can look at the Tokenizer class (Tokenizer.h and Tokenizer.cpp)

Notes:

  • Online GDB doesn't handle a large amount of tabs (files) well. My solution was to decrease the font size on the page so everything gets smaller and I can see the tabs. Later, if needed, you can increase the font size when the right source file (tab) is selected.
  • I needed to add a '|' to the end of each of your source data files so the parser works and I do not have to make it more complicated than it needs to be (checking for end of file, etc)
  • A beautiful solution would be buses.push_back(Bus(tokenizer.NextString(), tokenizer.NextString(), tokenizer.NextString(), tokenizer.NextInt(), tokenizer.NextFloat(), tokenizer.NextInt(), tokenizer.NextString())), however, since the evaluation order for the coma operator is right to left, it will put the first read element from the bus database (number) to the last parameter of the bus (route), and so on. This could be solved by re-organizing the order of the parameters in the constructor or in the data file. However, we are humans and we make mistakes and we often make assumptions. Often wrong ones. This would be just too confusing for any readers. Hence the Bus::fromTokens static function to deal with this in an acceptable manner.
    The same applies to the Driver class.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...