Improoving code

Jan 7, 2011 at 10:10pm
Hi im new to C++, trying to learn by reading the book. In the end of each chapter there are problems to solve. Im up to the loops. Is there any better way to clean up my code ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Ffirst.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	
	int nMiles, nTotalM=0;
	float fGallons=0, fTotGal=0;

	
	while (static_cast<int>(fGallons) != -1) {

		cout << "Enter the gallons used (-1 to end:) ";
		cin  >> fGallons;

	
		if (fGallons != -1){
			cout << "Enter the miles driven: ";
			cin  >> nMiles;

			cout << "The miles / gallon for this tank was " << nMiles / fGallons << endl;
			nTotalM += nMiles;  
			fTotGal += fGallons;
		}
	}
		
		if (fGallons != -1)
			cout << "The over all average miles / gallon was :" << nTotalM / fTotGal;

	return 0;
}





Thanks
Last edited on Jan 7, 2011 at 10:11pm
Jan 7, 2011 at 10:45pm
I'd like to know, myself. I'm no expert in any way, but I think it looks good. Here are two things you can do:

* Add a check so that the user can't pass negative values to fGallon (other than -1).
* Add a check so that the user can only enter numbers (try entering letters and see what happens!).
Last edited on Jan 7, 2011 at 10:47pm
Jan 7, 2011 at 10:51pm
I cleaned your code up some. Normally you want a condition before the while loop, so it has something to check (pre-test) And the if statements are not necessary, because if it fails the while loop, it never gets to the if statement(s).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main()
{
	
	int nMiles, nTotalM=0;
	float fGallons=0, fTotGal=0;

	std::cout << "Enter the gallons used (-1 to end:) ";
	std::cin  >> fGallons;

    while (static_cast<int>(fGallons) != -1) {

		std::cout << "Enter the miles driven: ";
		std::cin  >> nMiles;
		std::cout << "\nThe miles / gallon for this tank was " << nMiles / fGallons << std::endl;
		std::cout << std::endl;
		nTotalM += nMiles;  
		fTotGal += fGallons;

		std::cout << "Enter the gallons used (-1 to end:) ";
		std::cin  >> fGallons;
	}//end while loop
		
	return 0;
}
Jan 7, 2011 at 10:57pm
* Make a check so users cannot enter negative numbers for miles. (No going back in time!)
* Make it so when a user enters a negative number for fGallon the program ends.
* Avoid a divide by 0 error!
* Compare a float without using == or != (They can be glitchy with these!)
* Display the miles per gallon ratio whenever I enter a -1 for miles!

Try these?
Jan 7, 2011 at 11:01pm
Thanks, i guess i didnt think thru properly where to ask a user to input data. This makes sense.
Thanks for suggestions, but right now i just want to implement exactly what the problem asks, Checking for div by 0 ill implement.
Last edited on Jan 7, 2011 at 11:06pm
Jan 7, 2011 at 11:23pm
Crutoy: Don't worry, when you're just starting out you miss these things. You learn as you go along =)
Topic archived. No new replies allowed.