Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
There is a program I'm building in which I need to calculate torque and diameter. I have both equations in syntax but my output keeps being zero. What am I missing / doing wrong? (Disclaimer: I'm very new to this so please, I welcome constructive criticism! Anything to help me get better. Thanks).
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double p, n, s, t, d;
int main()
{
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
cin >> p, n, s;
t = 6300 * p / n;
d = pow((16 * t) / s, 0.333);
cout << setw(10) << "HP " << p << endl;
cout << setw(10) << "rpm " << n << endl;
cout << setw(10) << "psi " << s << endl;
cout << setw(10) << "torque " << t << endl;
cout << setw(10) << "diameter " << d << endl;
return 0;
}
//Output:
/*Enter values for horsepower (p), rpm (n) and shear strength (s): 20 1500 5000
HP 20
rpm 0
psi 0
torque inf
diameter inf */
The intent of the lines
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
cin >> p, n, s;
is not correctly represented in code.
The second line does not read anything into n or s. It's an expression that uses the comma operator. In this particular case, it evaluates n and s and discards the values.
It's as if you have:
cin >> p;
n;
s;
Change that line to:
cin >> p >> n >> s;
You can also be verbose and use:
cin >> p;
cin >> n;
cin >> s;
Take your input like this (cin >> p >> n >> s) way and also print your result after result calculation you are are printing before.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double p, n, s, t, d;
int main()
{
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
// cin >> p, n, s;
cin >> p >> n >> s;
cout << setw(10) << "HP " << p << endl;
cout << setw(10) << "rpm " << n << endl;
cout << setw(10) << "psi " << s << endl;
t = 6300 * p / n;
d = pow((16 * t) / s, 0.333);
cout << setw(10) << "torque " << t << endl;
cout << setw(10) << "diameter " << d << endl;
return 0;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm a beginner in C++. I appreciate any suggestions in which could improve my code but what I'm really looking for is an explanation as to why my current code isn't returning what I want.
#include <iostream>
#include <string>
using namespace std;
struct MATRIX {
float values[1][1];
string names;
};
MATRIX get_matrix(string name, MATRIX m){
m.names = name;
cout << "Enter values of 2x2 Matrix:" << endl;
cin >> m.values[0][0];
cin >> m.values[0][1];
cin >> m.values[1][0];
cin >> m.values[1][1];
return m;
}
// end get_matrix1
MATRIX get_matrix2(string name2, MATRIX m2){
m2.names = name2;
cout << "Enter values of 2x2 Matrix:" << endl;
cin >> m2.values[0][0];
cin >> m2.values[0][1];
cin >> m2.values[1][0];
cin >> m2.values[1][1];
return m2;
}
int main(){
string testname;
MATRIX matrixtest;
string testname2;
MATRIX matrixtest2;
cout << "Name 1st Matrix:" << endl;
cin >> testname;
MATRIX result = get_matrix(testname, matrixtest);
cout << "Name 2nd Matrix:" << endl;
cin >> testname2;
MATRIX result2 = get_matrix2(testname2, matrixtest2);
cout << "[" << result.values[0][0] << "," << result.values[0][1] << "]" << endl;
cout << "[" << result.values[1][0] << "," << result.values[1][1] << "]" << endl << endl;
cout << "[" << result2.values[0][0] << "," << result2.values[0][1] << "]" << endl;
cout << "[" << result2.values[1][0] << "," << result2.values[1][1] << "]" << endl << endl;
}
It's supposed to return the name of the matrix as well as its values. Whenever it hits the name member in result it just stops the program.
EDIT: Updated to current code
You have to specify the number of elements, not the maximum index, to declare arrays in C++.
With this declaration
float values[1][1];
Only values[0][0] is avaliable.
The declaration should be
float values[2][2];
I am obviously new :( It is my formula, but this has been the only way I can even get it to run at all. It just displays an insanely big number and I don't understand how to write it. Please have mercy on my newbie soul...
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
float F, P, i;
int t;
cout << "Enter how much money is currently in the account: ";
cin >> P;
while (P < 1) // Amount can not be less that $1
{
cout << "Value must be at least $1, enter another amount: ";
cin >> P;
}
cout << "Enter the monthly interest rate: ";
cin >> i;
cout << "Enter how many months this money will be in the account: ";
cin >> t;
F = P * pow( 1 + i,(t * 12));
cout << fixed << showpoint;
cout << "Original:" << setfill(' ') << setw(20) << "$" << P << endl;
cout << "Monthly Interest:" << setfill(' ') << setw(12) << "$" << i << endl;
cout << "Future amount:" << setfill(' ') << setw(15) << "$" << F;
return 0;
}
As already mentioned in the comments, the issue is located in your interest formula. Please change it to the following:
F = P * pow( 1 + i/(P * 12),(t * 12));
As you insert the monthly interest rate i in $, hence as a absolute value, you need to calculate the relative one i / P. Also the compounding frequency is missing, which shall be every month in your question. Therefore the 12 has to be added in the denominator.
Please have a look to Wikipedia for further information, especially in section Calculation.
Hope it helps.
User will enter 4 different points and findSlope (function) will calculate the value for slope and return back the value to be cout in main function.
But when I ran the program, it had logic error for slope value. Any idea why?
#include <iostream>
#include <iomanip>
using namespace std;
float findSlope(float a,float b,float c,float d)
{
return (d-b/c-a);
}
int main()
{
float slope,p1,q1,p2,q2;
cout << "Enter x1: ";
cin >> p1;
cout << "Enter y1: ";
cin >> q1;
cout << "Enter x2: ";
cin >> p2;
cout << "Enter y2: ";
cin >> q2;
slope=findSlope(p1,q1,p2,q2);
cout << "Point1" << "\t\tPoint2" << "\t\tSlope" << endl;
cout << fixed << setprecision(2) << p1 << "," << q1 << "\t" << p2 << "," << q2 << "\t" << slope << endl;
return 0;
}
return (d-b/c-a);
Please look at order of operations again. What you intended to calculate was (d-b)/(c-a). Right now you are calculating d - (b/c) - a. This is a basic mathematical thing and your calculator would most likely give you the same output if you had tried to check this yourself.
Your slope function has
return (d-b/c-a)
did you mean
return ((d-b)/(c-a))
Operator precedence will do the division first, without the brackets.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#ifndef CALC_H
#define CALC_H
class calc
{
double numb1;
double numb2;
public:
calc();
void FUN_SUM();
void FUN_Subtraction();
void FUN_Multiplication();
void FUN_Division();
void FUN_Power();
void FUN_Squareroot();
void FUN_Switch();
void FUN_Loob();
};
#endif // CALC_H
#include "calc.h"
#include <iostream>
#include <cmath>
using namespace std;
calc::calc()
{
numb1 = 0;
numb2 = 0;
}
void calc::FUN_SUM()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Sum;
Result_Of_Sum = numb1+numb2;
cout << "The result of Sum = " << Result_Of_Sum << endl;
}
void calc::FUN_Subtraction()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Subtraction;
Result_Of_Subtraction = numb1 - numb2;
cout << "The result of Subtraction = " << Result_Of_Subtraction << endl;
}
void calc::FUN_Multiplication()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Multiplication;
Result_Of_Multiplication = numb1*numb2;
cout << "The result of Multiplication = " << Result_Of_Multiplication << endl;
}
void calc::FUN_Division()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Division ;
Result_Of_Division = numb1/numb2;
cout << "The result of Division = " << Result_Of_Division << endl;
}
void calc::FUN_Power()
{
cout << "Enter number 1 " << endl;
cin >> numb1;
cout << "Enter number 2 " << endl;
cin >> numb2;
double Result_Of_Power;
Result_Of_Power = pow(numb1,numb2);
cout << "The result of Power = " << Result_Of_Power << endl;
}
void calc::FUN_Squareroot()
{
cout << "Enter the tow number you want Square root \n";
cin >> numb1;
double Result_Of_Square_root;
Result_Of_Square_root = sqrt(numb1);
cout << "The result of Square root = " << Result_Of_Square_root << endl;
}
void calc::FUN_Switch()
{
int S;
cout << "Enter the number you operator do you want do it " << endl;
cout << "1- Addition" << endl;
cout << "2- Subtraction" << endl;
cout << "3- Multiplication" << endl;
cout << "4- Division" << endl;
cout << "5- Power" << endl;
cout << "6- Square Root" << endl;
cout << "7- Exit" << endl;
cin >> S;
switch (S)
{
case 1:
FUN_SUM();
break;
case 2:
FUN_Subtraction();
break;
case 3:
FUN_Multiplication();
break;
case 4:
FUN_Division();
break;
case 5:
FUN_Power();
break;
case 6:
FUN_Squareroot();
break;
default :
break;
}
}
void calc::FUN_Loob()
{
char L;
do
{
FUN_Switch();
cout << "Do you want do another operator ( 'y' or 'n'?) \n";
cin >> L;
if (L== 'y' || L=='Y' || L=='n' || L=='N')
continue;
else
cout << "you are enter roang later\n";
}
while (L == 'Y' || L == 'y' );
}
#include <iostream>
#include "calc.h"
using namespace std;
int main()
{
cout << "Welcome to my simple Calculator\n";
calc simple_calc;
simple_calc.FUN_Loob();
cout << "\n Tank you for use my App :) :) " << endl;
return 0;
}
my question is how i can enable user Calc any value with operators like regular Calculator
for example 1+9*8-1+5 i want my program do like that but i don`t know how ?? :( :(
Writing a calculator is not an easy task, as there is more involved:
Operator Precedence
Grouping
Evaluating of expressions
Operator Precedence
Operator precedence is the grouping of operations, such as performing all multiplication and division before addition and subtraction. A calculator program should handle precedence without parenthesis (grouping).
Grouping
A more advanced calculator will allow grouping of expressions, using parenthesis. The calculator should be able to evaluate the inner most expression and work outward.
Evaluating of Expressions
This means allowing more than 1 digit numbers, and whitespace between numbers and symbols. Also, once the expression is parsed, it needs to be evaluated. The calculator should call the appropriate functions based on the operation (specified by the User).
I suggest you allow the User to enter an expression, and your program read it in as a string. Your program will then parse the string, then evaluate the expression(s).
Search the web and StackOverflow for "c++ calculator" for examples.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi i have problem with c++
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
float a,b,wynik;
cout << "quotient" << endl
<< "..." << endl << endl
<< "quotient 2 numbers."
<< "\ndivisior does not equal 0"<< endl << endl;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
if (b!=0)
cout << "\n" << a << " / " << b << " = " << a/b << "\n\n";
else
cout <<"\ndivisior does not equal 0!\n\n";
system("PAUSE");
return 0;
}
I must use for or while when someone try quotient number by 0.
If I understood correctly, you probably want to ask for input again if b is zero
You can have something like this :
do {
cout << "quotient" << endl
<< "..." << endl << endl
<< "quotient 2 numbers."
<< "\ndivisior does not equal 0"<< endl << endl;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
if (b!=0)
cout << "\n" << a << " / " << b << " = " << a/b << "\n\n";
else
cout <<"\ndivisior does not equal 0!\n\n";
}while(b==0);
Your terms are wrong, see Wikipedia definition of "quotient".
#include <iostream>
int main(void)
{
char prompt[] =
"\n"
"Division, dividend / divisor = quotient:\n"
" Enter divisior: ";
int divisior;
int dividend;
while (true)
{
std::cout << prompt;
std::cin >> divisor;
if (divisor == 0)
{
std::cout << "\n* Divisor is zero, try again.\n";
continue;
}
std::cout << "\n Enter dividend: ";
std::cin >> dividend;
std::cout << "\nResult of "
<< dividend
<< " / "
<< divisor
<< " is, using integer division, "
<< dividend / divisor
<< "\n";
break;
}
return EXIT_SUCCESS;
}