Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Basically I want to square any number I input. Why does this not work?
I compiles but it does not square my input.
#include <iostream>
using namespace std;
float SquareNumber;
float Squared = SquareNumber * SquareNumber;
int main()
{
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
cout << SquareNumber << " squared is " << Squared;
system("pause");
return 0;
};
I compiles but it does not square my input.
You need to compute the square after you input the number. At the time Squared is computed, the value of SquaredNumber is 0.0. Hence, the value of Squared is also 0.0.
The line
float Squared = SquareNumber * SquareNumber;
sets the value of Squared using the value of SquaredNumber at that time. It does not update the value of Square when the value of SquaredNumber is changed. To get that effect, you need to use a function.
#include <iostream>
using namespace std;
float SquareNumber;
float Squared(float in)
{
return in * in;
}
int main()
{
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
cout << SquareNumber << " squared is " << Squared(SquareNumber);
return 0;
};
Your problem is that you are not correctly calculating the square. You do it as "compile time", rather than at "run time".
Just change your code to this, but be sure that you understand why - and ask here if you do not (hint: how can you calculate the square of a number, before you know what the number is?).
#include <iostream>
using namespace std;
int main()
{
float SquareNumber;
float Squared;
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
Squared = SquareNumber * SquareNumber; // calculate at *run time*
cout << SquareNumber << " squared is " << Squared;
system("pause");
return 0;
};
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am new to C++ coding and am doing an assignment for school but I am stuck and can't seem to figure out what I have been doing wrong. The assignment asks that we use functions to carry out some equations involving a 'right-circular cylinder'. Now, I am able to make a code that does what its supposed to do but it doesn't use functions necessarily to calculate the answers. My current code is as follows:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double r = 0,
h = 0,
TSurfaceArea = 0,
LSurfaceArea = 0,
Volume = 0;
const double PI = 3.cylinder
cout << "Welcome to the right-circular clyinder area and volume calculator, this program will calculate three things: "
"\nThe first, is the Total Surface Area."
"\nThen we will see the Lateral Surface Area."
"\nAnd Finally we will see the volume." << endl;
cout << "\nPlease enter a value for the radius: ";
cin >> r;
cout << "\nThank you! \n\nNow please enter a value for the height: ";
cin >> h;
TSurfaceArea = 2 * PI*r*(r + h);
LSurfaceArea = 2 * PI*r*h;
Volume = PI * r*h;
cout << "Thank you for your input! \n\nSo here are your results based on a radius of " << r << " and a height of " << h << ":" << endl;
cout << "\nTotal Surface Area is " << TSurfaceArea << "\nLateral Surface Area is " << LSurfaceArea << "\nVolume is " << Volume << endl;
system("pause");
}
So like I mentioned, this works but the values have to be assigned as '0' at first and then the equations have to calculate the solutions way down in the code until after a user inputs numbers for 'r' and 'h'. I am sure there's a better way to do this and then I was looking at how to make the equations into functions I can just call later on without having to put a full-on equation everytime I want to calculate with new values which is what the instructor is looking for but I must be doing something wrong creating functions because I can't seem to find a method that works. I have tried this to no avail:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const double PI = 3.14159;
double r = 0,
h = 0,
TSurfaceArea() {
double tsa = 2 * PI*r*(r + h);
return tsa;
},
LSurfaceArea() {
double lsa = 2 * PI*r*h;
return lsa;
},
Volume() {
double v = PI * r*h;
return v;
};
cout << "Welcome to the right-circular clyinder area and volume calculator, this program will calculate three things: "
"\nThe first, is the Total Surface Area."
"\nThen we will see the Lateral Surface Area."
"\nAnd Finally we will see the volume." << endl;
cout << "\nPlease enter a value for the radius: ";
cin >> r;
cout << "\nThank you! \n\nNow please enter a value for the height: ";
cin >> h;
cout << "Thank you for your input! \n\nSo here are your results based on a radius of " << r << " and a height of " << h << ":" << endl;
cout << "\nTotal Surface Area is " << TSurfaceArea << "\nLateral Surface Area is " << LSurfaceArea << "\nVolume is " << Volume << endl;
system("pause");
}
Now I know this should be relatively easy to resolve but I feel like I'm just missing something and hopefully someone can help me out! Thanks in advance!
There are no objects here that I can see. You're using C++ as "a better C".
It's been a long time since I've written C or C++, but I think you want something more like this. I've taken the meat out of the main to emphasize my point:
#include <iostream>
#include <cmath>
using namespace std;
static const double PI = 3.14159; // surely you can do better than six digits
double topSurfaceArea(double r, double h) { return 2.0*PI*r*(r+h); }
double lateralSurfaceArea(double r, double h) { return 2.0*PI*r*h; }
double volume(double r, double h) { return PI*r*r*h; } // your formula was clearly wrong.
int main() { // removed body for simplicity }
A lambda is maybe the closest to what you want,eg for a rectangle
double width,height;
auto area = [&](){ return width*height; };
width = 5;
height = 10;
std::cout << area();
would print 50. However, you probably better learn about functions first...
double area(double width,double height) {
return width * height;
}
int main() {
std::cout << area(5.0,10.0);
}
Note that the function declares a return type (double), it takes some parameters (double and double), and it has neither a , nor a ; after its definition.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Just begin to learn C++, and the code below can't return the right value. I can't find out where is wrong, need some help here, thank you!
const int CM2M = 100;
int main()
{
using namespace std;
int CM;
float M = CM / CM2M + (CM % CM2M) * 0.01f;
cout << "Enter the height in CM: ";
cin >> CM;
cout << "Your in M is " << M << endl;
return 0;
}
cin operation is when you retrieve the value:
you need to get it before any process:
const int CM2M = 100;
int main()
{
using namespace std;
int CM;
cout << "Enter the height in CM: ";
cin >> CM;
float M = CM / CM2M + (CM % CM2M) * 0.01f;
cout << "Your in M is " << M << endl;
return 0;
}
Just for the fun of it, and for education's sake. If you want to emulate declarative programming in C++, to define the relationship between variables and "get the updated value" afterwards, you can get by with a lambda.
const int CM2M = 100;
int main()
{
using namespace std;
int CM;
auto M = [&] { return 1.0f * CM / CM2M + (CM % CM2M) * 0.01f; };
cout << "Enter the height in CM: ";
cin >> CM;
cout << "Your in M is " << M() << endl;
return 0;
}
It's cheating really, since M() invokes the lambda, thus making sure the arithmetic is done after the value is known. Writing the operations in the correct sequence is far clearer. C++ also allows you to declare variables right at the first point of use, so you don't need to declare all your variables ahead of time, you can execute code before the declaration is needed. So you'd do something like this:
const int CM2M = 100;
int main()
{
using namespace std;
int CM;
cout << "Enter the height in CM: ";
cin >> CM;
float M = 1.0f * CM / CM2M + (CM % CM2M) * 0.01f;
cout << "Your in M is " << M << endl;
return 0;
}
I added the artificial 1.0f to make sure it doesn't do integer division, which was another bug in your code.
Put the "float M=..." line after the "cin >>CM;" line. Now you are trying to compute the number of meters before the number of centimeters is even entered, so of course the result is random.
Because CM is only declared and used in the equation before the user has a chance to initialize it.
The current program flow is like this:
CM is declared
M is calculated using the CM value (whatever that is at this moment)
The user enters value for CM
The user sees the result of the conversion (the value for M) however it is already calculated and the user input is not actually
used.
To fix the program move the calculation after the user input:
cout << "Enter the height in CM: ";
cin >> CM;
float M = CM / CM2M + (CM % CM2M) * 0.01f;
cout << "Your in M is " << M << endl;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm making a mortgage calculator for my class, the program runs but the answer isn't correct, I think I have the right formula but maybe I'm declaring my variables wrong.
This is what I have so far
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//Variables
double rate;
rate = rate / 1200.0;
double term;
term = term * 12;
double principal;
double monthlyPayment;
monthlyPayment = (principal * rate) / (1.0 - pow(rate + 1, -term));
//Gathering Values
cout << "Please enter your interest rate." << endl;
cin >> rate;
cout << "Please enter your term." << endl;
cin >> term;
cout << "please enter your principal" << endl;
cin >> principal;
//Formula
monthlyPayment = principal * rate / (1.0 - pow(rate + 1, -term));
//Result
cout << "Your monthly payment is, " << monthlyPayment << endl;
return 0;
}
All of the math you do before the cin statements is not being factored in to your calculation. Also, your second formula calculating monthlyPayment is missing parentheses in the numerator.
I am coding a program to find the maximum of an equation over an interval given by the user. When I compile the code instead of outputting the maximum it gives me this
Please enter the first number of the interval to be checked:
Please enter the last number of the interval to be checked:
Please enter the desired initial step size:
sh: PAUSE: command not found
I figure the problem has to do with my loops, but I'm not sure how to rectify the situation.
Here's my code
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, delta, x, y;
int max = 0;
cout <<"Please enter the first number of the interval to be checked: " << endl;
cin >> a;
cout << "Please enter the last number of the interval to be checked: " << endl;
cin >> b;
cout << "Please enter the desired initial step size: " << endl;
cin >> delta;
for(x = a; x <= b; x = x+delta)
{
y = pow(x, 2)-7*x-18;
if (y > max)
{
max = y;
cout <<"The maximum over the interval from " << a <<" to " << b <<" is " << max;
}
else
{
delta= delta/2;
}
if (delta < pow( 10, -6))
{
break;
}
}
return 0;
}
Looking at your code it seems like that the value of delta should be in float as you are recursively dividing it by 2. Your pow (10,-2) doesn't do any useful comparison for that matter. even after everything it doesn't throw me that pause error. I ran it on VS 12 (C++11).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So, here is the updated thread: Maybe there is white space in there that I'm not seeing? It is the exact same error as it was before. You anyone can think of anything to try, ill do it.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// 1. Initialize Objects
double retail = 0.0;
double markup = 0.0;
double wholesale = 0.00;
// 2. Input
cout << "Hello Emily CO 127: ";
cout << "Enter Retail Price: ";
cin >> retail;
cout << "Enter Mark Up Percent: ";
cin >> markup;
// 3. Process
wholesale = retail * (1 + markup);
cout << " Wholesale " << wholesale << endl;
return 0;
system("pause");
}
using namespace std; is a declaration that introduces the identifiers from the namespace called std in the global scope. It is not the beginning of function or starting point for a block. What you're missing is your main function:
int main() // start of the program
{
// ...
}
This is what your program should look like then:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// 1. Initialize Objects
double retail = 0.0;
double markup = 0.0;
double wholesale = 0.00;
// 2. Input
cout << "Hello Emily CO 127: ";
cout << "Enter Retail Price: ";
cin >> retail;
cout << "Enter Mark Up Percent: ";
cin >> markup;
// 3. Process
wholesale = retail * (1 + markup);
cout << " Wholesale " << wholesale << endl;
return 0;
system("pause");
}
You should read up more on the basics of C++, they will explain everything you need to know.