Thread: Help on a simple functions program

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    Help on a simple functions program

    It seems that my code is way off.

    I get no errors from this code, but the output is...er...VERY interesting.

    Code:
    //Cube volume calculating program v2.0
    //calculates the difference between two 3-dimensional objects
    
    typedef unsigned short USHORT;
    
    #include <iostream>
    
    using namespace std;
    
    USHORT areaOfObj1(USHORT length, USHORT width, USHORT height);
    USHORT areaOfObj2(USHORT length, USHORT width, USHORT height);
    
    int main() {
        cout << "\t\tCube Volume Calculating Program v2.0" << endl;
        cout << "\t   Calculates the difference between the volume" << endl; 
        cout << "\t       of one cube to the volume of another." << endl;
        
        USHORT Obj1length;
        USHORT Obj1width;
        USHORT Obj1height;
        USHORT Obj2length;
        USHORT Obj2height;
        USHORT Obj2width;
        USHORT Obj1volume;
        USHORT Obj2volume;
        USHORT Obj1and2dif;
        
        Obj1volume = areaOfObj1(Obj1length, Obj1width, Obj1height);
        Obj2volume = areaOfObj2(Obj2length, Obj2width, Obj2height);    
        
        cout << "\n\tWhat is the length of the first cube?" << endl;
        cin >> Obj1length;
        cout << "\n\tWhat is the width of the first cube?" << endl;
        cin >> Obj1width;
        cout << "\n\tWhat is the height of the first cube?" << endl;
        cin >> Obj1height;
        cout << "\n\tWhat is the length of the second cube?" << endl;
        cin >> Obj2length;
        cout << "\n\tWhat is the width of the second cube?" << endl;
        cin >> Obj2width;
        cout << "\n\tWhat is the height of the second cube?" << endl;
        cin >> Obj2height;
        
        Obj1and2dif = Obj1volume - Obj2volume;
        cin.get();
        
        cout << "The volume of the first cube is " << Obj1volume << endl;
        cout << "The volume of the second cube is " << Obj2volume << endl;
        cout << "\nThe difference between the volume of" << endl;
        cout << "the first cube and the second cube is: " << Obj1and2dif << endl;
        
        cin.get();
        return 0; 
    }
    
    USHORT areaOfObj1 (USHORT l, USHORT w, USHORT h) {
           cin.ignore();
           cin.get();
           return l * w * h;
    };
    
    USHORT areaOfObj2 (USHORT l, USHORT w, USHORT h) {
           cin.ignore();
           cin.get();
           return l * w * h;
    };
    I'm making logical errors. Please help me! Thanks!
    Last edited by blankstare77; 08-13-2005 at 10:12 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are calculating the volume before you ask the user to input the dimensions. You should probably wait until you have the dimensions before using them. If you initialize your variables which is a good habit (to 0 in this case), then you would have noticed that your volume was always 0.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try reading the values of Obj1Length and other variables BEFORE you pass their values to the functions that compute the volumes.

    The reason you'll get interesting output is that variables are not initialised to anything in particular unless you initialise them (or reassign them to something valid).

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Thanks guys! Stupid mistake.

    Appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Program skips functions in main
    By En-Motion in forum C++ Programming
    Replies: 5
    Last Post: 02-18-2009, 09:35 PM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  4. Simple program with list
    By SebastianB in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 10:41 PM
  5. Help please: program using functions
    By ZeroBurn7 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2005, 05:38 PM