Can somebody help me coding this c++ problem

Thread Starter

iNgine

Joined Mar 12, 2011
3
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 :(
 

spinnaker

Joined Oct 29, 2009
7,830
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 :(

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?
 

Thread Starter

iNgine

Joined Mar 12, 2011
3
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.
 

Thread Starter

iNgine

Joined Mar 12, 2011
3
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.

Rich (BB code):
#include<iostream.h>
int main()
{
    int im,fm,g,mpg;
    cout<<"Miles Per Gallon Program\n";
    {
    do
    cout<<"Initial miles:\n";
    cin>>im;
    while(im>0)
    cout<<"Final Miles:\n";
    cin>>fm;
    cout<<"Gallons:\n";
    cin>>g;
    mpg=(fm-im)/g;
    cout<<"Miles per gallon: "<<mpg<<"\n\n";
    }
    cout<<"bye\n";
    return 0;
}
 

debjit625

Joined Apr 17, 2010
790
You are very new to C++ ,you need to know many things first...
In language "C" we used this syntax to include header files
Rich (BB code):
#include "stdio.h"
But in C++ we use this syntax to include header files
Rich (BB code):
#include <iostream>
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…
Rich (BB code):
#include <iostream>
using namespace std;
int main()
{
int u = 0;
cout<<"Input any number\n";
cin>>u;
cout<<"Your input was : "<<u<<endl;

return 0;
}
Last thing ,look at your code how you used “do” “while” loop,it should be used
like this
Rich (BB code):
#include<iostream>
using namespace std;
int main()
{
int u = 0;
do
{
  cout<<u++<<endl;  //Here add your looping code
}
while(u!=100);    //Here you check for condition.
return 0;
}
Always initialize your local variables
Rich (BB code):
int im = 0,fm = 0,g = 0,mpg = 0;
Good luck
 

spinnaker

Joined Oct 29, 2009
7,830
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.

Rich (BB code):
#include<iostream.h>
int main()
{
    int im,fm,g,mpg;
    cout<<"Miles Per Gallon Program\n";
    {
    do
    cout<<"Initial miles:\n";
    cin>>im;
    while(im>0)
    cout<<"Final Miles:\n";
    cin>>fm;
    cout<<"Gallons:\n";
    cin>>g;
    mpg=(fm-im)/g;
    cout<<"Miles per gallon: "<<mpg<<"\n\n";
    }
    cout<<"bye\n";
    return 0;
}
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:
Top