illegal else with no matching if [closed] - c++

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

Related

Is a function terminates as soon as it returns a value, ignoring its next lines? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
What value the return_a_number function returns in following case:
#include <iostream>
#include <conio.h>
using namespace std;
int return_a_number(int n)
{
if(n < 0) return -1;
return 1;
}
int main()
{
cout << "Enter any number: ";
cin >> num;
cout << return_a_number(num);
getch();
return 0;
}
As return 1 is the last statement of function, Is it returns 1 everytime?
I'm new in c++. So I've many confusions.
If a function returns, it terminates there, following lines of code in that function will not be executed.
#include <iostream>
#include <string>
std::string foo(int x)
{
if(x < 0) return "If x < 0, this will be returned";
if(x == 0) return "If x == 0, this will be returned";
return "If x > 0, this will be returned";
}
int main() {
std::cout << foo(-1) << std::endl;
std::cout << foo(0) << std::endl;
std::cout << foo(1) << std::endl;
}
Output:
If x < 0, this will be returned
If x == 0, this will be returned
If x > 0, this will be returned

Function with boolean expression doesnt seem to work in C++ [closed]

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.

Facing error in c++ code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am writing code of ACM problem in which we have to check possibilities of different items. It's some minor error in the code.
#include<iostream>
using namespace std;
void CheckPossibilities( int numItems, int maxWeights )
{
if( numItems <= 0 )
{
cout << "Invalid Items";
}
if ( maxWeights <= 0 )
{
cout << "Impossible";
}
while( maxWeights > 0 )
{
if(numItems%2==0) //for even
{
numItems = numItems / 2;
maxWeights--;
}
else
{
numItems = (numItems -1)/ 2; //for odd
maxWeights--;
}
}
if( numItems <= 1 )
{
cout << "Possible";
}
else
{
cout << "Impossible";
}
}
void main()
{
int numItems1,maxWeights1;
cout<<"enter numItems"<<endl;
cin>>numItems1;
cout<<"maxWeights"<<endl;
cin>>maxWeights1;
cout<<numItems1 "AND" maxWeights1<<endl;
cout<<CheckPossibilities(numItems1, maxWeights1);
}
Your mistakes were trying to cout multiple strings in one line without concatting them in some way, either seperate with a << or a +. You also cant cout a void function because it tries to output void, you just need to call it and let the function do the outputting. With errors fixed the main should be
int main()
{
int numItems1,maxWeights1;
cout<<"enter numItems"<<endl;
cin>>numItems1;
cout<<"maxWeights"<<endl;
cin>>maxWeights1;
cout<<numItems1+"AND"+maxWeights1<<endl;
CheckPossibilities(numItems1, maxWeights1);
return 0;
}
Next time look at what line the error is thrown on when compiling and search those specific error because these were really simple and specific syntax errors that could be found by a google search easily.
void main()
{
int numItems1, maxWeights1;
cout << "enter numItems" << endl;
cin >> numItems1;
cout << "maxWeights" << endl;
cin >> maxWeights1;
cout << numItems1 << "AND" << maxWeights1 << endl;
CheckPossibilities(numItems1, maxWeights1);
}
you can never do this: cout<<CheckPossibilities(numItems1, maxWeights1); cout take standard output stream, not functions. And also you forgot to put << in cout << numItems1 << "AND" << maxWeights1 << endl; in this form, your code build succesful.

How to use an if statement in a string array? [closed]

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
The following code is supposed to randomly pick a string from the array and then say "Yay!" if Happy has been chosen.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
int main()
{
srand(time(NULL));
string textArray[4] = { "Happy", "Sad", "Mad", "Overjoyed." };
int RandIndex = rand() % 4;
cout << textArray[RandIndex] << endl;
//if (textArray == "Happy") old
if (RandIndex == 0) //new
{
cout << "Yay!" << endl;
}
cin.get();
}
My problem is that the operand types are incompatible with strings and chars. What would be the best solution to this problem?
EDIT: All I needed to do was replace "if (textArray == "Happy")" with "if (RandIndex == 0)"
For example like
if ( textArray[RandIndex] == "Happy" )
{
cout << "Yay!" << endl;
}
or like
if ( RandIndex == 0 )
{
cout << "Yay!" << endl;
}
Also it would be better to write at least like
string textArray[] = { "Happy", "Sad", "Mad", "Overjoyed." };
const size_t N = sizeof( textArray ) / sizeof( *textArray );
size_t randIndex = rand() % N;

Creating a Tic Tac Toe with 4x4 grid, cout and endl unidentified? [closed]

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 8 years ago.
Improve this question
Here is the code, i am a beginner at coding and do not know how to find out the errors after following various different tutorials. I keep coming up with errors as 'cout' and 'endl' are undeclared identifiers.
#include <iostream>
#define ORDER 4
void PrintGrid(char [ORDER][ORDER]);
bool CheckGrid( char [ORDER][ORDER]);
int main(void){
char grid[ORDER][ORDER] ;
int j,k,l;
for(j=0;j<ORDER;j++)
for(k=0;k<ORDER;k++)
grid[j][k]=' ';
grid[2][3]='X';
PrintGrid(grid);
return 0;
}
void PrintGrid(char g[ORDER][ORDER]){
for (int j=0;j<ORDER;j++){
for(int l=0;l<2*ORDER +1;l++)
cout << '-';
cout << endl <<'|';
for (int k=0;k<ORDER;k++)
cout << g[j][k] <<'|';
cout << endl;
}
for(int l=0;l<2*ORDER +1;l++)
cout << '-';
cout << endl;
}
bool CheckGrid( char g[ORDER][ORDER]){
// check horiz
// untested
int k,j;
for( k=0;k<ORDER;k++){
for( j=1;j<ORDER;j++)
if(g[0][k]!=g[k][j]) break;
if(j==ORDER) return true;
}
for( k=0;k<ORDER;k++){
for( j=1;j<ORDER;j++)
if(g[k][0]!=g[k][j]) break;
if(j==ORDER) return true;
}
for( k=0;k<ORDER;k++){
if(g[0][0]!=g[k][k]) break;
if(k==ORDER) return true;
}
for( k=0;k<ORDER;k++){
if(g[0][ORDER-1]!=g[ORDER-k][ORDER-k]) break;
if(k==ORDER) return true;
}
return false;
}
cout and endl are in the std namespace. You have to reference them like this:
std::cout << ... << std::endl;
unless you are using them:
using std::cout;
using std::endl;
You can also include the whole namespace:
using namespace std;