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;
}
Related
I'm trying to create a simple calculator and i already encountered an issue when addition is being used. I created a function for addition and whenever i pass in two values i get a different answer. For Example when i add 4,5 i would expect to get 9 but the answer i get is 0029144C . Im still a beginner, so at first i wasn't sure if using type bool for the adding function would affect my result, but i changed it to type float and still getting the same result (in case anyone asks).
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void SimCalcMenu();
void additionSign();
bool makeSum(float num1, float num2);
int main() {
float firstNum, SecondNum;
char operationLetter;
SimCalcMenu();
cout << " Please Select an Operation You Would Like to Perform ";
cin >> operationLetter;
if (operationLetter == 'a' || operationLetter == 'A')
{
additionSign();
cout << " Enter the First Number : ";
cin >> firstNum;
cout << " Enter the Second Number: ";
cin >> SecondNum;
makeSum(firstNum, SecondNum);
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum << endl;
}
else
{
cout << " Error ";
}
return 0;
}
void SimCalcMenu() {
cout << "------------------------------------------------------------------------------" << endl;
cout << " WELCOME TO SIM CALCULATOR " << endl;
cout << "------------------------------------------------------------------------------" << endl;
cout << endl;
cout << " Please Select an Operation : " << endl;
cout << " A.) Addition " << endl;
cout << " B.) Subtraction " << endl;
cout << " C.) Multiplication " << endl;
cout << " D.) Division " << endl;
cout << " E.) Roots ( Only Positive Number)" << endl;
cout << " F.) Power ( Only Positive Number " << endl;
cout << " G.) Percentage " << endl;
cout << " H.) Display functions execution " << endl;
cout << " I.) Quit " << endl;
cout << "------------------------------------------------------------------------------" << endl;
}
void additionSign() {
cout << "------------------------------------------------------------------------------" << endl;
cout << " ADDITION " << endl;
cout << "------------------------------------------------------------------------------" << endl;
}
bool makeSum(float num1, float num2) {
float totSum;
totSum = num1 + num2;
return totSum;
}
makeSum() should return float, because you are returning the sum of two floats.
You are not getting the right result because you are printing makeSum, which is the address of the function. You want to print the value of makeSum(firstNum, SecondNum).
this line
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum << endl;
IS 'printing' 'makesum', makesum is a function so its printing the address of makesum
you need
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum(firstNum, SecondNum) << endl;
now at least it will print the result of makesum. As other have pointerd out that function is wrong (it returns a bool).
should be
float makeSum(float num1, float num2) {
float totSum;
totSum = num1 + num2;
return totSum;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have a simple program but it's running weirdly. Basically the code runs fine but when the numbering at the beginning of the line comes into play, int x++ displays the same number as the first line then continues. Why does this happen?
Code:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include "logo.h"
int main()
{
SetConsoleTitle("plains.exe");
displayLogo();
int number;
int addTotal = 0;
int numbersEntered = 0;
std::cout << " [1] enter your first number: ";
std::cin >> number;
while (number != -1) {
addTotal = addTotal + number;
numbersEntered++;
std::cout << " [" << numbersEntered << "]" << " enter your next number or type '-1' to add them: ";
std::cin >> number;
}
if (number == -1) {
std::cout << " " << std::endl;
std::cout << " --------------------------------" << std::endl;
std::cout << " " << std::endl;
std::cout << " the sum of your numbers is " << addTotal << "." << std::endl;
std::cout << " you entered a total of " << numbersEntered << " numbers." << std::endl;
std::cout << " " << std::endl;
std::cout << " the average of your numbers is " << addTotal / numbersEntered << "." << std::endl;
std::cout << " " << std::endl;
}
return 0;
}
You initialized numbersEntered to 0. The first time through the while loop, it does numbersEntered++, which sets it to 1. So the first prompt in the loop contains [1]. This is the same as what you printed before the loop with:
std::cout << " [1] enter your first number: ";
so you see [1] twice.
To prevent this duplication, add 1 to the variable when displaying the prompt:
std::cout << " [" << (numbersEntered++) << "]" << " enter your next number or type '-1' to add them: ";
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 8 years ago.
Improve this question
I have to make a Calculator using a constructor and destructor, that adds, subtracts, multiplies and divides, and returns the total every time. For some reason, when I call the line "Calculator.add(num);" or any of the "Calculator." portions return me an error saying that it "expected an identifier". Am I missing something simple?
Thanks.
Here is my main.cpp file.
#include <iostream>
#include "Calculator.h"
#include <cstdlib>
using namespace std;
double total;
int main(){
while (true){
cout << "*** Calculator *** " << endl;
cout << "A: Add a value " << endl;
cout << "S: Subtract a value " << endl;
cout << "M: Multiply by a value " << endl;
cout << "D: Divide by a value " << endl;
cout << "T: Get the total " << endl;
cout << "Q: Quit " << endl;
cout << endl;
char input;
cin >> input;
if (input == 'A'){
cout << "Current Total: " << total << endl;
cout << "Selection: A";
cout << endl;
cout << "*** Add selected *** " << endl;
cout << "Value:";
double num;
cin >> num;
cout << endl;
double turnTotal = total;
Calculator.add(num);
cout << turnTotal << "+" << num << " = " << total;
}
if (input == 'S'){
cout << "Current Total: " << total << endl;
cout << "Selection: S";
cout << endl;
cout << "*** Subtract selected *** " << endl;
cout << "Value: ";
double num2;
cin >> num2;
cout << endl;
double turnTotal2 = total;
Calculator.subtract(num2);
cout << turnTotal2 << "-" << num2 << "=" << total;
}
if (input == 'M'){
cout << "Current Total: " << total << endl;
cout << "Selection: M";
cout << endl;
cout << "*** Multiply selected *** " << endl;
cout << "Value: ";
double num3;
cin >> num3;
cout << endl;
double turnTotal3 = total;
Calculator.multiply(num3);
cout << turnTotal3 << "*" << num3 << "=" << total;
}
if (input == 'D'){
cout << "Current Total: " << total << endl;
cout << "Selection: D";
cout << endl;
cout << "*** Divide selected *** " << endl;
cout << "Value: ";
double num4;
cin >> num4;
cout << endl;
double turnTotal4 = total;
Calculator.divide(num4);
cout << turnTotal4 << "/" << num4 << "=" << total;
}
if (input == 'T'){
cout << "Current Total: " << total << endl;
cout << "Selection: T";
cout << endl;
cout << "*** Total selected *** " << endl;
cout << "Value: ";
double num5;
cin >> num5;
cout << endl;
double turnTotal5 = total;
Calculator.getTotal(num5);
cout << turnTotal5 << "-" << num5 << "=" << total;
}
if (input == 'Q'){
cout << "Thank you for using the calculator! Bye bye! Have a great day!" << endl;
}
}
}
And here is the .cpp file
#include <cstdlib>
#include <iostream>
#include "Calculator.h"
using namespace std;
Calculator::Calculator(double x){
double total = x;
return;
}
double Calculator::getTotal(){
return total;
}
void Calculator::add(double x){
total += x;
}
void Calculator::subtract(double x){
total -= x;
}
void Calculator::multiply(double x){
total *= x;
}
void Calculator::divide(double x){
total /= x;
}
And here's the class .h file.
#include <cstdlib>
#include <iostream>
using namespace std;
//class specification
class Calculator {
public:
//constructor
Calculator(){double total = 0;}
Calculator(double total);
//member functions
void add(double x);
void subtract(double x);
void multiply(double x);
void divide(double x);
double getTotal();
//destructor
~Calculator();
private:
//data
double total = 0;
};
You doesn't seem to initialize your Calculator first. You could add the initialization for example on the start of main function as follows
double total;
Calculator calc;
int main(){
while (true){
And then use it like
calc.add(num);
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.
So I've seen the few other posts here and elsewhere regarding increasing decimal precision, but for some odd reason I cannot for the life of me get it to work. Here's what I've got:
//Euler's Method Approximation
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int n;
int i=1;
double h;
double x_n;
double y_n;
cout << "Enter n: " ;
cin >> n;
cout << endl << "Enter h: " ;
cin >> h;
cout << endl << "Enter x_0: ";
cin >> x_n;
cout << endl << "Enter y_0: ";
cin >> y_n;
cout << "n=" << i << " x=" << x_n << " y=" << y_n << endl;
++i;
while(i<n+1)
{
y_n = y_n + 3*x_n*h*y_n;
x_n = x_n + h;
cout << "n=" << i << " x=" << x_n << " y=" << setprecision(6) << y_n << endl;
++i;
}
return(0);
}
I apologize for the mess. Just threw this down under a time crunch, but now I've got more time to look at it. What could I be doing wrong? The mathematics definitely generates decimals to arbitrary precision depending on input, and all the variable types are as they should be. Even have the iomanip library in there. Any pointers? ( Still only prints out to the millionths )
Put std::fixed at the end of cout << "n=" << i << " x=" << x_n << " y=" << setprecision(6) << y_n << fixed << endl; as suggested by #40two in comments.
For ofstream you can use the precision method, http://www.cplusplus.com/reference/ios/ios_base/precision/