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.
Related
I have attempted to make a program which allows the user to input 2 values as integers and 2 as doubles, to find the maximum of the two, respectively, using template functions in C++. I can't seem to get my desired outcome when I try to validate the error as it doesn't take in the new input after the error message. The output is usually a '0' after re-entering the new number. I tried using the validation code from this YouTube video:
https://www.youtube.com/watch?v=5kqJlkQCukk&t=666s
Here is the code I've been working with:
#include<iostream>
#include<cmath>
using namespace std;
template <typename E>
void errorA(E a) {
if (!std::cin.good())
{
cout << "\n\nERROR! Faulty input! Try again!\n";
//clear stream
cin.clear();
std::cin.ignore(INT_MAX, '\n');
//get input again
cout << "Enter NUMBER again: ";
cin >> a;
}
}
template <typename T>
void maximum(T& a, T& b) {
if (a > b) {
cout << a << " is the maximum of these 2 numbers: " << a << " & " << b << endl;
}
else if (b > a) {
cout << b << " is the maximum of the 2 numbers: " << a << " & " << b << endl;
}
else if (a == b) {
cout << "Both these numbers: " << a << " & " << b << ", are equal! " << endl;
}
}
int main() {
int a, b;
cout << "Please enter 2 integers to find its maximum.\n";
cout << "Enter 1st number: ";
cin >> a;
errorA (a);
cout << "Enter 2nd number: ";
cin >> b;
errorA( b);
maximum(a, b);
double c, d;
cout << "\n\nPlease enter 2 doubles to find its maximum.\n";
cout << "Enter 1st number: ";
cin >> c;
errorA(c);
cout << "Enter 2nd number: ";
cin >> d;
errorA(d);
maximum(c, d);
}
Take a close look at the signature of the errorA method:
void errorA(E a);
Compare it to your maximum function:
void maximum(T& a, T& b);
Do you see the difference? It's in the ampersand (&) character - it makes the difference between passing by value and passing by reference (with &). For errorA to work as you expect it to, you need to pass by reference (void errorA(E& a), so that the value entered in errorA is actually modifying the passed in variable, and not a copy of it local to the errorA function (which is what happens currently).
Note that the maximum function actually wouldn't need the pass-by-reference parameters, as the variables passed in as parameters are not modified in it.
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;
}
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'));
}
I'm new to programming (in general) and C++ (in particular) and currently learning classes and objects.
I've defined the following as an exercise:
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class X
{
public:
void setNum_1(int);
void setNum_2(int);
void setNum_3(int);
void setNum_4(int);
double getNum_1();
double getNum_2(int num_1);
double getNum_3(int num_1, int num_2);
double getNum_4(int num_1, int num_2, int num_3);
private:
int num_1;
int num_2;
int num_3;
int num_4;
};
int main()
{
X testObject;
int lNum_1 = 0;
int lNum_2 = 0;
int lNum_3 = 0;
int lNum_4 = 0;
cout << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNum_1(lNum_1);
testObject.setNum_2(lNum_2);
testObject.setNum_3(lNum_3);
testObject.setNum_4(lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.getNum_1() << endl;
cout << "The 2nd number returned is: " << testObject.getNum_2(lNum_1) << endl;
cout << "The 3rd number returned is: " << testObject.getNum_3(lNum_1, lNum_2) << endl;
cout << "The 4th number returned is: " << testObject.getNum_4(lNum_1, lNum_2, lNum_3) << endl;
cout << endl;
return 0;
}
void X::setNum_1(int n_1)
{
num_1 = n_1;
}
void X::setNum_2(int n_2)
{
num_2 = n_2;
}
void X::setNum_3(int n_3)
{
num_3 = n_3;
}
void X::setNum_4(int n_4)
{
num_4 = n_4;
}
double X::getNum_1()
{
return sqrt(num_1);
}
double X::getNum_2(int num_1)
{
return pow(num_2,3);
}
double X::getNum_3(int num_1, int num_2)
{
return num_1 * num_2;
}
double X::getNum_4(int num_1, int num_2, int num_3)
{
return (num_1 + num_2) / num_3;
}
Can anyone offer some guidance on how to modify this class so that it has only one member function with all of the arguments defaulted?
Thanks in advance,
Ryan
Ofcourse there are MANY ways to do this and most of them will be more elegant than what I have done here. But this should give you some ideas.
a. You should use the variables defined in the class
b. If you need to perform certain conditional operations in a function, use a switch case or an if statement. And decide the operation based on a parameter passed.
Again, many ways to make this more elegant, but this should get you started and thinking.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class X
{
public:
void setNums(int, int, int, int);
double performOp(int);
private:
int num_1;
int num_2;
int num_3;
int num_4;
};
int main()
{
X testObject;
int lNum_1 = 0;
int lNum_2 = 0;
int lNum_3 = 0;
int lNum_4 = 0;
cout << endl;
cout << "Please enter an integer: ";
cin >> lNum_1;
cout << "Please enter an integer: ";
cin >> lNum_2;
cout << "Please enter an integer: ";
cin >> lNum_3;
cout << "Please enter an integer: ";
cin >> lNum_4;
testObject.setNums(lNum_1,lNum_2,lNum_3,lNum_4);
cout << endl;
cout << "The 1st number returned is: " << testObject.performOp(1) << endl;
cout << "The 2nd number returned is: " << testObject.performOp(2) << endl;
cout << "The 3rd number returned is: " << testObject.performOp(3) << endl;
cout << "The 4th number returned is: " << testObject.performOp(4) << endl;
cout << endl;
return 0;
}
void X::setNums(int n_1, int n_2, int n_3, int n_4)
{
num_1 = n_1;
num_2 = n_2;
num_3 = n_3;
num_4 = n_4;
}
double X::performOp(int n)
{
if(n == 1) return sqrt(num_1);
if(n == 2) return pow(num_2,3);
if(n == 3) return num_1 * num_2;
if(n == 4) return (num_1 + num_2) / num_3;
}