I am not getting a number as output but instead a emoji - c++

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.

Related

Creating a very basic calculator in C++

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)

no operator "=" matches these operands operand types are std::basic_ostream<char, std::char_traits<char>> = int

I'm getting the error no operator "=" matches these operands operand types are std::basic_ostream> = int when running code in C++ and i'm not sure whats actually causing the error.
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
int main()
{
int num1, num2;
double average;
// Input 2 integers
cout << "Enter two integers separated by one or more spaces: ";
cin >> num1, num2;
//Find and display their average
cout << average = (num1 + num2) / 2;
cout << "\nThe average of these 2 numbers is " << average << "endl";
return 0;
}
The compiler treats
cout << average = (num1 + num2) / 2;
As:
(cout << average) = ((num1 + num2) / 2);
See C++ operator precedence for more details.
Fix:
cout << (average = (num1 + num2) / 2);
Prefer simpler statements:
average = (num1 + num2) / 2;
cout << average;
Also
cin >> num1, num2;
Should be
cin >> num1 >> num2;

Postfix Notation Calculator

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)

C++ Do-while loop stopping

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;
}

error : binary '>>' : no operator found which takes a right-hand operand of type 'const char [1] And program crashes after taking first input

This is a simple program which takes 2 numbers, reverses them and prints their reversed sum. I have 2 problems
If I keep using "cin >> " it gives error "binary >> :no operator found which take a right hand operand of type 'const char[1](or there is no acceptable version )" .
If I use "scanf_s()" instead of "cin >> " then it crashes after taking the first number.
int calculate_sum(string num){
stack<char> mystack;
int sum = 0;
for (int i = 0; i < num.length(); i++){
mystack.push(i);
}
while (!mystack.empty()){
char c; int n;
c = mystack.top();
mystack.pop();
n= (int)c;
sum = sum + n;
}
int main(){
cout << "Enter testcases:" << endl;
int testcase=0;
cin >> testcase;
while (testcase--){
string num1, num2;
int rev_sum1, rev_sum2, final_sum;
int sum = 0;
cin >> num1 >> "" >> num2 ;
//scanf_s("%s %s", num1, num2);
rev_sum1 = calculate_sum(num1);
rev_sum2 = calculate_sum(num2);
final_sum = rev_sum1 + rev_sum2;
cout << final_sum << endl;
}
return sum;
}
The problem is with this line:
cin >> num1 >> "" >> num2 ;
You cannot store the value got from the input into "". I believe you wanted something like:
cin >> num1 >> num2 ;