Programming Project 1

This assignment is due Friday, February 13, by the start of class.

Write a program that does the following:

  1. Prompt the user to enter a file name for input.

  2. Open the specified file. If the file doesn't exist, print an error message and exit the program.

  3. Read in all the words from the input file. You may assume that there are no more than 1000 words in the file.

    You may also assume that a ``word'' is a contiguous group of non-white space characters. This sounds complicated, but is really not. For example, in the previous sentence, not. (including the period) would be a word, as would "word" (including the quotation marks) in the sentence before that.

  4. Sort the set of words into alphabetical order. Remember that alphabetical order in C++ means that capitalized words come first, then non-capitalized, etc. Keep in mind that you can compare strings using the same comparison operators (<, >, etc.) that you use with numbers.

  5. Prompt the user to enter a file name for output.

  6. Write the words, in alphabetical order, to the output file.

Place the work for this assignment in your personal I:\CSC122 folder in a folder called HW1. See below for how to create a project from scratch. Turn in a printout of this assignment on the due date in class. There is a file called sample.txt in the public\Howard folder that you can use to test your program.

There are a number of topics that will be useful for the assignment:

You may want to brush up on these, either with your CS1 book or Appendix A in the textbook.

Creating a Microsoft Visual C++ .NET 2003 Project from Scratch

From the File menu, choose Project... out of the New submenu. The ``New Project'' dialog box will appear. Select Visual C++ Projects from the project types, and Win32 Project from the templates, then enter the correct project location and name in the text boxes; for this assignment, you would enter I:\CSC122\YourName as the location, and HW1 as the name. Click ``OK'' and the ``Win32 Application Wizard'' dialog will appear. Click on ``Application Settings'' and then select both ``Console Application'' and ``Empty Project''; when you click on ``Finish'' a blank project skeleton will be created.

In the ``Solution Explorer'' window, right-click on the ``Source Files'' folder and choose Add/Add New Item...; in the dialog that pops up, select C++ File and enter a name (such as main.cpp), then click ``Open''. An editor window will appear where you can start entering your program. If you need to add more source files to your project, follow this same process (right-clicking on either ``Source Files'' or ``Header Files'' as appropriate).

To compile and run your program, first select Build/Build Solution from the menu bar. If there are no errors, then you may run the program by selecting Debug/Start Without Debugging from the menu bar (this choice causes the program to display the ``Press any key to continue'' message after your program exits, so that you don't need to insert a pause at the end of main).

Creating a Microsoft Visual C++ 6.0 Project from Scratch

From the File menu, choose New...; in the dialog box that appears, make sure the ``Project'' tab is selected, then choose Win32 Console Application. Enter the correct project name and location in the text boxes; for this assignment, you would enter I:\CSC122\YourName as the location, and then enter HW1 as the project name (doing so will also tack on HW1 to the end of the location). Click ``OK'' and the ``Win32 Console Application'' wizard will appear; choose ``An empty project'' and click ``Finish'', then click ``OK'' on the confirmation dialog.

To add a source file to the project, select File/New... from the menu bar, and make sure the Files tab is selected in the dialog box that appears. Now choose C++ Source File (or C/C++ Header File, as appropriate), and enter a file name such as main.cpp. When you click on ``OK'', a blank editor window will appear.

To compile your project, select Build from the Build menu; to run, select Execute from the Build menu. As with VC++ .NET 2003, after your program terminates the console will display the message ``Press any key to continue'', so that you don't need to insert a pause at the end of main for testing.

Creating a Borland C++ Project from Scratch

From the File menu, choose Project... out of the New submenu. The ``New Target'' dialog box will appear. Enter the correct project path and name in the first text box; for this assignment, you would enter I:\CSC122\YourName\HW1\hw1.ide. Make sure that ``Application [.exe]'' is selected under ``Target Type,'' and that the ``Platform'' and ``Target Model'' are ``Win32'' and ``Console'' (it will probably default to ``GUI,'' so you will need to be sure this gets changed). Now click the ``Advanced'' button and uncheck the boxes labeled .rc and .def, then click ``OK'' twice to make each dialog go away. Finally, double-click the .cpp node in the project window that appears and you will get an editor window where you can start entering your program.

To attach additional source files to a project, choose File/New/TextEdit and then File/Save as... from the menu. Give your file an appropriate name in the ``Save File'' dialog and click ``OK.'' Now, in the project window, right-click on the .exe node (at the top) and choose ``Add node'' from the context menu. This will bring up a file chooser; select the new file you just created and click ``OK.'' The next time you build the project, this new file will be compiled in with the rest of the program.

To build your project, select Project/Build All from the menu bar; to run, select Debug/Run. Remember that Borland does not automatically pause after your program executes, so you will need to insert either getch() (defined in conio.h) or cin.get() (defined in iostream) at the end of main.