Homework help

Well I know it said not to ask about homework but I am soo lost at the moment.

I am being asked to write a program that prompts the capacity, in gallons of an automobile fuel tank and the miles per gallons the automobile can be driven. The program outputs the number of miles the automobile can be driven without refueling.

A nudge in the right direction would be great and much apperciated.
How would you do this on paper? After you figure that out you can try to translate that into code of some sort.
mpg*gallon = miles

turn it into code.. should take about 2 minutes.
Sorry forgot to reply back thanks for the help guys; after you let me know that I felt like a dumb dumb.
1
2
3
4
float calc_miles (float mpg, float gallons)
{
    return mpg * gallons;
}


Now make a main(), add your cout and cins and call this function. Voila.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdlib.h>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
  double gallons, mpg, miles;
  
  if (argc != 3) {
    cout << "Usage: prog GALLONS MILES_PER_GALLON\n";
    return -1;
  }
  
  gallons = strtod(argv[1], NULL);
  mpg = strtod(argv[2], NULL);
  miles = gallons * mpg;

  cout << miles <<  endl; 

  return 0;
}


It's my first post here. And perhaps it's not very good to give a ready solution for this kind of problem. But I'm a novice too and I'm interested whether there is a more C++ way to convert program arguments ( c strings) to numbers? And whether it is bad (I mean very bad) to use "using namespace std"?

Thank you.
Last edited on
yazu, I can see quite a few things wrong with that code.

Start up a new thread and the community will be able to help you out!
Last edited on
@yazu, here is something a little more basic:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
  double gallons, mpg, miles;
  
  cout << "Enter the gallons in the tank: "
  cin >> gallons;
  cout << "Enter the MPG of the vehicle: "
  cin >> mpg;

  miles = gallons * mpg;

  cout << "Miles available: " << miles <<  endl; 
  cin.get()
  return 0;
}
Last edited on
Ben, what exactly are you seeing wrong with that piece of code yazu posted? Granted, I would have prompted the user for the values during runtime, but that code requires the user to input the values as parameters to the program. What is wrong with it?
Well, after reading some first chapters from some books I can see only one real problem - using <stdlib.h> instead of <cstdlib>.

I don't bother (at least yet) about "using namespace std", using c functions, using 0 and -1 instead of EXIT_SUCCESS (or how it is exactly). And about user interface - it sucks in both cases, so it doesn't matter. But yes, it simpler to prompt and get data from stdin. Thank you, Stewbond.
Last edited on
using namespace std; is almost an oximoron, because in a way it defeats the purpose of a namespace. A namespace provides a way to avoid having conflicting names among variables, functions, classes etc. However whenever you say using namespace std you will get an error if you were to for example, make the following statement:
 
char endl = '\n';

The compiler would become confused whenever you try to use the variable endl, because it conflictts with the std namespace. There are two ways to avoid this. First, instead of using the entire standard namespace, just use what you need from it. If you're writing a program that only needs to use cout, endl, strings and ios flags instead of writing using namespace std you could write:
1
2
3
4
5
using std::cout;
using std::endl;
using std::string;
using std::ios;
using std::iosflag;

This avoids unnecessary conflict between names with things you don't intend to use. The second (and in my opinion, better) way to go about this problem is to prepend everything from the std library with std::. Things would be written as such:
1
2
3
4
5
string string;  // old way, note that this would cause errors because the names would conflict
std::string string;  // new way, which causes no errors

cout << "hello, world!" << endl;  // old way
std::cout << "hello, world!" << std::endl;  // new way 


Hopefully you get the idea now :) I'd recommend using the latter method, just a little more typing that will save you a lot of pain!
Topic archived. No new replies allowed.