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 2 years ago.
Improve this question
I have written a code that prompts the user to input a number and then tell them if it is odd or even. But it is giving me these errors that don't make sense at all.
How do you fix them?
Haven't used C++ in a while but i think it should look something like this
if(x%2 == 0)
{
std::cout << x << "is even\n";
}
else
{
std::cout << x << "is odd\n";
}
your errors are because you were basically saying
cout << x;
"is even";
which doesn't make sense to the compiler or to anyone what exactly is supposed to happen with "is even"
You should use cout << x << "is_even" instead of cout << x; "is_even";.
You get the warning about expression result unused because in your case you don't use "is even".
And the error is because after your if without {} brackets there are 2 expressions.
You can omit brackets and use else after if only if there is one expression after if.
Try adding {} around the code after if and you will get only warnings about expression result unused.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Here's my code:
std::cout << "The contaner is " << (!container)?"not":; "empty";
Which obviously doesn't work but I hope the idea is clear now. I want to print "The container is empty", and to add "not" before "empty" if bool container is false.
I'd like to know if it's possible or if I have to write something along the lines of:
if(container) std::cout ...;
else std::cout ...;
When all else fails, just use an if statement:
std::cout << "The contaner is ";
if (!container)
std::cout << "not ";
std::cout<< "empty";
Personally I like this better then using the conditional operator as it's easier for me to read. This also works when the types of the things you want to display are different. The conditional operator requires that both cases be converted to a common type so something like !container ? "not" : 1 wont work.
You're almost there. The ternary operator will need the else result, you can use an empty string "", then, due to precedence issues you will need to encapsulate the expression with parenthesis:
std::cout << "The contaner is " << (!container ? "not" : "") << "empty";
Try ... << (!container ? "not" : "") << "empty".
You can add the empty string in the non-empty case.
std::cout << "The container is " << (!empty ? "not ": "") << "empty";
Or turn down the cleverness level a bit, which I personally find more readable,
std::cout << "The container is " << (empty ? "empty": "not empty");
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In the following code, the function throws two exceptions in one statement. Now, why is the int catch block handles the exception and not the other block?
Is this always the case that the last exception is the one that gets handled?
try
{
quotient = safe_divide(numerator , denominator);
}
catch(DivideByZero)
{
cout << "Error: Division by zero!\n"
<< "Program aborting.\n";
system("pause");
}
catch (int )
{
cout << "got you " << endl;
cout << "top : " << numerator << endl;
system("Pause");
exit(0);
}
double safe_divide(int top, int bottom) throw(DivideByZero,int)
{
if(bottom == 0)
throw (DivideByZero(),top);
return top/static_cast<double>(bottom);
}
This expression throw (DivideByZero(),top); does not throw two exceptions (which is impossible). It only throws one exception, which is an int.
Here the , is an example of the rarely used comma operator. This operator takes two expressions, evaluates the first, throws that result away, then evaluates the second and returns the value of that.
In practical terms the comma operator is only used when the first expression has some side effect. Since that is not the case here, your code could be simplified to throw top; which makes it clear that an int is being thrown.
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 6 years ago.
Improve this question
I am learning C++ and I have a problem with my program. It should print out following if n=11:
*---------*
-*-------*-
--*-----*--
---*---*---
----*-*----
-----*-----
----*-*----
---*---*---
--*-----*--
-*-------*-
*---------*
This is my code, which works correctly with n=5, but not with greater numbers:
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter size (n x n): " << endl;
cin >> n;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (i%n==j%n) cout << '*';
else if (i%(n-i)==j%(n-j)) cout << '*';
else cout << '-';
}
cout << endl;
}
return 0;
}
This is being printed out if n=11:
*---------*
-*----*--*-
--*-----*--
---*---*---
----*------
-----*-----
-*----*--*-
---*---*---
--*-----*--
-*----*--*-
*---------*
I see that I have successfully wrote how to print out one of '*' diagnoles. But something isn't working with other one, which is going backwards.
Unfortunately, I am not being able to resolve this problem and need your advice. What am I doing wrong? How to debug such problems?
This problem is really simple to debug.
Take a look at the first erroneous *. It appears at the position with i=1, j=6. With n=11, your condition i%(n-i)==j%(n-j) becomes 1%(11-1) == 6%(11-6) which is effectively true because the expression evaluates to 1 on both sides.
What is behind this expression? Why do you use this kind of if to determine whether the cell belongs to the second diagonal? Try to write down each pair i, j which should be printed on the second diagonal, and you should notice a more simple pattern.
P.S. In the expression if (i%n==j%n) you don't have to take operands modulo n, because both of them are less than n, so it is redundant and may be rewritten simply as if (i == j).
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 6 years ago.
Improve this question
I am having an issue with my C++ code. It keeps telling me Undeclared Identifier in the second function (the menu), but I cannot see the issue since I am passing the variable. The code is here
//Menu
void menu(int &selction) //TYPO!
{
cout << "Welcome to the math work along program!" << endl;
cout << "This will generate 2 numbers with the selected operator and\n allow you to solve the equation" << endl;
cout << "1. Addition\n2. Subtraction\n3. Multiplication\nOr type -1 to quit\n";
cin >> selection;
You have a typo. You've misspelled selection as selction
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 7 years ago.
Improve this question
New to coding. The first part in the first link which is the character letter 'a' is correct. But then after that, its downhill from there.
Its showing me whats wrong, but I do not understand it.
You have declared several variables with the same name 'number' in the same scope. That is not allowed and leads to the error messages.
Within a scope (such as a function or loop or something), you can only declare a variable once. Otherwise, it would be ambiguous which one you were talking about.
The error is saying you've already declared a variable called number (as an int), and you cannot declare it again within the same scope.
Make the second variable called something else:
double dNumber = 1.11;
cout << "Please enter a double: " << dNumber << endl;
bool bNumber = 0;
cout << "Please enter a bool: " << bNumber << endl;
Note, it's usually more typical to set bool values to either true or false.
Now, if you really, really wanted to use the variable name number multiple times, you could put each section in curly braces:
{
double number = 1.11;
cout << "Please enter a double: " << number << endl;
}
{
bool number = 0;
cout << "Please enter a bool: " << number << endl;
}
In that case, you would no longer have access to that variable outside the curly braces, thus the reference is no longer ambiguous.