C++ Lab: Give the Characters a Break!

Mon Oct 06 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction

Every operation has its downtime. Even the most dependable characters need a break before they’re back in action. In this lab, you’ll create a program that helps keep track of when those breaks end — calculating how long each character will be gone before returning.

Your program will read data from a file listing each character’s name, their break time, downtime, and any additional delay. From there, it will validate that the information in the file makes sense — ensuring no negative or corrupted data slips through — and then output a clean, formatted summary showing exactly when everyone will be back.

Along the way, you’ll practice essential programming concepts that go far beyond simple input and output. You’ll open and validate external files, handle common errors gracefully, and produce an output file summarizing the results. Together, these steps introduce how real programs interact with data stored outside of the console — a major step toward building robust, automated systems that can process information on their own.


Task

Create a small program that reads information from a file about a character at Disneyland that is on break, then outputs to a file how long until the character will be back out on stage (in the park). Each row in the file contains information about a different character in the form of:

character breaktime downtime delaytime

Which each item refers to:

  • character - The name of the character on break

  • breaktime - The amount of time (minutes) the character is on break.

  • downtime - The amount of time (mintes) the character has already been on break.

  • delaytime - The amount of time the character is delayed to come back for whatever reason (maybe Mickey tripped and hurt himself chasing down Minnie backstage).

So all four pieces of information can be read in, the breaktime, downtime and delaytime checked for input failure and that they are non-negative, and if none of them are errored, then the time til the character is back can be calculated and output to a file. To calculate the time until a single character is back use:

backIn=(breaktimedowntime)+delaytimebackIn = (breaktime - downtime) + delaytime

Steps to Complete the Lab

All input files needed for this lab can be downloaded here.

You will need to:

  1. Get the input file name and open it into an input file variable. The file will need to be validated that it is opened, and an error message "Error: Invalid filename\n" will need to be output if an invalid file name is entered. The program should loop until the input file successfully opens.

  2. Statically open the file "howLongTilCharacter.txt" to an output file variable. This means the file does not be read in from the user and can just always be opened into the variable as a string.

  3. Read from the input file to skip over the headers.

  4. In an end of file controlled while loop:

    • Read in the character, breaktime, downtime and delaytime to appropriate variables.

    • Validate that the breaktime, downtime and delaytime did not have input failure and are non-negative. Output "Error: Invalid number detected on line " if they are and go on to the next character in the file.

    • Calculate the time until the character will be back.

    • Output the time until the character will be back to the output file. The output should be in the form C will be back in M minutes where C is the name of the character and M is the time until the character will be back.

  5. When the loop is done close the files.


Libraries Permitted

You may only use the following libraries in your program:

  1. iostream

  2. fstream


Criteria for Success

See CodeGrade

Example Program Interaction

An example of an interaction with your program is shown below. When interacted with, your program must produce the exact same results.

Input File

character breaktime downtime delaytime Pooh 30 10 0 Pluto -1 5 5 Pluto 20 -10 5 Pluto 20 5 -17 Pluto -100 -1 5 Pluto -67 5 -1 Pluto 20 -54 -22 Mickey 15 10 20 Minnie 15 10 0 Pluto error 5 5 Pluto 20 fail 5 Pluto 20 5 off Pluto invalid failure 5 Pluto err 5 num Pluto 20 inv no Pluto 20 5 5 Belle 45 20 60 Tiana 60 30 0 Gaston 30 0 120 Stitch 15 0 0 Mandolorian 90 30 0 Moana 60 10 15

Terminal

lab 5 % ./a.out Character Break File: error Error: Invalid filename Character Break File: charactersResting3.txt Error: Invalid number detected on line 3 Error: Invalid number detected on line 4 Error: Invalid number detected on line 5 Error: Invalid number detected on line 6 Error: Invalid number detected on line 7 Error: Invalid number detected on line 8 Error: Invalid number detected on line 11 Error: Invalid number detected on line 12 Error: Invalid number detected on line 13 Error: Invalid number detected on line 14 Error: Invalid number detected on line 15 Error: Invalid number detected on line 16

howLongTilCharacter.txt

Pooh will be back in 20 minutes Mickey will be back in 25 minutes Minnie will be back in 5 minutes Pluto will be back in 20 minutes Belle will be back in 85 minutes Tiana will be back in 30 minutes Gaston will be back in 150 minutes Stitch will be back in 15 minutes Mandolorian will be back in 60 minutes Moana will be back in 65 minutes

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.


Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.