I'm extremely new to C++, I was following a tutorial and wanted to go a bit off what the course said, I attempted to make a basic calculator that instead of just being able to add could subtract, divide and multiply but it still seems to only be able to add, why is this?
int num1, num2;
double sum;
int addType;
cout << "Type a number: ";
cin >> num1;
cout << "Type a second number: ";
cin >> num2;
cout << "Do you want to 1. add, 2. subtract, 3. divide or 4. multiply: ";
cin >> addType;
if (int addType = 1) {
sum = num1 + num2;
}
else if (int addType = 2) {
sum = num1 - num2;
}
else if (int addType = 3) {
sum = num1 / num2;
}
else if (int addType = 4) {
sum = num1 * num2;
}
cout << "Your total is: " << sum;
}
You are creating new variable in if condition part, update condition part to check if it is equal to something with addType == x
int num1, num2;
double sum;
int addType;
cout << "Type a number: ";
cin >> num1;
cout << "Type a second number: ";
cin >> num2;
cout << "Do you want to 1. add, 2. subtract, 3. divide or 4. multiply: ";
cin >> addType;
if (addType == 1) {
sum = num1 + num2;
}
else if (addType == 2) {
sum = num1 - num2;
}
else if (addType == 3) {
sum = num1 / num2;
}
else if (addType == 4) {
sum = num1 * num2;
}
cout << "Your total is: " << sum;
}
if (int addType = 1)
means assign 1 to "addType", "addType" is alway 1, so condition alway is true.You will only ever be able to add.
Instead of checking equality, your if conditions are initialising the addType var. It should be:
if (addType == 1)
= means to assign a value, which will set addType to the value of 1, and then the if will always be true.
if(int addType = 1)
this is a expression which defines a variable named addType and assigns it's value to 1.
so it basically becomes
if(1)
which is true. so instead of this write:
if( addType ==1)
Related
enter image description hereI wrote C++ program which asks the user for two numbers and operator and gives the output based on the input.I think everything is correct but the output is not the desired output.
#include <iostream>
using namespace std;
int main()
{
int num1;
string op;
int num2;
string result;
cout << "Enter a number: ";
cin >> num1;
cout << "Enter a operator: ";
cin >> op;
cout << "Enter another number: ";
cin >> num2;
if (op == "+"){ //if user types '+' the result is num1 + num2
result = num1 + num2;
//cout << result;
}else if (op == "-"){ //if user types '-' the result is num1 - num2
result = num1 - num2;
// cout << result;
}else if (op == "*"){ //if user types '*' the result is num1 * num2
result = num1 * num2;
//cout << result;
}else if (op == "/"){ //if user types '/' the result is num1 / num2
result = num1 / num2;
//cout << result;
}else{
cout << "Invalid operator...";
}
cout << result;
return 0;
}
The output should be integer.But the output is just a diamond.
Declare the variable result as having the type int
int result = 0;
Or maybe it is even better to declare it as
long long int result = 0;
and use it like
result = static_cast<long long int>( num1 ) * num2;
In your program it has the type std::string
string result;
You are assigning a integer value to a string so I think so it is converting that integer value to character value and giving that emojis. My case was same as your, I
was assigning integer value to a string. But I then declared that integer value as a character value and it worked fine then.
Also the best approach is to use to_string() function to change the integer value to a character value.
I have a question where I need to make a simple program that has you input 2 numbers and then use those as bounds and multiply those numbers and all of them in between.
i.e.
1 and 4
1*2*3*4=24
This is what I've got, but I can not figure out how to make it do that and I end up making it multiply the last 2 numbers together instead of all of them. I think this is because I can't figure out how to pull the number in the loop out so I could multiply it by the next number it would give me.
int main()
{
int num1,
num2,
product = 0,
total = 0;
cout << "Enter any two integers(with a space in between: " << endl;
cin >> num1;
cin >> num2;
for (int i = num1; i < num2; i++)
{
product = i * (i + 1);
}
cout << "The total is: " << product << endl;
return 0;
}
I know for sure that I need to do something else to be able to get the product out and be able to multiply it by the next product, but I can not get it to work. I also don't know if it is alright to put the i there an use it to multiply.
You were facing this problem because of your line: product = i * (i + 1);
What was happening is that the product variable was being overwritten by the product of i and i++. This was not added to the previous value of product. This is why your program only multiplied the last 2 numbers together instead of all of them.
This code below should run as intended:
int main()
{
int num1,
num2,
product = 1; // modified. Plus, total is unused, so you can remove it
//total = 0;
cout << "Enter any two integers(with a space in between: " << endl;
cin >> num1;
cin >> num2;
for (int i = num1; i <= num2; i++) // modified
{
product = product * i ; // modified
}
cout << "The total is: " << product << endl;
return 0;
}
I'm trying to code a postfix calculator, but I keep running into two issues-
first: When the calculator encounters a space, it sort of just exits immediately
second: when it encounters a non operator/non digit (ie- z) it doesn't display the error message that I coded.
int main()
{
stack <int> calcStack;
string exp;
char ans;
cout << "\nDo you want to use the calculator?" << endl;
cin >> ans;
while (ans == 'y')
{
cout << "\nEnter your exp" << endl;
cin >> exp;
for (int i = 0; i < exp.size(); i++)
{
if (isspace(exp[i]))
{
}
else if (isdigit(exp[i]))
{
int num = exp[i] - '0';
calcStack.push(num);
}
else
doOp(exp[i], calcStack);
}
while (!calcStack.empty())
{
calcStack.pop();
}
cout << "\nDo you want to use the calculator again?" << endl;
cin >> ans;
}
system("pause");
return 0;
}
This is the function--
void doOp(const char & e, stack <int>& myS)
{
if (myS.size() == 2)
{
int num1, num2, answ;
num2 = myS.top();
myS.pop();
num1 = myS.top();
myS.pop();
if (e == '+')
answ = num1 + num2;
else if (e == '-')
answ = num1 - num2;
else if (e == '*')
answ = num1 * num2;
else if (e == '/')
answ = num1 / num2;
else if (e == '%')
answ = num1 % num2;
else
cout << "\nError- Invalid operator" << endl;
cout << "\nCalculating..." << endl << answ << endl;
myS.push(answ);
}
else
cout << "\nInvalid stack size- too few, or too many" << endl;
}
In your main loop, you're reading strings with the string extractor:
cin >> exp;
THe string extractor is space sensitive. So as soon as a space char is encountered in the input, the string reading stops, and the witespace is not included in exp.
If you want to get a full line including spaces, you should opt for:
getline (cin, exp);
Edit:
The issue you experience with getline() is realted to the char extraction when you ask if user wants to use the calculator. Entering y is not sufficient. So you'l enter yenter. Only the y will be put into ans, so that getline() will start reading an empty line.
To solve this, update your initial input:
cin >> ans; // as before
cin.ignore (INT_MAX, '\n'); // add this to skip everything until newline included
Here an online demo showing that it works (including error message in case of wrong operator)
I got an assignment where we make a cmd prompt show up and display a flashcard game for multiplication. After inputting a correct answer a prompt shows up and asks the user to go "Again? Y/N." after the second input answer the prompt to ask the user doesn't show up and it's stuck on a "congratulations" message. This happens when I write in code to randomly generate two numbers for the game twice. one outside the while loop, and one inside while loop. If I leave one out the 2nd code for the random numbers it will run fine but will only display the same numbers over again. what I'm asking is how do I fix it so that it won't get stuck after the second answer input?
sample code below:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, ans, guess, count = 0;
char choice;
num1 = rand() % 12 + 1;
num2 = rand() % 12 + 1;
//first number generator.
ans = num1 * num2;
do
{
{
cout << num1 << " X " << num2 << " = ";
cin >> guess;
cout << "Wow~! Congratulations~! ";
count++;
num1 = rand() % 12 + 1;
num2 = rand() % 12 + 1;
//second number generator.
} while (guess != ans);
cout << "\nAgain? Y/N: ";
cin >> choice;
} while ((choice == 'y') || (choice == 'Y'));
//after two turns the loop stops. Can't make a choice.
cout << " Thanks for playing! Number of tries:" << count << endl;
return 0;
}
I'd guess the problem is because your loops aren't quite what you think they are.
do
{
The code above has started a do loop.
{
I suspect you intended to start another (nested) do loop here--but you left off the do, so it's just a block that gets entered, executed, and exited. Useless and pointless in this case.
cout << num1 << " X " << num2 << " = ";
cin >> guess;
cout << "Wow~! Congratulations~! ";
count++;
num1 = rand() % 12 + 1;
num2 = rand() % 12 + 1;
//second number generator.
} while (guess != ans);
You've formatted this as if the while were closing the nested do loop--but since you didn't actually create a nested do loop, this is just a while loop with an empty body. Its meaning would be more apparent with a little re-formatting:
// second number generator
}
while (guess != ans)
/* do nothing */
;
The problem can be found here:
do
{
{
cout << num1 << " X " << num2 << " = ";
cin >> guess;
As you can see, the second scope has no do statement. As a result it is only a codeblock.
You can solve it by writing a do statement for the second code block.
Because the do is not present in the second bracket ({), the while is interpreted as a while loop:
while (guess != ans);
or
while (guess != ans) {
}
this thus keeps looping until guess is not equal to ans. But since in the loop does not modify any of the two variables, the loop will keep iterating.
Other errors: note that the program is still incorrect, since it will claim you have answered the question, regardless of the answer. You can fix it by implementing this as follows:
int main()
{
int num1, num2, ans, guess, count = 0;
char choice;
do {
num1 = rand() % 12 + 1;
num2 = rand() % 12 + 1;
ans = num1 * num2;
do {
cout << num1 << " X " << num2 << " = ";
cin >> guess;
if(guess == ans) {
cout << "Wow~! Congratulations~! ";
} else {
cout << "No, wrong!\n";
}
count++;
} while (guess != ans);
cout << "\nAgain? Y/N: ";
cin >> choice;
} while ((choice == 'y') || (choice == 'Y'));
//after two turns the loop stops. Can't make a choice.
cout << " Thanks for playing! Number of tries:" << count << endl;
return 0;
}
This is the code for my calculator:
#include <iostream>
using namespace std;
int main()
{
int fNumber, sNumber, sum;
string op;
cout << "You will be asked for two numbers and the operator to use on them.\nCurrently supported operators are:\n";
cout << " Addition: +\n Subtraction: -\n Multiplication: *\n Division: \\\n Modulo: %\n\n";
cout << "First Number: ";
cin >> fNumber;
cout << "\nSecond Number: ";
cin >> sNumber;
cout << "\nOperator: ";
cin >> op;
if(op == "+"){
int sum = fNumber + sNumber;
} else if(op == "x" || op == "*"){
int sum = fNumber * sNumber;
} else if(op == "/"){
int sum = fNumber / sNumber;
} else if(op == "-"){
int sum = fNumber - sNumber;
} else if(op == "%"){
int sum = fNumber % sNumber;
} else{
cout << "\nPlease use a correct Operator\n";
return 1;
}
cout << "\n" << fNumber << " " << op << " " << sNumber << " = " << sum << "\n";
return 0;
}
I have absolutely no idea why, but for some reason, whatever numbers or operators I give it it returns "7208640" as the answer:
You will be asked for two numbers and the operator to use on them.
Currently supported operators are:
Addition: +
Subtraction: -
Multiplication: *
Division: \
Modulo: %
First Number: 6
Second Number: 2
Operator: /
6 / 2 = 7208640
Process returned 0 (0x0) execution time : 4.612 s Press any key to
continue.
if(op == "+"){
int sum = fNumber + sNumber;
should be
if(op == "+"){
sum = fNumber + sNumber;
and the same for all the other int sum except the first. You declare a variable once, not every time you use it.
As above, remove the int declarations from inside the if statements.
Also, please change the name of the sum variable to something like "answer." A result from subtraction, multiplication, division and modulo is referred to as difference, product, quotient, and remainder, respectively.