Previous Page TOC Next Page



- Project 3 -
Data Basics


In this lesson, you learned about the fundamental data variables found in most C++ programs. You learned the following:

Project 3 Listing. Introduction to data and I/O.




1:  // Filename: PROJECT3.CPP



2:  // This program defines different kinds of variables and



3:  // lets the user's keyboard input fill the variables with



4:  // values. Subsequent couts print the data in the variables.



5:  #include <iostream.h>



6:



7:



8:  void main()



9:  {



10:    char prodName[12];



11:    int count;



12:    float price;



13:    float totalInv;



14:    cout << "Inventory Calculation" << endl;



15:    cout << "---------------------" << endl << endl;



16:



17:    cout << "This program calculates an inventory's cost." 



18:         << endl << endl;



19:    cout << "What is the product name? ";



20:    cin >> prodName;



21:    cout << "How many " << prodName << "s are there? ";



22:    cin >> count;



23:    cout << "How much does each " << prodName << " cost? ";



24:    cin >> price;



25:    // Ready to extend the inventory cost



26:    totalInv = count * price;



27:    cout.precision(2);



28:    cout.setf(ios::showpoint);



29:    cout.setf(ios::fixed);



30:    cout << endl << "The total valuation for " << prodName



31:         << " is $" << totalInv << endl;



32:    return;



33:  }

Output

Description

1: A Visual C++ comment that includes the program's filename.

2: A C++ comment that begins the program's description.

3: The program's description continues.

4: The program's description continues.

5: cout needs information in the IOSTREAM.H header file.

6: Extra blank lines make your program more readable.

7: Extra blank lines make your program more readable.

8: All functions have names, and the first function in all C++ programs is main().

9: All functions begin with a left brace.

10: Defines a character array that can hold a string as long as 11 characters plus a terminator.

11: Defines an integer variable that will hold an inventory count.

12: Defines a floating-point variable that will hold a floating-point price per inventory item.

13: Defines a floating-point variable that will hold a floating-point total price of all the items.

14: The first line of a title is printed.

15: The title appears underlined on the screen due to the well-placed hyphens.

16: Extra blank lines make your program more readable.

17: A printed message to describe the program's goal.

18: Two newlines that are a continuation of the previous line.

19: A prompt for the item name.

20: Takes the user's input and stores the item name in a character array.

21: A prompt for the item's inventory count.

22: The item's count is entered.

23: The cost of each item is requested.

24: The user enters the floating-point price per item.

25: A comment describing the upcoming program section.

26: The total inventory price for the item is calculated and stored in totalInv.

27: The dollar values require two decimal places to be printed.

28: An I/O manipulator that ensures a decimal point.

29: An I/O manipulator that ensures fixed point (as opposed to scientific notation) displays.

30: You can continue couts on more than one line, as done here, as long as you break the line at a <<.

31: The cout continues.

32: The program returns to QuickWin.

33: All program functions (main() here) end with a closing brace.



5: Add ample comments at the top of your programs that give a brief description of the code.

22: Notice how the item name, obtained in line 20, is embedded in the prompt.


Previous Page Page Top TOC Next Page