Expected unqualified-id - c++

Im doing a random password generator but I have a problem at a switch.
From case 4 to case 18 it says there is an "Expected unqualified-id" and points the '=' sign but I can't get the error.
Is there a problem with the syntaxis or code?
My code:
switch (a) {
case 0:
char[1] = 'a';
break;
case 1:
char[1] = 'b';
break;
case 2:
char[1] = 'c';
break;
case 3:
char[1] = 'd';
break;
case 4:
char[1] = 'e';
break;

Problem: You can not name your array char, because this is a reserved word in C++.
Solution: Rename your array and try again.

Related

How to do a if condition in a switch statement in c++

I have this code:
#include <iostream>
using namespace std;
int main() {
int square; char state;
cout<<"Write a numbber"; cin>>square;
square *= square;
cout<<square;
switch(square) {
case 1: state = 'h';
case 3: state = 'm';
case 7: state = 'j';
case (square > 10): state = 'u'; // I try this, but not works
}
return 0;
}
I would like to know how a condition is made inside a switch, in c ++.
The expression following case must be a compile time constant. Hence, you may not use what you are trying.
Change that to default: and then use if.
default:
if (square > 10)
state = 'u';
If you have lots of items you should use a switch. If not, if else is better.
If a switch contains more than five items, it's implemented using a lookup table or a hash list. This means that all items get the same access time, compared to a list of if:s where the last item takes much more time to reach as it has to evaluate every previous condition first.
If you want to use condition or combine some:
switch(square) {
case 1: state = 'h'; break;
case 3: state = 'm';break;
case 7: state = 'j'; break;
case 8:
case 9: state = 'u';
case 10: state = 'z';break;
default: state = 'd';
}
Case 8, 9, 10 will be combined if the square is 8.
If you don't have a break at the end of a case region, control passes along to the next case label.

how does this switch block executes?

#include<bits/stdc++.h>
using namespace std;
void show(int errorCause)
{
switch(errorCause)
{
case 1:
{
cout<<"in 1\n";
break;
}
case 2: break;
case 3:
{
cout<<"in 3\n";
break;
case 4:
{
cout<<"in 4\n";
case 5: cout<<"in 5\n";
break;
}
}
break;
default:
{
cout<<"in deafult\n";
break;
}
}
return;
}
int main()
{
show(5);
return 0;
}
I used this sample of code and I could not figure out its flow.According to me it should match the default condition as the errorCause does not match anything,but its output is:
in 5
I don't understand why it is not going to default condition?
Here is my build environment details:
compiler:
g++ version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
System:
Ubuntu 14.04(64-bit)
You pass 5, why should the switch statement not go into 'case 5'?
To make it clear: Remove all these curly braces inside the switch-block, none of them is necessary. The re-align and format the code, then it should be clear.
case/default labels for a switch statement may appear anywhere within that switch statement, except within a nested switch statement.
A famous example of this usage is Duff's device for unrolling loops:
void copy(unsigned char *to, const unsigned char *from, size_t count)
{
size_t n;
if (!count)
return;
n = (count + 7) / 8;
switch (count % 8) {
case 0:
do {
*to++ = *from++;
case 1:
*to++ = *from++;
case 2:
*to++ = *from++;
case 3:
*to++ = *from++;
case 4:
*to++ = *from++;
case 5:
*to++ = *from++;
case 6:
*to++ = *from++;
case 7:
*to++ = *from++;
case 1:
} while (--n > 0);
}
}
(adapted from the original).
At first glance, that doesn't make any sense (and it is somewhat redundant if you allow the compiler to unroll loops for you), but it illustrates that case labels can be placed more or less where you like within the switch statement.
First, don't write code like that. <g>
Second, the reason that it gets to case 5: is simply that there's a case 5: inside the switch statement. It doesn't matter that it's nested inside two levels of curly braces; it's just a label for the code to jump to. It doesn't have to be at the outer level of the switch statement.
It's because actually the switch statement evaluation is "relaxed", so the braces do not matter there. Only case matters, but you can jump right into the middle of a scope by the case (or even to the middle of a loop, see Duff's device).
because the value you passed is 5 , which exactly matches with the switch case parameter.
case 5: cout<<"in 5\n";
break;
if you want to get the default statement then modify the main function as shown below :
int main()
{
show(6);
return 0;
}
hope this helps.

