Actually i was just trying my hands upon C++ and stopped at this particular problem where instead of program getting closed, it should ask the user whether he/she wants to continue or wants to exit. Now for what i understand, i wrote the do-while code in ending lines but its not working. Please gimme a solution to it. Thanks!!
#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<string>
using namespace std;
class Cal
{
public:
int Add(int a, int b)
{
int res;
res=(a+b);
cout << "Answer is " << a << "+" << b << "=" << res << endl;
}
int Sub(int a,int b)
{
int res;
res=(a-b);
cout << "Answer is " << a << "-" << b << "=" << res << endl;
}
int Mul(int a,int b)
{
int res;
res=(a*b);
cout << "Answer is " << a << "*" << b << "=" << res << endl;
}
int Div(int a,int b)
{
int res;
res=(a/b);
cout << "Answer is " << a << "/" << b << "=" << res << endl;
}
};
int main()
{
int first, second, res, operation;
cout<<"**********************************"<<endl;
cout<<"******* Simple Calculator ********"<<endl;
cout<<"**********************************"<<endl;
cout<<"Select the Operation: "<<endl;
cout<<"1. Addition"<<endl;
cout<<"2. Subtraction"<<endl;
cout<<"3. Multiplication"<<endl;
cout<<"4. Divison"<<endl;
cout<<"Choosen Operation is: ";
cin>>operation;
cout << "Enter the 1st Number: ";
cin>>first;
cout << "Enter the 2nd Number: ";
cin>>second;
switch(operation){
case 1:
Cal a;
a.Add(first,second);
break;
case 2:
Cal b;
b.Sub(first,second);
break;
case 3:
Cal c;
c.Mul(first,second);
break;
case 4:
Cal d;
d.Div(first,second);
break;
default:
cout<< "Please Enter a Operation";
break;
}
char ans;
do
{
cout<< "Do you want to continue (Y/N)?\n";
cout<< "You must type a 'Y' or an 'N' :";
cin >> ans;
}
while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));
}
The do while loop does not enclose the body to be repeated, i.e. the calculator part.
The condition in do while does not look correct.
I would try this.
int main() {
char ans = 'N';
do {
// calculator stuff, better to be in a separate function
cout << "Do you want to continue (Y/N)?\n";
cout << "You must type a 'Y' or an 'N' :";
cin >> ans;
} while ((ans == 'Y') || (ans == 'y'));
}
Related
I am trying to build a calculator in C++. I'm new to the program and have to do this for a school assignment, so sorry for my ignorance. English is also my second language so excuse me if I don't make much sense.
Let's say I have two integers A and B for which a user has to assign a value to either add, subtract, etc. How would I then be able add a third integer (let's say X) without all three showing up when I run the program? So instead of having to type a value for A, B, AND X, it only asks to type a value for X?
For example 4 + 5 = 9, but the calculator can also square numbers, so how do I get the option of a user just filling in 4 squared = 16, while still keeping the former code that lets me add and subtract two numbers?
Maybe seeing the code would help understand what I mean? Sorry if I'm confusing.
#include <iostream.h>
#include <conio.h>
int main ()
{
cout << "Calculator [v.1.0]" << endl;
cout << "(c) 2021 <Chayenne van der Erf>" << endl << endl;
cout << "Kies een bewerking en druk op Enter:" << endl;
cout << "1. Optellen 2. Aftrekken" << endl;
cout << "3. Vermenigvuldigen 4. Delen" <<endl;
cout << "5. Kwadraat 6. Worteltrekken" <<endl;
cout << "7. Reciproke 8. Logarithme" <<endl;
cout << "0. Exit" << endl << endl;
int Bewerking;
cout << "Bewerking: ";
cin >> Bewerking;
cout << "" << endl;
switch (Bewerking) {
case 1:
cout << "+";
break;
case 2:
cout << "-";
break;
case 3:
cout << "*";
break;
case 4:
cout << "/";
break;
default: "Invalid Number";
}
cout << "" << endl << endl;
double A, B;
cout << "Enter een waarde: ";
cin >> A;
cout << "Enter een waarde: ";
cin >> B;
int antwoord;
if (Bewerking == 1) {antwoord = A + B;}
else if (Bewerking == 2 ) {antwoord = A - B;}
else if (Bewerking == 3) {antwoord = A * B;}
else if (Bewerking == 4) {antwoord = A / B;}
cout << "" << endl;
cout << "= " << antwoord << endl;
getch();
return 0;
}
Make the variables, and the reading, conditional on the operation.
Example outline:
if (operation takes one input)
{
double x;
cin >> x;
Calculate result...
}
else if (operation takes two inputs)
{
double x, y;
cin >> x >> y;
Calculate result...
}
else if (operation takes three inputs)
{
double x, y, z;
cin >> x >> y >> z;
Calculate result...
}
Print result...
I'm studying functions in c++ form a book called "jumping to c++" and there are a problem exercise that is create a calculator and I need make the arithmetic operation in separate functions, sound easy and I think I did it 90% good, the program gives me the correct answer but with some random numbers.
the code is:
#include <iostream>
using namespace std;
int a, b;
int sum()
{
return a + b;
}
int subs()
{
return a - b;
}
int div()
{
return a / b;
}
int mult()
{
return a * b;
}
int ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
int main()
{
int opcion;
cout << "1. Sum \n2. Substraction \n3. Division \n4. Multiplication \n\nChoose one option from above: \n\n";
cin >> opcion;
if(opcion == 1)
{
cout << ask();
cout << "The result is: " <<sum() <<"\n\n";
} else if (opcion == 2)
{
cout << ask();
cout << "The result is: " << subs() <<"\n\n";
}else if (opcion == 3)
{
cout <<ask();
cout << "The result is: " << div() <<"\n\n";
}else if(opcion == 4)
{
cout << ask();
cout << "The result is: " << mult() <<"\n\n";
}else
{
cout << "Error.\n\n";
}
system("pause");
}
and this is the "error/bug/whatever"
1. Sum
2. Substraction
3. Division
4. Multiplication
Choose one option from above:
4
Give me the first number: 5
Give me the second number: 5
1878005856The result is: 25
Press any key to continue . . .
notice the error before of "The result is:"
appreciate any help, thanks
ask() does not return anything so it should be a void. Also, you do not need to do cout << ask(); since ask() already does the printing inside of it and it is a void (now) so it can't be printed.
Here is the code with the modifications, see comments with **** in front for changes:
#include <iostream>
using namespace std;
int a, b;
int sum() {
return a + b;
}
int subs() {
return a - b;
}
int div() {
return a / b;
}
int mult() {
return a * b;
}
void ask() { // **** Changed to void here
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
int main() {
int opcion;
cout << "1. Sum \n2. Substraction \n3. Division \n4. Multiplication \n\nChoose one option from above: \n\n";
cin >> opcion;
if (opcion == 1) {
ask(); // **** Removed cout <<
cout << "The result is: " << sum() << "\n\n";
} else if (opcion == 2) {
ask(); // **** Removed cout <<
cout << "The result is: " << subs() << "\n\n";
} else if (opcion == 3) {
ask(); // **** Removed cout <<
cout << "The result is: " << div() << "\n\n";
} else if (opcion == 4) {
ask(); // **** Removed cout <<
cout << "The result is: " << mult() << "\n\n";
} else {
cout << "Error.\n\n";
}
system("pause");
}
You can try it here
The random number was caused by you doing cout << ask(); even though you had not returned anything.
As aschepler pointed out "make sure you enable and read compiler warnings - there should be one saying that ask() doesn't return anything although declared to return an int."
The problem is your int ask() function.
It must return int value which you are writing to console with cout << ask();
The answer above won't work because you cannot write void to cout.
Since you do not return a value then a random number retruned. My compiler marks that as an error.
Replace type of ask function:
void ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
Then replace cout << ask(); in every if statement with just ask();
Like this:
if (opcion == 1)
{
ask();
cout << "The result is: " << sum() << "\n\n";
}
else if (opcion == 2)
{
ask();
cout << "The result is: " << subs() << "\n\n";
}
else if (opcion == 3) ...
Consider checking if b==0 in case of devision operation. Or your program will crash if u try to devide by zero.
this function is returning a random integer. convert it to void
int ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
new
void ask()
{
cout << "Give me the first number: ";
cin >> a;
cout << "\nGive me the second number: ";
cin >> b;
}
I'm working on a project by where I have to create a simple program that works based of the user input. I've gone with a basic calculator but I'm having trouble getting my if/else if statements to work. Basically, if the user types in "Addition", I want the program to say "...I will help you with addition!", and so on for whether the user says "Subtraction", "Division", and "Multiplication".
I'm new to this and so this has already taken me hours upon hours, not looking for you to do it for me but to point out my erorrs and advise so that I can learn from it will be great.
TIA.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
//user inputs what he needs help with/program output
char Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of assistance today?" << endl;
char inpsum[20];
cin >> inpsum;
char output;
if (inpsum == "Addition")
{
cout << "Great! I'll help you with addition!" << endl;
}
else if (inpsum == "Subtraction")
{
cout << "Great! I'll help you with subtraction!" << endl;
}
else if (inpsum == "Division")
{
cout << "Great! I'll help you with division!" << endl;
}
else if (inpsum == "Multiplication")
{
cout << "Great! I'll help you with multiplication!" << endl;
}
return 0;
REST OF CODE
//addition function
void Add() {
float add1, add2;
cout << "Please enter two values you want added together" << endl;
cin >> add1;
cin >> add2;
cout << "The answer is: " << (add1 + add2) << endl;
}
//subtraction function
void Subt() {
float subt1, subt2;
cout << "Please enter two values you want subtracted" << endl;
cin >> subt1;
cin >> subt2;
cout << "The answer is: " << (subt1 - subt2) << endl;
}
//division function
void Div()
{
float div1, div2;
cout << "Please enter two values you want divided" << endl;
cin >> div1;
cin >> div2;
cout << "The answer is: " << (div1 / div2) << endl;
}
//multiplication function
void Mult() {
float mult1, mult2;
cout << "Please enter two values you want multiplacted" << endl;
cin >> mult1;
cin >> mult2;
cout << "The answer is: " << (mult1 * mult2) << endl;
}
int main()
{
Inpsum(); //user inputs what they want help with
Add();
Subt();
Div();
Mult();
return 0 ;
}
This code is all wrong you need to learn about C++ correctly first, here is the corrected code
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include<string>
using namespace std;
//addition function
float Add(float add1, float add2)
{
return (add1 + add2);
}
//subtraction function
float Subt(float subt1, float subt2) {
return (subt1 - subt2);
}
//division function
float Div(float div1, float div2)
{
return (div1 / div2);
}
//multiplication function
float Mult(float mult1, float mult2)
{
return (mult1 * mult2);
}
void input(float &num1, float &num2)
{
cout << "\nEnter First Number : ";
cin >> num1;
cout << "Enter Second Number : ";
cin >> num2;
}
//user inputs what he needs help with/program output
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of assistance today?" << endl;
float num1;
float num2;
string inpsum;
cin >> inpsum;
if (inpsum == "adding")
{ //if user enters "adding"
cout << "Great!, I will help you with " << (inpsum) << endl;
input(num1, num2);
cout << "\nAnser Is " << Add(num1, num2);
}//then output = "...i will help with adding"
else if (inpsum == "subtraction") //otherwise, if user enters "subtraction"
{
cout << "Great!, I will help you with " << (inpsum) << endl; //then output = "...i will help with subtraction"
input(num1, num2);
cout << "\nAnser Is " << Subt(num1, num2);
}
else if (inpsum == "division") //if user enters "division"
{
cout << "Great!, I will help you with " << (inpsum) << endl; ////then output = "...i will help with division
input(num1, num2);
cout << "\nAnser Is " << Div(num1, num2);
}
else if (inpsum == "multiplication") //if user enters "muliplication"
{
cout << "Great, I will help you with " << (inpsum) << endl; ////then output = "...i will help with multiplication"
input(num1, num2);
cout << "\nAnser Is " << Mult(num1, num2);
}
else
{
cout << "Enter A Correct Mathematical Operation";
}
}
int main()
{
Inpsum(); //user inputs what they want help with
cout<<endl;
system("pause");
return 0;
}
First of all, instead of using char array, use std::string.
Secondly, if-else statements have syntax errors.
Basic structure of if-else statements is like
if(condition)
{
//code
}
else
if(condition)
{
//code
}
More on if-else statements in C++
I have been assigned a project that I am having trouble figuring out the issue. The project is :
"The main() function, which shall ask for input from the user to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int,int,int&,int&,int&) that computer the sum, product, and difference of two input arguments, and passes the sum, product and difference by-reference. A value returning function called Power(int a, int b) that computes a raised to the b power. Design and implement your own power function using an iterative control structure, or even recursion. Do no simply write a wrapper around the C++ function called pow(). There should be a user loop and a menu so that the user can select either SumProductDifference, Power, or Quit. The menu should also provide options of allowing the user to set and change the values of the two input integers.
#include "stdafx.h"
#include <iostream>
using namespace std;
void SumProductDifference(int, int, int&, int&, int&);
int Power(int a, int b);
bool GoAgain();
int main() {
int a, b;
int choice;
int sum, product, difference;
do
{
cout << "================================================" << endl;
cout << "=====Welcome to AB Calculator 2016 Edition======" << endl;
cout << "==== 1 - SumProductDifference Function ======" << endl;
cout << "==== 2 - Power of Function ======" << endl;
cout << "==== 3 - Quit ======" << endl;
cout << "= Make A Selection: ======" << endl;
cout << "================================================" << endl;
cin >> choice;
switch (choice) {
case '1': SumProductDifference(a, b, sum, product, difference);
break;
case '2': Power(a, b);
break;
case '3':
break;
default:
break;
}
return 0;
} while (choice != 3);
return 0;
}
bool GoAgain() {
char answer;
cout << "would you like to go again (y/n) ==> ";
cin >> answer;
return answer == 'y';
}
void SumProductDifference(int a, int b, int& s, int& p, int& d) {
cout << "Enter two integers: ";
cin >> a, b;
s = a + b;
p = a * b;
d = a - b;
cout << "The sum of " << a << " + " << b << " = " << s << endl;
cout << "The product of " << a << " * " << b << " = " << p << endl;
cout << "The difference of " << a << " - " << b << " = " << d << endl;
}
int Power(int a, int b) {
int total = 1;
int i;
cout << "Enter a number: ";
cin >> a;
cout << "Raise this integer to the power of: ";
cin >> b;
for ( i = 1; i < b; i++) {
total = total *b;
}
return total;
}
Declaring int choice; but in the switch - case '1'?
You are reading int but comparing it to a char literal.
Either change int choice; --> char choice; or case '1' --> case 1:
Read about fundamental types in C/C++.
Char is just a 8bit integer, which we conveniently choose to represent a character (with the agreement of the compiler).
Also you are not reading correctly multiple values from std::cin: cin >> a, b; and several other errors.
Learn to research correctly. Consult cppreference.com before you ask SO.
Hello I just started learning C++ and Im trying to make a calculator, right now having a fue problems that I simply dont know how to fix in C++.
#include <iostream>
using namespace std;
int main() {
int a, b, c, d;
cout << "1. Saskaitiissana(+)" << endl;
cout << "2. atnnemssana(-)" << endl;
cout << "3. daliissana(/)" << endl;
cout << "4. reizinaassana(*)" << endl;
cin >> d;
switch(d){
case 1 :
cout << "ievadiet a un b lai saskaitiitu(+)" << endl;
cin >> a;
cin >> b;
c = a + b;
cout << "The sum of number 1 and number 2 is " << c << "\n" <<endl;
break;
case 2 :
cout << "ievadiet a un b lai atnnemtu(-)" << endl;
cin >> a;
cin >> b;
c = a - b;
cout << c << endl;
break;
case 3 :
cout << "ievadiet a un b lai reizinaatu(*)" << endl;
cin >> a;
cin >> b;
c = a * b;
cout << c << endl;
break;
case 4 :
cout << "ievadiet a un b lai dal'itu(/)" << endl;
cin >> a;
cin >> b;
if(b==0)
{
cout<<"Nulle neder! start over."<<endl;
}
c = a/b;
cout << c << endl;
break;
}
return 0;
}
The things I still have to do.
Find the most easy way for the program to use numbers only. Also when I type in a number it can not be "empty space".
Also how can I make the case after it finish and gives you the result, go back to the begining of start menu? and if I want to exit a program I press esc or 5?
Also with the exit option I was thinking of useing do while "5" is pressed, can that work in c++?
Right now Im most interested on how to check program to use numbers only and have no empty space when adding numbers.
Thank you for your time :)
For ignoring non-numeric input you can this piece of code:
std::cin >> d;
while(std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Bad entry. Enter a NUMBER: ";
std::cin >> d;
}
or in C-style:
while(scanf("%i",&d)!=1)
{
fseek(stdin,0,SEEK_END);
}
You can also put your whole bunch of code in a while statement to re-run the calculator after one operation.
Taking into account the safe input:
//----------------------------------------------------------------------------
#include <iostream>
using namespace std;
//----------------------------------------------------------------------------
void SafeDouble (double &d)
{
while (!(cin >> d))
{ cin.clear();
while (cin.get() != '\n');
cout << "\tIncorrect. Try again\n\t";
}
cin.sync();
}
//----------------------------------------------------------------------------
int main()
{
cout << "The simpliest calculator\n";
double a = 0.,b = 0.;
cout << "\na = ";
SafeDouble (a);
cout << "b = ";
SafeDouble (b);
cout << "\nEnter operation sign: +, -, * or /\n";
char op;
cin >> op;
cin.sync();
switch (op)
{
case '+': cout << a << " + " << b << " = " << a + b;
break;
case '-': cout << a << " - " << b << " = " << a - b;
break;
case '*': cout << a << " - " << b << " = " << a * b;
break;
case '/': if (b == 0.0)
cout << "Division by zero";
else
cout << a << " / " << b << " = " << a / b;
break;
default: cout << "Incorrect operation sign";
}
cin.get();
return 0;
}
//-----------------------------------------------------------------------------