Thread: Need help with my first program

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Need help with my first program

    enrolled in my first c++ class and have a homework assignment due. my problem is that i declared my variables and wrote what i think is a good start for the program. my problem is that when i enter the information that i want, my actual output does nothing. i cant seem to get my program to do the math it is supposed to. can someone look at this and see what i am missing. this is just part of the project. i want to get on the right track before i write the rest. i just want this to figure the old monthly cost. i use Dev C++. thank you.

    Code:
    
    #include <iostream>
    #include <cstdlib>
    #include <cfloat>
    
    using namespace std;
    
    int main(int argc, char *argv[]) {
    
    
    float CostOfNewCar; //New vehicle price
    float MonthlyMilesDriven; //Mile driven per month
    float NewVehicleMPG; //New vehicles mile per gallon of gas
    float OldVehicleMPG; //Old vehicles miles per gallon of gas
    float CurrentMonthlyCost; //Current monthly cost that a persons spends
    float ProposedMonthlyCost; //Proposed monthly cost that person will spend
    float PriceOfGas; //Enter the current price of gas per gallon
    float NewCarPayment; //New car payment cost
    float OldMonthlyGasExpense; //Old vehicle gas expense per month
    float NewMonthlyGasExpense; //New vehicle gas expense per month
    float OldCarPayment; //Old car payment
    
    // Prompt user for information
    
    cout << "How many mile do you drive a month?:" ;
    cin >> MonthlyMilesDriven;
    
    cout << "Current vehichles MPG :" ;
    cin >> OldVehicleMPG;
    
    cout << "How much is gas per gallon:" ;
    cin >> PriceOfGas;
    
    
    
    
    
    
    
    
    // Calculations
    
    NewCarPayment = CostOfNewCar / 60 ;
    OldCarPayment = 130.00;
    OldMonthlyGasExpense = MonthlyMilesDriven / OldVehicleMPG * PriceOfGas;
    CurrentMonthlyCost = OldMonthlyGasExpense + OldCarPayment;
    
    
    
    
    
    // Output results
    
    cout << "Currently monthly cost: ";
    cin >> CurrentMonthlyCost;
    
    
    
    
    
    
    
    system("pause");
    
    
    return(0);
    
    
    
    
    }
    Last edited by Salem; 09-18-2008 at 10:51 PM. Reason: Restore original - please don't delete your posts as soon as you get an answer, no one else can learn (or contribute)

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first, you should always initialize your variables. this means assigning a value to your variable--even if you dont think you need to. for example, in your code you have:
    Code:
    NewCarPayment = CostOfNewCar / 60 ;
    however CostOfNewCar was never initialized, so the result of this is undefined. one time you run the program, CostOfNewCar may have the value of 10, another time it may have the value of -999.12345. maybe you need to add some code to ask for this information from the user, and of course read it in?

    now for the problem your referring to, its pretty simple, your pretty much there!
    Code:
    // Output results
    
        cout << "Currently monthly cost: ";
        cin >> CurrentMonthlyCost;
    notice in the second line your telling the computer "receive input and store it in the CurrentMonthlyCost variable". you probably want to instruct the computer to "output the value of the CurrentMonthlyCost variable". cout is for output and cin is for input.

    edit: also, for the code above you only need the first include statement (iostream). if you know your going to need the other two headers for further development of your project, then you of course must leave them in, but otherwise get rid of them.
    more importantly, you should not use something like "system("pause")", as this immediately restricts your program to Windows operating systems. if you submit your source code for marking to your teacher who could be running Linux and he/she tried to compile and then run it, the output would very likely contain an error message saying that "pause" cannot be found. instead of using a windows-specific function, you could do a simple "cout" saying to press enter to exit, and a simple "cin" to accept the enter (into a dummy variable), which would work on any operating system.
    Last edited by nadroj; 09-18-2008 at 08:37 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    thanks alot. i think im on the right track now. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM