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 7 years ago.
Improve this question
I was wondering why I get a compile error when I try to use std::cout in between, say, an if statement and else if statement.
For example:
if (condition)
{body}
std::cout << "hello world" << std::endl;
else if (condition)
{body}
Gives the error
error: 'else' without a previous 'if'
Let me get this right first:
You want to execute the cout statement in between the conditional no matter whether the condition was met or not, i.e. no matter whether the body of the if got executed or not.
As previous commenters noted, you cannot place something between the end of scope of the if-block and the else keyword.
What about approaching this by splitting up the if-else-if block into two separate if-blocks:
if (condition1) {
body1
}
cout << "hello world" << endl;
if (!condition1 && condition2) {
body2
}
That's why indentation is important
if (condition)
{ body
std::cout << "hello world" << std::endl;
}
else if (condition)
{
body
}
In your code, cout is outsite the if block, so no more else is expected.
You cannot add any executable code between if and else if other than the enclosed body of the if and else if loops.
if (firstCondition)
{
/*code for firstCondition*/
//code anything here
}
//not here #######
else if (secondCondition)
{
/*code for secondCondition*/
//code anything here
}
Correct is:
if (condition)
{
std::cout << "hello world" << std::endl;
}
else if (condition)
{body}
That would be an option for you.
if (fistCondition)
{
/*code for fistCondition*/
std::cout << "hello first" << std::endl;
}
else if (secondCondition)
{
/*code for secondCondition*/
std::cout << "hello second" << std::endl;
}
If you wanna call the cout in any case between the first and second condition, then avoid the else keyword and apply two if statements.
if (fistCondition)
{
/*code for fistCondition*/
std::cout << "hello first" << std::endl;
}
std::cout << "posterior to first and prior to second if statement" << std::endl;
if (secondCondition && !firstCondition)
{
/*code for secondCondition*/
std::cout << "hello second" << std::endl;
}
In this case, && !firstCondition emulates an else keyword for your purpose.
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 4 years ago.
Improve this question
I'm facing a problem in c++ that I don't understand.
this is my code:
auto DataArray = jvalue.at(U("data")).as_array();
std::cout << "Outside the loop, first output" << std::endl;
for (int i = 0; i <= 10; i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
std::cout << "Outside the loop, second output" << std::endl;
Output:
Outside the loop, first output
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
inside the loop
Press any key to continue . . .
It seems the code stops after the loop reach its end.
But why?
But if I commented out the
//auto data = DataArray[i];
//auto dataObj = data.as_object();
it doesn't have a problem.
By the way I'm working on cpprest and get json object data from api.
The jvalue variable holds the result.
And if I try and catch the code:
try {
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
catch (const std::exception& e) {
std::wcout << e.what() << std::endl;
}
the result is infinite loop with output: not an object.
Please help. Thank you.
I think you should use i < 10 instead i <= 10 in your loop:
for (int i = 0; i < 10; i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
Your last iteration hasn't output inside the loop. It fails there, there is no DataArray[10] with 10 index
And much better is to use DataArray.size() instead i < 10
for (int i = 0; i < DataArray.size(); i++)
{
auto data = DataArray[i];
auto dataObj = data.as_object();
std::wcout << "inside the loop" << std::endl;
}
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 4 years ago.
Improve this question
This is the function I made:
void loading(bool wsound) {
if (wsound = true)
{
PlaySound(TEXT("sounds/intro.wav"), NULL, SND_ASYNC);
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << ".........." << endl;
Sleep(1000);
}
else if (wsound = false)
{
cout << "..........";
Sleep(1000);
cout << "..........";
Sleep(1000);
cout << ".........." << endl;
}
else {
cout << "An error occured" << endl;
cin.get();
system("exit");
}
}
So what this basically does is that it takes a bool if the value is true then it loads it with sounds if false then it loads it without sounds.
My problem is that in the main I placed a bool with the value of true then it worked though after calling it again with the value type of false it still loads it with sounds.
The code is like this:
//a bunch of code here
loading(true);
//a bunch of code here
loading(false);
//and more codes.........
This is not a check, this is an assignment: wsound = true
You should use something like wsound == true, however in an if statement it is enough to only use if (wsound), which is equal to if (wsound == true). For the false check you could use: if(!wsound).
EDIT: To understand what happens:
if(wsound = true) is a correct statement, because the assignment operator=(), used in this if statement, should in the most cases return a non-cost reference to the variable, which is already explained here.
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 5 years ago.
Improve this question
It seems that my if statements are not working and I'm getting the "illegal else without matching if" error message. any help would be great, thanks.
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int igame = 0;
int main()
{
Sleep(1000);
cout << "welcome to the Wild Casino!";
Sleep(1000);
cout << "\nplease select a game to play. 1 for Slots, 2 for Roulette, and 3 for Blackjack: ";
cin >> igame;
if (igame == 1);
{
cout << "\nWelcome to Slots";
}
else if (igame == 2);
{
cout << "\nWelcome to Roulette";
}
else
{
cout << "\nWelcome to Blackjack";
}
Sleep(1000000);
return 0;
}
if (igame == 1);
You have an extra semicolon at the end - this is equivalent to
if (igame == 1) { }
Your code creates an ill-formed program:
if (igame == 1) { }
{ // block not attached to if
cout << "\nWelcome to Slots";
}
else if (igame == 2) { } // this else has no matching if
{
cout << "\nWelcome to Roulette";
}
else // this else has no matching if
{
cout << "\nWelcome to Blackjack";
}
I'm just learning how to use exceptions in C++ and have come across weird behavior in my "test" code. (excuse overly stupid questions like this one, please...it's NOT lack of research/effort, just lack of experience!) If I'm catching just the exception DivideByZero it works fine.
But introducing the second exception StupidQuestion makes the code not work exactly how I expected. How I wrote it below I thought it should take care of the DivideByZero exception if it needs to, and if not then check if StupidQuestion occurs, and if not just go back to the try clause and print the normal result. But if I input, say, a=3 and b=1, the program redirects to the DivideByZero try clause instead of the StupidQuestion one. The weird thing is, though, divide does seem to be throwing StupidQuestion (see via cout statement), but it's not catching right, as also seen by the absense of the cout statement.
#include <iostream>
#include <cstdlib>
using namespace std;
const int DivideByZero = 42;
const int StupidQuestion=1337;
float divide (int,int);
main(){
int a,b;
float c;
cout << "Enter numerator: ";
cin >> a;
cout << "Enter denominator: ";
cin >> b;
try{
c = divide(a,b);
cout << "The answer is " << c << endl;
}
catch(int DivideByZero){
cout << "ERROR: Divide by zero!" << endl;
}
catch(int StupidQuestion){
cout << "But doesn't come over here...?" << endl;
cout << "ERROR: You are an idiot for asking a stupid question like that!" << endl;
}
system("PAUSE");
}
float divide(int a, int b){
if(b==0){
throw DivideByZero;
}
else if(b==1){
cout << "It goes correctly here...?" << endl;
throw StupidQuestion;
}
else return (float)a/b;
}
I was wondering if it had something to do with the fact that DivideByZero and StupidQuestion were both of type int, so I changed the code to make StupidQuestion be of type char instead of int. (So: const char StupidQuestion='F'; and catch(char StupidQuestion) were really the only things changed from above) And it worked fine.
Why isn't the above code working when the two exceptions have the same type (int)?
Instead of this
catch(int DivideByZero) {
cout << "ERROR: Divide by zero!" << endl;
}
catch(int StupidQuestion) {
cout << "But doesn't come over here...?" << endl;
cout << "ERROR: You are an idiot for asking a stupid question like that!" << endl;
}
you are looking for
catch (int errval) {
if (errval == DivideByZero) {
cout << "ERROR: Divide by zero!" << endl;
}
else if (errval == StupidQuestion) {
cout << "ERROR: You are an idiot for asking a stupid question like that!" << endl;
}
else {
throw; // for other errors, keep searching for a handler
}
}
The variable name inside the catch clause is creating a new local variable, which has no relation to a global constant with the same name.
Also note that there will be no way to catch just one error number... but you can rethrow unknown errors as I show.
catch(int DivideByZero) { }
catch(int StupidQuestion) { }
Both catch blocks catch ints, they're just named differently. Only the first one can ever be entered, the second one is dead code.
When choosing a handler for an exception only type is taken into account, and neither values nor addresses (addresses of variables are not applicable here at all because of how exceptions work), also names of variables do not exist after compilation.
The first appropriate handler for the exception is always chosen.
Please look my answer to another question for details: https://stackoverflow.com/a/45436594/1790694
I am test out a question/answer program and relatively new to c++. I am trying to create a question/answer and so far using the while loop to check if the answer is true it will retuen the problem is if the question is wrong if the here is my code:
while(input1 == answer1)
{
cout << "Your answer is correct!\n";
answer_correct += 1;
break;
}
while(answer_correct = 0 )
{
cout << "Correct Answer is:\n" << answer1 << "\n";
break;
}
for ZETA
original code:
if (input3 == answer3)
{
cout << "Your answer is correct!";
answer_correct += 1;
}
else
{
cout << "Correct Answer is:\n" << answer3;
}
all if statements below this one are return false and display the answers below when input == to answer
Now I need to make it not show the correct answer using if statements. I am probably confused would someone educate me on a proper loop to use?
Is there any particular reason why you don't use a if-else statement?
if(input1 == answer1)
{
cout << "Your answer is correct!\n";
}
else
{
cout << "Correct Answer is:\n" << answer1 << "\n";
}
Explanation
The block/statement following the if(condition) will only be executed if and only if the condition is true. An else following an if will only be executed if and only if the preceding if failed.
See also:
CPP: Control Structures