Can somebody help me coding this c++ problem

Discussion in 'Programmer's Corner' started by iNgine, Mar 12, 2011.

  1. iNgine

    Thread Starter New Member

    Mar 12, 2011
    3
    0
    i am ask to write a program that calculates miles per gallon for a list of cars. The data for each car consists of initial odometer reading, and a number of gallons of gas. The user signals that there are no more cars by entering a negative initial odometer reading.

    the console output must look like this:

    Miles Per Gallon Program
    Initial miles:
    15000
    Final miles:
    15250
    Gallons
    10
    Miles per Gallon: 25.0

    Initial Miles:
    107000
    Gallons
    15
    Miles per Gallon:30.0

    Initial miles:
    -1
    bye


    i hope somebody can help me :(
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    990

    And exactly what have you done so far? Or do you expect someone to do all of your homework for you. Do you have the permission from your instructor to have someone else you your work?
     
  3. iNgine

    Thread Starter New Member

    Mar 12, 2011
    3
    0
    I already try coding it but unfortunately my code didn't run successfully and got many errors that's why Im asking for somebody to help me.
     
  4. iNgine

    Thread Starter New Member

    Mar 12, 2011
    3
    0
    here is the one that i code, i got one error on it, i don't know where i got it wrong pls. help me spotting that error.

    Code ( (Unknown Language)):
    1. #include<iostream.h>
    2. int main()
    3. {
    4.     int im,fm,g,mpg;
    5.     cout<<"Miles Per Gallon Program\n";
    6.     {
    7.     do
    8.     cout<<"Initial miles:\n";
    9.     cin>>im;
    10.     while(im>0)
    11.     cout<<"Final Miles:\n";
    12.     cin>>fm;
    13.     cout<<"Gallons:\n";
    14.     cin>>g;
    15.     mpg=(fm-im)/g;
    16.     cout<<"Miles per gallon: "<<mpg<<"\n\n";
    17.     }
    18.     cout<<"bye\n";
    19.     return 0;
    20. }
     
  5. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    What is the error message you get?
     
  6. debjit625

    Well-Known Member

    Apr 17, 2010
    790
    186
    You are very new to C++ ,you need to know many things first...
    In language "C" we used this syntax to include header files
    Code ( (Unknown Language)):
    1.  
    2. #include "stdio.h"
    3.  
    But in C++ we use this syntax to include header files
    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3.  
    with these angle brackets don’t use ".h" extension (we can but thats another story)

    Now ,you used two objects from standard C++ Lib i.e. “cout” and “cin” they both
    comes under standard namespace and you should be using the std namespace for
    that some thing like this…
    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. using namespace std;
    4. int main()
    5. {
    6. int u = 0;
    7. cout<<"Input any number\n";
    8. cin>>u;
    9. cout<<"Your input was : "<<u<<endl;
    10.  
    11. return 0;
    12. }
    13.  
    Last thing ,look at your code how you used “do” “while” loop,it should be used
    like this
    Code ( (Unknown Language)):
    1.  
    2. #include<iostream>
    3. using namespace std;
    4. int main()
    5. {
    6. int u = 0;
    7. do
    8. {
    9.   cout<<u++<<endl;  //Here add your looping code
    10. }
    11. while(u!=100);    //Here you check for condition.
    12. return 0;
    13. }
    14.  
    Always initialize your local variables
    Code ( (Unknown Language)):
    1.  
    2. int im = 0,fm = 0,g = 0,mpg = 0;
    3.  
    Good luck
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    990
    Looks like your starting { is in the wrong place and you have no branch decision.

    i = 0;
    do
    {

    i++;

    } (while i<=1000);

    Blocks are basis stuff. What have you been doing during class?
     
    Last edited: Mar 13, 2011
Loading...