im havin trouble with this code...if anyone could help that be nice

This is a discussion on im havin trouble with this code...if anyone could help that be nice within the C++ Programming forums, part of the General Programming Boards category; im havin trouble with gettin this code to work...it will compile but im not gettin the results i want... heres ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    1

    im havin trouble with this code...if anyone could help that be nice

    im havin trouble with gettin this code to work...it will compile but im not gettin the results i want...

    heres what the code looks like:

    #include <iostream.h>
    #include <iomanip.h>

    const int MAX=10;
    const int INPUT_SENTINEL=0;
    int a,b,m,g,results;
    void printheader();

    void main ()
    {

    cout<<"***************************"<<endl;
    cout<<"**Gas Calculation Program**"<<endl;
    cout<<"***************************"<<endl;
    cout<<endl<<endl;
    cout<<"Please enter the starting mileage: ";
    cin>>a;
    cout<<"To stop the reading press 0 for miles and gallons....to see results type in results for miles."<<endl;
    cout<<endl;
    cout<<"What was your stopping point in miles: ";
    cin>>m;
    cout<<endl;
    cout<<"How many gallons of gas did you need to travel this far: ";
    cin>>g;
    cout<<endl;





    if ((m<0) && (g<0))
    {
    cout<<"Please enter in a positive integer for miles and gallons."<<endl;
    }
    else
    if((m>0) && (g>0))

    {

    cout<<"To stop the reading press 0 for miles and gallons."<<endl;
    cout<<endl;
    cout<<"What was your stopping point in miles: ";
    cin>>m;
    cout<<endl;
    cout<<"How many gallons of gas did you need to travel this far: ";
    cin>>g;
    cout<<endl;
    }



    return;
    }

    void printheader ()
    {
    cout<<endl;
    cout<<setw(15)<<"Odometer"<<setw(5)<<"Odometer"<<s etw(5)<<"Fuel"<<setw(5)<<"Miles"<<setw(5)<<"Fuel"< <setw(5)<<"Miles"<<setw(5)<<"Mpg"<<setw(5)<<"Mpg"< <endl;
    cout<<setw(15)<<"(begin)"<<setw(6)<<"(end)"<<setw( 8)<<"(tank)"<<setw(7)<<"(tank)"<<setw(7)<<"(trip)" <<setw(7)<<"(trip)"<<setw(7)<<"(tank)"<<setw(7)<<" (trip)"<<endl;
    cout<<endl;
    cout<<"Miles: "<<b<<endl;
    cout<<"Gallons: "<<g<<endl;
    cout<<endl;
    cout<<setw(18)<<a<<setw(11)<<b<<setw(13)<<g<<setw( 9)<<endl;
    }

    i think the problem may be in the if else loop but im not totally sure....if anyone out there can help me send me a PM b4 950 am PST....thanks to those who could help

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Once you've read this:
    << !! Posting Code? Read this First !! >>
    edit your original post and apply code tags thanks.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    change the "&&" in the if statement (first part of it) to "||" is at least one thing

    also change "void main()" to "int main()" and then "return 0" at the end.

    also it sounds like you are trying to loop until they enter 0, but there is no loop...you could use a do...while() loop and use check whether m and g are equal to zero for the condition
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What results are you getting/expecting?

    An if/else construction isn't a loop, just a control structure. Also, it looks like printheader ( ) is never called.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. trouble understanding collision detection code
    By silk.odyssey in forum Game Programming
    Replies: 5
    Last Post: 06-16-2004, 02:27 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21