Help me C++

Hey L1
So i just enrolled myself into a hybrid C++ course in college am so far very intrigued in the potential for programming.

How about you do the work, and then someone will check it?
I don't have a clue how to code, but I do know how to learn and that isn't by looking at someone else work but by doing it yourself and making mistakes.

9 Likes
/*
10. Miles per Gallon
This program finds the MPG of a car.

A Car holds 15 gallons of gasoline and can travel 375 miles before refueling. Write a program that calculates the number of miles per gallon the car gets. Display the result on the screen.
Hint: Use the following formula to calculate miles per gallon (MPG)
MPG = Miles Driven/Gallons of Gas Used


*/


#include <iostream>
using namespace std;

int	main();
{

	const	int	tank_capacity =15 //gallons
	const	int	car_total_miles_per_tank=375

	cout << "The MPG of the car is"car_total_miles_per_tank/ tank_capacity "miles per gallon" << endl;

	cin.get(); //stops result screen (console)
	return 0;
}
1 Like

Hopefully someone will help check out your work. Thumbs up!

if you truly want to be a programmer, you need to put a bit more effort in it. This doesn't even compile. Start by using the compilers error messages as help to fix your program. After that you can start looking at logic errors.

And if the haven't given you any info yet on how to compile, just save the program in a file, car.cpp for example.. And (if you use gcc) type this on command line:
g++ car.cpp

Also, I would recommend getting a C++ book like "C++ Primer Plus" by Stephen Prata (or something similar). And read that while taking your course.


// ConsoleApplication1.cpp : Defines the entry point for the console application.
//


#include <iostream>

using namespace std;



int main()
{
int tank_capacity = 15;
int car_total_mpt = 375;

cout << "The MPG of the car is " << (car_total_mpt / tank_capacity) << " Miles per Gallon" << endl;

double percent = 1.35;
double product_value = 14.95;

cout << "The price of the good is $" << product_value * percent << endl;


return 0;
}

Here, I did the first bit for you. Now learn how it works, why it works and learn how to learn. You're not going to get far at all unless you can learn how to learn. This is programming, Anyone can learn a language but you're only a coder if you know the language, a programmer is always learning and if you're at all serious about this, you should teach yourself to hammer away at a problem untill you understand it, because if you can't learn this, you have no hope in hell learning basic window programming, linked lists, matrices and vectors. You really have to give it your all or you're not going to get far

3 Likes

Is an int the right varable for fuel. I know the problem uses whole numbers. But fuel it can come in 0.5. Driving doesnt clock in at whole numbers either. Futue proof the code.

1 Like

This.

Also make it more usable.

Make it a program where you can pass the tank size and the mileage as variables.

That way you can apply the program to any car or vehicle.

relax

this is my first week of class

1 Like

That's not the issue people are seeing here. The issue is the way you're going about learning. You need to learn how to research. This stuff is not hard at all and if you don't learn how to teach yourself early on, you won't get far.

Trust me, I'm in my third year of Computer and Mobile Engineering and the people who couldn't teach themselves dropped out after the first year. While people I know who struggle a lot with the topic but try hard to learn are still hanging on.

3 Likes

Additional tip;

Don't use std::endl unless you need to, You can use "\n" for just a new line.

If you have questions search! Will take more time but you will learn a lot more. :)

A friend; http://en.cppreference.com/w/

If you find just reading up books and stuff confusing for start, maybe try this too; https://www.sololearn.com/Play/CPlusPlus/

1 Like

You can still use ints though for accuracy. For example you could have 1000092 and that could represent 10000.92 . You can get into some problems using floats as computers might add a number to 1.9999999 instead of 2.

Should really use floats all around in the MPG problem to avoid truncation.

thanks, im just reading my book rn
And actually i was not even anticipating cpp to be this amusing but it is...

I dont intend to come out a programmer but i could see the potential for myself using this and installing home automation into peoples homes.

till then, I will remain poor and continue going to community college

2 Likes

Now, learning programming languages is almost free if you already have a computer and Internet. And in many cases it's more flexible and "interesting" to learn. So don't worry about being poor and stuff. ;)

I figured it out

the answer is 25mpg

	Miles per Gallon
This program finds the MPG of a car.

A Car holds 15 gallons of gasoline and can travel 375 miles before refueling. Write a program that calculates the number of miles per gallon the car gets. Display the result on the screen.
Hint: Use the following formula to calculate miles per gallon (MPG)
MPG = Miles Driven/Gallons of Gas Used


*/

#include <iostream>

using namespace std;



int main()
{
	int tank_capacity, car_total_mpt,mpg;
	car_total_mpt = 375;
	tank_capacity = 15;
	mpg = (car_total_mpt / tank_capacity);


	cout << "The MPG of the car is " << mpg << " Miles per Gallon" << endl;



	cin.get(); //stops result screen (console)
	return 0;

}

anon Just wondering why you included percent? The question does not call for the cost of the gas… endl;

btw thanks for the help

anon it seems did myself a learning…

/*THIS PROGRAM DETERMINES THE PRICE OF A CIRCUT BOARD!
Circuit Board Price
A electonic company sells circuit boards at a 35 percent profit. Write a program that will calculate the selling price of a circuit board that costs $14.95. Display the result on the screen.
*/

#include <iostream>
using namespace std;

	int	main()
{

		//variables
		float profit_margin, circut_board, percentage, ultimate_cost;
		
		profit_margin = .35;						//
		circut_board = 14.95;						//
		percentage = (circut_board * profit_margin);// (14.95*.35)
		
		ultimate_cost = (circut_board + percentage);
		
		
		//Displays Output visually
		cout << "The selling price of a circut board that costs $";
		cout << circut_board << " is\n" << "$" << ultimate_cost << endl;

		cin.get(); //stops result screen (console)
	return 0;
}

Second question...

1 Like

gotcha…

I programmed the last question, and it failed to compile… can you take a look at this->

/*14. Personal Information
Write a program that displays the following pieces of information, each on a separate line :
Your name
Your address, with city, state, and ZIP code
Your telephone number
Your college major

Use only a single cout statement to display the following pattern on the screen
*/

#include <iostream>
using	namespace std;

int main()
{
	// Variable declaration:
	string firstname, m_initial, lastname, full_name, address, college_major, streetname, city, state, address;
	int zip, telephone_number;
	char s, streetnum, m_initial;



	// Variable definition:
	firstname = " Ed ";									// First
	m_initial = 'M';									// Middle Int.
	s = ' ';											// Adds a space
	lastname = " Exampleton ";							// Last
	full_name = (firstname + m_initial + lastname); 	// FULL NAME
	
	streetnum = 1;
	streetname = " street ";
	city = " Los Angeles ";
	state = " California ";
	zip = 0575723;
	address = (streetnum + streetname + city + state + zip);
	
	telephone_number = 12345678;
	college_major = " Computer Science; Networking Path";




	// Visual output
	cout << "Hello my name is" << full_name << "\nI live at" << address << " . My phone number is\n" << telephone_number << "\n and my college major is" << college_major << endl;

	

	cin.get(); //stops result screen (console)
	return 0;
}
1 Like