qt textbrowser name save in an array

I am trying to show my 4by4 matrix in qt gui, there for I have used one text browser for each element of matrix. Right now I am able to display matrix using switch case but I dont like this method. I want to make an array in which I can save the name of textbrowser and willing to access them using for loop. below is the my current code. please guide me how can I get what I am willing to do.
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 3;j++)
{
switch(no){
case 1:
ui->textBrowser_200->setText(text1);
break;
case 2:
ui->textBrowser_201->setText(text1);
break;
case 3:
ui->textBrowser_202->setText(text1);
break;
case 4:
ui->textBrowser_203->setText(text1);
break;
case 5:
ui->textBrowser_204->setText(text1);
break;
case 6:
ui->textBrowser_205->setText(text1);
break;
case 7:
ui->textBrowser_206->setText(text1);
break;
case 8:
ui->textBrowser_207->setText(text1);
break;
case 9:
ui->textBrowser_208->setText(text1);
break;
case 10:
ui->textBrowser_209->setText(text1);
break;
case 11:
ui->textBrowser_210->setText(text1);
break;
case 12:
ui->textBrowser_211->setText(text1);
break;
case 13:
ui->textBrowser_212->setText(text1);
break;
case 14:
ui->textBrowser_213->setText(text1);
break;
case 15:
ui->textBrowser_214->setText(text1);
break;
case 16:
ui->textBrowser_215->setText(text1);
break;
}
no++;
}
}
Here is a simple solution which will allow you to keep your current Designer ui:
Add this member variable to your own class, to have two-dimensional matrix of widget pointers:
std::array< std::array <QTextBrowser *, 4>, 4> mTextBrowserMatrix;
Then initialize it in the constructor, with 16 lines of code like this, after you have called setupUi() for the Designer ui:
mTextBrowserMatrix[0][0] = ui->textBrowser_200;
// repeat above for all 16 widgets.
And then just access them like mTextBrowserMatrix[0][0] etc.
You could use any container or even plain C arrays for this, above is just an example.
Of course you will save some repetitive copy-paste style code and have cleaner Designer design, if you just create the QTextBrowser matrix in code, instead of using Designer for it. But since you already have them, might as well stick with it for now, 16 lines of repeated code is not that horrible.

In a switch case statement, it says "duplicate case value" comes up as an error. Anyone know why?

