I am supposed to only use the "procedural fragment of C++", i.e. no object-oriented programming (like cin, cout etc). Vectors are not allowed either.
So, one of my goals is to count the number of lines in a text file (let's call it train1.in) which contains data like:
engine 15 engine 40 coach 30 boxcar 52
Now, I am supposed to count the number of cars (i.e. the number of lines in the file), which in this particular case is clearly 4 (two engines, a coach, a boxcar).
Please tell me a simple way to do this. I have one idea in mind- count the number of newlines but I am not sure how exactly to implement this. I am not sure but I think getline() is perhaps not a part of procedural C++. The bottomline is that the code should look like C code.
I have another question. I am just interested in knowing how to read the "spur file_name n" and "shunt file_name n" from user and interpret them as commands.
Write pseudocode and procedural C++ routine for a main routine that accepts a sequence of
train construction commands from standard input (i.e. just use read).
• spur fname n – accept a filename and a number of cars (locomotives) to remove
• shunt fname n – accept a filename and a number of cars (locomotives) to remove (before the reversal)
and then print out the train that results from concatenating each of the trains that the commands generate,
including its length.
For example, if train1.in contains
and train2.in contains
then the commands
The train contains 5 cars
engine, 40 m.
coach, 30 m.
boxcar, 52 m.
hopper, 50 m.
lowboy, 45 m.
totalling 217 m.
So, if I get to know how to read the commands from user and also interpret them then I will put the necessary code in braces to perform the requisite task. I need help on this as soon as possible. I would greatly appreciate your help.
Thanks a lot!!:)
I have written a few pseudocode algorithms (may have a lot of errors)
Edited 5 Years Ago by codeyy: n/a