Thread: programs off the tech school's hard drive

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    programs off the tech school's hard drive

    I'm on the school's network hard drive, looking around, out of curiousity for what they use in their C++ class. Visual Basic.NET isn't a challenge for me, so it's discouraging and that is why I'm currently getting bad marks until I make some stuff up/settle down.

    Anyhow, I'm going to post a few programs here from the drive, and I want anybody who can find errors, etc. with the programs to post them. I can see a few, but this is more or less a thought of, I'd take the course, as it would be fun for me since I do c & c++ during my free time, but, I think this particular course is a bit dodgy.

    loop example:
    Code:
    #include <iostream.h>
    void main(void)
    // This program calculates the average
    // of five user-entered numbers.
    {
      int count;
      float num, total, average;
    
      total = 0.0;
    
      for (count = 0; count < 5; count++)
      {
        cout << "Enter a number: ";
        cin >> num;
        total = total + num;
      }
    
      average = total / count;
      cout << "\nThe average of the data entered is " << average;
    }
    array example:
    Code:
    /*		Using an array to BUILD PRICE */
    
    /* Include Files */
    #include <iostream.h>
    
    void main()
    {
    const int ARRAY_NUM	= 2;
    int price[ARRAY_NUM] = {0};
    float quantity[ARRAY_NUM];
    float amount[ARRAY_NUM];
    int incr;
    
    //magic formulas for printing decimal points
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    
    // Display directions
    cout << "*********************************************************************";
    cout << "\n                    The Great Widget Company";
    cout << "\n*******************************************************************";
    cout << "\nThis program will ask you for prices and quantity of Widgets";
    cout << "\nThen the program will display a table showing quantity, Price,";
    cout << "and the amount of your purchases";
    cout << "\n*******************************************************************\n\n";
    
    /* Building array */
    	for (incr = 0; incr < ARRAY_NUM; ++incr)
    	{
    		cout << "Enter a price:  ";
    		cin >> price[incr];
    		if (price[incr] == 9999)
    				break;
    		else
    		{
    		cout << "\nEnter the quantity purchased:  \n";
    		cin >> quantity[incr];
    		amount[incr] = quantity[incr];
    		}
    	}
    
    // Printing the table headings
    cout << "\n-----------------------------------------------------------------------";
    cout << "\n                       Purchasing Report";
    cout << "\n  Quantity                   Price             Amount";
    cout << "\n  --------                   -----             ------\n";
     
    // Print the array
    	for (incr = 0; incr < ARRAY_NUM; ++incr)
    	{
    		
    		cout <<  "      " << quantity[incr] << "                    " << price[incr];
    		cout << "               " << amount[incr] << "\n";
    	}
    cout << "\n\n Thank you for Using Great Widgets\n";
    }
    function example:
    Code:
    /*		This program has two functions: Main and total_cost */
    
    #include <iostream.h>
    
    //function prototype 
    void total_cost(int, double);
    
    //Global variable
    double subtotal;
    
    void main( void )
    {
    	double price;
    	int number;
    
    
    	cout << "Enter the number of items purchased: ";
    	cin >> number;
    	cout << "Enter the price per item $";
    	cin >> price;
    
    	// function call
    	total_cost(number, price);
    	
    	//magic formulas for printing decimal points
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(2);
    	cout << number << " item at "
    		 << "$" << price << " each.\n"
    		 << "Final bill, including tax, is $" << subtotal
    		 << endl;
    }
    //function heading
    void total_cost(int number_par, double price_par)
    {
    	const double TAX_RATE = 0.05; //5% SALES TAX
    	
    	subtotal = price_par * number_par;
    	subtotal = subtotal + (subtotal * TAX_RATE);
    }
    The world is waiting. I must leave you now.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    int main( )
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > #include <iostream.h>
    #include <iostream>
    using namepsace std;
    The world is waiting. I must leave you now.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Shadow
    > #include <iostream.h>
    #include <iostream>
    using namepsace std;
    http://www.cpp-home.com/faq/faq.pl?20
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I use this method:
    Code:
     
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main( )
    {
       cout << "Hello World!" << endl;
       return 0;
    }
    It doesn't make much difference in small program, but its handy for larger programs where you will be typing std::cout hundreds of times. For objects not used so much I just put std in front of it, eg.
    Code:
    std::vector<int> inVector;
    One other thing, I put argument identifers in function prototypes. It makes it easier to understand what the function does and why it need those arguments in my opinion
    Last edited by endo; 11-04-2002 at 09:38 AM.
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detect SCSI Hard Drive Serial Number
    By mercury529 in forum Windows Programming
    Replies: 3
    Last Post: 10-17-2006, 06:23 PM
  2. 'Installing' to hard drive and 'Saving' to hard drive
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2002, 02:37 PM
  3. Getting the hard drive serial number?
    By kes103 in forum Windows Programming
    Replies: 2
    Last Post: 01-18-2002, 03:03 PM
  4. Getting the hard drive serial number?
    By kes103 in forum C Programming
    Replies: 2
    Last Post: 01-18-2002, 01:43 PM
  5. read hard drive
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-20-2001, 05:38 PM