I am working on a rock paper scissors program, but this time the computer chooses rock half the time, scissors a third of the time, and paper only one sixth of the time. The way I did this was I enumerated six possible computer choice values:
enum choicec {rock1, rock2, rock3, scissors1, scissors2, paper};
choicec computer;
But then, after the computer makes its choice, I have to convert these enumerated values to either rock, paper, or scissors. I did this using a switch-case statement:
switch(computer) {
case rock1 || rock2 || rock3:
c = 1;
break;
case scissors1 || scissors2: //ERROR!
c = 3;
break;
case paper:
c = 2;
break;
}
one is rock, two is paper, and three is scissors. However, on the line where I have error written in as a comment, it gives me this error: [Error] duplicate case value.
I'm not sure why.
Any ideas?
I am not sure what you doing, but switch statement should look like this
switch(computer)
{
case rock1:
case rock2:
case rock3:
c = 1;
break;
case scissors1:
case scissors2:
c = 3;
break;
case paper:
c = 2;
break;
}
You can't use || in case branches. Sorry :(
When you use || it does a logical or on them, that says "is rock1 or rock2 or rock3 not a zero?". And the answer is yes, at least one of those is not zero. So rock1 || rock2 || rock3 is true, which is 1. And scissors1 || scissors is also true, which is 1. So you have two case branches for the 1 case.
You should simply use case fallthrough to select multiple conditions:
switch(computer) {
case rock1: case rock2: case rock3:
c = 1;
break;
case scissors1: case scissors2:
c = 3;
break;
case paper:
c = 2;
break;
default:
std::cerr << "INVALID COMPUTER MOVE";
}
Also, I always have a default in my case switches. Sometimes mistakes happen, and we definitely want to know if it doesn't hit any of the case branches. I'm also pretty paranoid about missing else statements, but about half the time it's ok if there's no else.
That switch statement does not do what you think.
Each case defines one value that the value of computer is matched against. Combining several values with logical disjunction to give the value associated with a single case label does not make the corresponding block be entered when the value of computer is equal to any of those values, but rather when it is equal to the result of their logical OR combination. Not very meaningful, indeed.
This is how you could rewrite your switch statement in order to make more sense:
switch(computer) {
case rock1: // Is it rock1?
case rock2: // Or perhaps rock2?
case rock3: // Or maybe rock3?
c = 1; // Well, if it's one of the above, do this...
break;
case scissors1: // OK, it wasn't. So is it scissors1?
case scissors2: // Or scissors2?
c = 3; // If it's one of the above, do this...
break;
case paper: // So is it paper?
c = 2;
break;
default: // Always better to be explicit about this
break;
}
Change it to:
switch(computer) {
case rock1:
case rock2:
case rock3:
c = 1;
break;
case scissors1:
case scissors2:
c = 3;
break;
case paper:
c = 2;
break;
}
rock1 || rock2 || rock3 and scissors1 || scissors2 are both expressions which evaluate to "true", hence the conflict.
The expression used in the switch statement must be integral type ( int, char and enum). In the Switch statement, all the matching case execute until a break statement is reached and Two case labels cannot have the same value.
But in the above case with logical or condition.
At first
case: rock1 || rock2 || rock3:
This will evaluate to 1 and second case scissors1 || scissors2: will also evaluate to 1. This is cause error as said Two case labels cannot have the same value.
This is the reason compiler complains and giving an error:
Compiler Error: duplicate case value
To solve this convert to
switch(computer) {
case rock1:
case rock2:
case rock3:
c = 1;
break;
case scissors1:
case scissors2: //Now will not give any error here...
c = 3;
break;
case paper:
c = 2;
break;
}

c++ classes switch case

switch(choice)
{
case 1:
uinstance1.addNewProduct(data);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
uinstance1.listAllProducts(data);
break;
case 8:
break;
case 9:
break;
case 10:
//name,category,barcode,price,manufacturer,noinstock,soldpermonth,expirydate,discount
// Perishable(string,string,string,double,string,int,int);
Perishable item0("Ferrari","Automobile","9999",2999.99,"Popular",5,0);
data.addNew(item0);
break;
default:
cout<<"Wrong Choice "<<endl;
system("pause");
break;
}
}
Hi ,i have been thinking about this error for quite some time and cant seem to figure out the issue.
error C2361: initialization of 'item0' is skipped by 'default' label
: see declaration of 'item0'
Some help would be appreciated.
Thanks
The whole select block counts as one scope, if you decalare a variable in that scope you need to initialize it in every case statement (every possible execution path). You can avoid it by creating a additional scope in your case to avoid the problem (see the brackets):
switch(choice)
{
case 1:
uinstance1.addNewProduct(data);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
uinstance1.listAllProducts(data);
break;
case 8:
break;
case 9:
break;
case 10:
{
//name,category,barcode,price,manufacturer,noinstock,soldpermonth,expirydate,discount
// Perishable(string,string,string,double,string,int,int);
Perishable item0("Ferrari","Automobile","9999",2999.99,"Popular",5,0);
data.addNew(item0);
}
break;
default:
cout<<"Wrong Choice "<<endl;
system("pause");
break;
}
}
MSDN explains the error C2361 aptly:
The initialization of identifier can be skipped in a switch statement. You cannot jump past a declaration with an initializer unless the declaration is enclosed in a block. (Unless it is declared within a block, the variable is within scope until the end of the switch statement.)
Always pay attention to the error numbers they provide vital information about why the error.
You forgot the braces in one of the cases.
case 10:
{
^^^
Perishable item0;
data.addNew(item0);
break;
}
^^^
Your label is crossing initialization which is illegal. Pretty sure moving default to the top should fix it. You can also add code blocks { } around your relevant code. If you still have problems then move your object outside of the switch block.
You can't create variables inside case statements if you don't define explicity the scope.
There is another discussion about that: Variables inside case statement
case 10:
{ // <<-- This gives explicit scope for the stack variable and let's you get rid of the error
Perishable item0;
// ...
}
break;