I'm pretty new to programming and not sure what I did wrong but I'm getting the error on line 20, it says that I haven't initialized intownMiles and highwayMiles.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double intownMPG = 23.5;
double highwayMPG = 28.9;
double intownMiles;
double highwayMiles;
double gallons = intownMiles / intownMPG + highwayMiles / highwayMPG;
cout << "Please enter the number of in-town driving miles: " << endl;
cin >> intownMiles;
cout << "Please enter the number of highway driving miles: " << endl;
cin >> highwayMiles;
cout << "The total number of gallons required is: " << gallons << "gal" << endl;
}
Your code:
double intownMiles;
double highwayMiles;
double gallons = intownMiles / intownMPG + highwayMiles / highwayMPG;
You clearly have not initialized those variables before using them. All you did was define them - so they exist, but have indeterminate values until you assign to them (which you never do).
Initialize the variables to sane initial values when you define them and the compiler warning will go away (and your code will no longer have Undefined Behaviour).
Writing C++ is different from writing regular math equations.
Firstly, the code is executed sequentially.
When execution reaches this line:
double gallons = intownMiles / intownMPG + highwayMiles / highwayMPG;
The value of gallons is computed immediately, using the current values of variables used in the expression.
You haven't assigned any values to some of the variables prior to using them, so you can't expect to get a meaningful result.
When you change those variables later, the value of gallons is not affected.
So you have to ask the user for values of those variables first, and then compute the formula.
Related
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main () {
//initializing my variables
double mealcost;
float tax_percent, tip_percent, tax_total, tip_total, overall_total;
cout << "What is the cost of your meal?" << endl;
cin >> mealcost;
cout << "What percent tip would you like to leave?" << endl;
cin >> tip_percent;
cout << "What percent are you taxed?" << endl;
cin >> tax_percent;
tax_total = mealcost * (tax_percent/100);
tip_total = mealcost * (tip_percent/100);
overall_total = mealcost + tax_total + tip_total;
/*trying to take the overall total from the formula above and round it
to the nearest whole integer*/
round (overall_total);
cout << "What is the total cost of my meal? " << overall_total << endl;
return 0;
}
Whenever I run my code it compiles correctly and gives me the correct overall total, but the round function seems to not work. I input 12 dollars for the meal total, 8 percent tip, and 20 percent tax. The correct answer is $15.36, but I'd like to round it down to $15. Any help would be appreciated thanks.
You must assign the return value of the round() function to overall_total, like this:
overall_total = round(overall_total);
The above line should replace round (overall_total);.
Some functions in C++ take a reference (pass-by-reference) to the parameters of the function, e.g. std::sort(), so that you simply std::sort(v.begin(), v.end()) and the vector v is sorted without you having to assign the return value. (Technically, std::sort takes in iterators which have similarities to pointers, but they basically have the same result.)
However, the round() function actually takes a copy (pass-by-value) of the parameter and returns a new value - it does not directly 're-assign' the parameter to have the rounded value. Therefore, you must assign the return value of the function to a variable (or in this case, the same variable in order to 're-assign').
You can learn more about the difference here:
What's the difference between passing by reference vs. passing by value?
#include <iostream> // try to convert height in centimeters to meters
using namespace std;
int main()
{
int height_cm ; // declaring first int in centimeters
cout << " State your height in centimeters: " << endl ; // stating
cin >> height_cm ; /* I was thinking also of storing this value for later reusage in retyping but I don´t know how to do it */
double (height_cm) ; /* here is the problem,debugger says that I can not declare height_cm again because I had declared it as int before , but I´m actually trying to retype it */
height_cm /= 100 ; /* I´m also not sure about this , I think this assignment should be possible to get number in meters */
cout << " Your height in meters is: " << height_cm << endl ; /* this should give me height in meters */
return 0 ;
}
The problem is that, as your compiler is saying, you are trying to use the same name (height_cm) for another variable. Try doing:
...
double height_m = height_cm/100.0;
cout << " Your height in meters is: " << height_m<< endl ;
...
This way the meter variable will have a new name and the compiler will compile. Moreover, note that I divided height_cm by 100.0 instead of 100. This is because 100 is an int and 100.0 is a float or double. If you use int then you would have an int division meaning that you would lose the decimal part.
A part from that:
I was thinking also of storing this value for later usage in retyping but I don´t know how to do it: The cin>>height_cm; code takes whatever the user has typed, converts it to int and stores it in a variable called height_cm that you can use anytime you want in the current function (in this case main()).
I´m also not sure about this, I think this assignment should be possible to get number in meters: That code would compile with no problem. However, this would end up with an int divison. If you want you can do:
Code:
...
double height_m(height_cm);// this converts the int to double and stores it in the new variable height_m
height_m /= 100;// Divide the double variable height_m by 100 and store it again in height_m
...
Note that in this case although you are using 100 instead of 100.0 that would not be an int division because height_m is a double.
I'm trying to get an output for my weight_Fee using double, and I cannot seem to get the correct value. I have tried using float, but I haven't been able to get that to work either.
My goal is to get an output value containing two decimal places as if I were to be calculating a cost, but I get 0.00 every time.
I'm new to C++, so if anyone can tell me what I'm doing wrong, it would be a big help. Thanks.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
double animal_Weight;
double weight_Fee = .5 * animal_Weight;
cout << "In rounded poundage, how much does your animal weigh? ";
cin >> animal_Weight;
cout << setprecision (2) << fixed << weight_Fee;
return 0;
}
double weight_Fee = 0.5 * animal_Weight;
When you initialize weight_Fee like that you are setting it equal to 0.5 * the current value of animal_Weight. Since this is currently undefined weight_Fee will be some garbage value.
When you set animal_Weight to something based on user input later on, that won't change the value of a previous variable. You'll have to use that statement again to set weight_Fee = 0.5 * the current value of animal_Weight
The best thing to do is probably to just declare weight_Fee at the top, and not define it until you have set animal_Weight to what you want it to be.
Something like this:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
double animal_Weight;
double weight_Fee;
cout << "In rounded poundage, how much does your animal weigh? ";
cin >> animal_Weight;
weight_Fee = .5 * animal_Weight
cout << setprecision (2) << fixed << weight_Fee;
return 0;
}
The variable animal_Weight is undefined and can be initialized to anything by the compiler or the operating system or whatever value happen to be last in the memory.
You need to calculate weight_Fee after you input a value for animal_Weight:
double animal_Weight = -1.0;
cout << "In rounded poundage, how much does your animal weigh? ";
cin >> animal_Weight;
double weight_Fee = .5 * animal_Weight;
cout << setprecision (2) << fixed << weight_Fee;
Either someone forgot to mention to you that your computer does only 1 instruction at a time (and the C++ compiler generates instructions in a sequence corresponding to your code); or perhaps you've never grok'ed the statement.
1) double animal_Weight;
2) double weight_Fee = .5 * animal_Weight;
3) cout << "In rounded poundage, how much does your animal weigh? ";
4) cin >> animal_Weight;
5) cout << setprecision (2) << fixed << weight_Fee;
Your code prompts for (3) and cin's (4) an animal weight. ok.
But the weight_Fee was computed (2) prior to knowing the animal_Weight (4). This is a logic error.
So if the computation at (2) did not know the animal_Weight, the correct value simply can not be determined.
Also, the animal_Weight (1) is not initialized, creating undefined behaviour.
Note that you CAN get the compiler to complain about (generate a warning) the attempted use of an uninitialized variable (at line 2), but you have to command the compiler to do so (by using an option).
I am required to fully understand the following code :
#include <iostream>
using namespace std;
double area(double length, double width);
double time(double p_area, double h_area, double mow_rate);
int main() {
double d_plot_length, d_plot_width, d_home_side, d_mow_rate;
double plot_area, home_area, time_taken;
// I've used double for all of these to get the most precise values possible, something I'd only really consider doing on small programmes such as this
cout << "What is the length of the plot? In meters please." << endl;
cin >> d_plot_length;
cout << "What is the width of the plot? In meters please." << endl;
cin >> d_plot_width;
cout<< "What is the size of the side of the house? In meters please." << endl;
cin >> d_home_side;
cout << "What is the rate at which you are going to be mowing? In meters per minute please" << endl;
cin >> d_mow_rate;
// Just getting all the data I need from the user
plot_area = area(d_plot_length, d_plot_width);
home_area = area(d_home_side, d_home_side);
time_taken = time(plot_area, home_area, d_mow_rate);
cout << "It will take " << time_taken << " minutes to mow this lawn. Better get cracking" << endl;
return 0;
}
double area(double length, double width) {
double value;
value = length * width;
return value;
}
double time(double p_area, double h_area, double mow_rate) {
double value;
value = (p_area - h_area) / mow_rate;
return value;
}
I am struggling to understand how the time() function works.
So far I understand that :
time_taken , gets its value from the time() function: time(plot_area, home_area, d_mow_rate).
The time() function gets its values from the function declaration at the bottom.
double time(double p_area, double h_area, double mow_rate) {
double value;
value = (p_area - h_area) / mow_rate;
return value;
}
However, this is where I'm stuck. The user is asked to enter values for d_plot_length, d_plot_width, etc. So I cannot understand how the compiler knows what these values p_area, and h_area actually are.
I realise that somehow the area() function is being used to aid the time() function, but as far as I'm aware the variables P_area etc within the time() function do not have values assigned to them.
Please can someone fill in the gaps in my understanding.
To be more precise, I want to know exactly how time_taken is displayed on the screen, from the start of the process, to the cout. Like I say I am familiar with most areas but not all.
In your program, you had computed the following values:
plot_area = area(d_plot_length, d_plot_width);
home_area = area(d_home_side, d_home_side);
When the method area(double,double) is invoked, the resultant double value gets stored in these variables.
Then you have the function call:
time_taken = time(plot_area, home_area, d_mow_rate);
This is the call by value type of function invocation. A copy of the values in the variables, plot_area, home_area and d_mow_rate are passed to the function. In the time(double, double, double) the computing is done upon the basis of the logic you had defined in this method and the resultant value is returned to the function call in the main() method.
Please note that the function call is of call by value and hence only a copy of the values are passed to the arguments mentioned in the function time(double, double, double) even though the variable names are the same in the main() and in the function call.
For further reading, I will suggest you to have a look at the following links:
Call By
Value
Call By
Reference
Call By
Pointer
Am working on a small problem and have spent quite a few hours trying to figure out what I did wrong. Using Dev++ compiler which at times has some cryptic error messages.
I tried to make the Volume calculation a function and got it to work but I have 2 small nits. Will work on error checking after I resolve this.
With the function added, for some reason with dev++ now, the program does not pause (press any key to continue).
Volume is coming up with blank instead of a number.
Thanks
PC
// The purpose of this program is to determine the Volume of a
// square-based pyramid after the user inputs the Area and
// the Height.
#include <iostream>
#include <iomanip>
using namespace std;
double calcvolume(double a, double h)
{
double volume;
volume = ( a * h ) / 3;
return (volume);
}
int main()
{
double area, height, volume; // declare variables
cout << "Please enter the Area of the Square-based pyramid:"; // requests users input
cin >> area; // assigns user input to area
cout << "Please enter the Height of the Square-based pyramid:"; // requests user input
cin >> height;
// assigns user input to height
cout << "Area= " << area << "\n"; // Prints user input for area
cout << "Height= " << height << "\n";
calcvolume(area,height);
cout << "Volume= " << fixed << showpoint << setprecision(2) << volume << "\n"; // Prints resolution to the formula stored in volume
system("pause"); // forces DOS window to pause to allow user to utilize program
return 0;
}
Your updated code looks correct, but you aren't storing the calcvolume return value. The volume variable you declare in calcvolume is different than the one you declare in main. Each of these variables can only be referenced from within the function it is declared in.
In order to save the volume,
calcvolume(area,height);
should be
volume = calcvolume(area,height);
This will store the value being returned from calcvolume in the volume variable in your main function.
You have to assign the result of calcvolume(area,height) to main's volume as follows:
volume = calcvolume(area,height);
Now you can safely use main's volume variable.
I'm guessing that your program was not even reaching the system("pause") line, and was crashing the line above. It could be because volume was never set to anything and was holding garbage data. This garbage data made cout << ... fail.
Before you fix the calcvolume(area,height) line, try fixing your variable declarations so that your variables are initialized to zero:
double area=0.0, height=0.0, volume=0.0; // declare variables
Now run it again and see if it outputs Volume=0.00 and pauses.
It's always good to initialize your variables to zero or something meaningful. Otherwise, they will be initialized to random data (whatever was already in those memory bytes) and will make troubleshooting more difficult.