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 8 years ago.
Improve this question
on line 34 of ZipCode.cpp my compiler (MS Visual Studios 2013) is giving me Error: identifier "barCode" is undefined. Here is my header file ZipCode.h:
#ifndef _ZIPCODE_H
#define _ZIPCODE_H
#include <string>
class ZipCode {
private:
std::string barCode;//The Bar Code
void convDigit(int);//Converts a single digit to its bar code equivalent
public:
ZipCode(int);//Constructor recieving a zip code
ZipCode(std::string);//Constructor recieving a bar code
int getZipCode(void);//Returns the zip code
std::string getBarCode(void);//Returns the bar code
};
#endif
Source code ZipCode.cpp:
#include "ZipCode.h"
#include <string>
ZipCode::ZipCode(std::string a){
barCode = a;
}
ZipCode::ZipCode(int a){
barCode = "";
for (int b = 0; b < 5; b++){
int digit = a % 10;
a /= 10;
convDigit(digit);
}
}
void ZipCode::convDigit(int a){
switch (a){
case 0: barCode = std::string("11000") + barCode; break;
case 1: barCode = std::string("00011") + barCode; break;
case 2: barCode = std::string("00101") + barCode; break;
case 3: barCode = std::string("00110") + barCode; break;
case 4: barCode = std::string("01001") + barCode; break;
case 5: barCode = std::string("01010") + barCode; break;
case 6: barCode = std::string("01100") + barCode; break;
case 7: barCode = std::string("10001") + barCode; break;
case 8: barCode = std::string("10010") + barCode; break;
case 9: barCode = std::string("10100") + barCode; break;
}
}
std::string getBarCode(){
return (barCode);//Error: identifier "barCode" is undefined
}
In the previous three functions I use barCode and there is no problem there, so I'm confused why there is now a problem in getBarCode(). I've tried:
return (ZipCode::barCode)//Error: member ZipCode::barCode is inaccessible.
return (this->barCode)//Error: 'this' may only be used inside a non-static member function.
Both of the above errors also confuse me, because barCode and getBarCode are both members of the ZipCode class, and getBarCode is not a static function. I'm still new to programming (if it isn't obvious) and I'm especially new to C++. Any help would be greatly appreciated, thank you.
Like the other methods you define, it needs to be qualified with the name of the class its in, so
std::string ZipCode::getBarCode() {
Related
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 2 months ago.
Improve this question
My code looks like this
enum possible_cases; //assigned somewhere
bool decision; //assigned somewhere
//basically the default action for my possible_cases
int value = 10;
do_something(value);
switch (possible_cases)
{
case 0:
//assume covered by do_something(value)
break;
case 1:
if ( decision )
{
value = get_other_value();
do_something(value);
}
break;
case 2:
value = get_other_value(); //will return same value as in case 1
do_something(value);
break;
}
As you see
it has to run do_something() with one specific value
it might have to run do_something() with other values additionally, and the list of cases might grow, to a point, where having bools is impractical
but overall I am not happy as it is kinda redundant and I think there is a way to do it better.
I would like to stay with enum of cases
Edit:
It seems not clear where the problem is:
I see the call of the same function in 3 places, while I assume I could reduce it to 2 as I know I need to run it only 2 times at max.
It is really more the aesthetic aspect
The redundant part in your code is do_something(value)
So I would suggest you to separate that part of the code
enum possible_cases; //assigned somewhere
bool decision; //assigned somewhere
//basically the default action for my possible_cases
int value = 10;
switch (possible_cases)
{
case 1:
if ( decision )
value = get_other_value();
break;
case 2:
value = get_other_value(); //will return same value as in case 1
break;
}
do_something(value);
By adding extra layer you might do:
std::optional<int>
get_additionnal_value(Epossible_cases possible_cases, bool decision)
{
//basically the default action for my possible_cases
const int value = 10;
switch (possible_cases)
{
default:
case 0: return std::nullopt;
#if 1
case 1: return decision ? get_other_value() : value;
#else
case 1: if (!decision) return value;
[[fallthrough]];
#endif
case 2: return get_other_value(); //will return same value as in case 1
}
}
and then
do_something(10);
if (auto opt_value = get_additionnal_value(possible_cases, decision)) {
do_something(opt_value);
}
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 8 years ago.
Improve this question
So I tried running this piece of code and could not understand why it is giving me the output is giving me. If I input 4, why is the result 17?? I really don't get it. Can someone please explain?
#include<iostream>
using namespace std;
int main() {
int num;
int alpha = 10;
cout << "Enter a number :" << endl;
cin >> num;
switch(num) {
case 3:
alpha++;
break;
case 4:
case 6:
alpha = alpha + 3;
case 8:
alpha = alpha + 4;
break;
default:
alpha = alpha + 5;
}
cout << alpha << endl;
return 0;
}
If you don't have any break;in the body of acase(or something else that would exit the case, like agoto) execution will continue with the next case statement (it will fall through as it's usually known).
In your case entering 4 will do first the case 4:, then fall through tocase 6:and then tocase 8:and the end result will be alpha (10) + 3 + 4 = 17. since the case 8:has abreak;the switch statement will exit here.
On a side note, there's something called compound assignment +=that you can use to save some space, so instead of
alpha = alpha + 3;
you can do this:
alpha += 3;
A case statement works in the following way: The execution jumps to the matching case statement and continues until a break or the end of the switch block. Therefore, the statement after the case 8 is executed, too. So, 10+3+4=17.
Not writing a break at the end of a case block is considered as an error in most cases. In some cases it might be useful to continue with the execution, but this has to be commented very clearly to prevent confusions.
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 8 years ago.
Improve this question
"Expected Member name or ";" after declaration specifiers" error appear on the top line switch(stuff)
float waveform = getParameter(6);
switch(waveform){
case 1: Sine signalGenerator
break;
case 2: SawWave signalGenerator
break;
case 3: SquareWave signalGenerator
break;
default: Sine signalGenerator
}
Any suggestions on how to sort it would be great! Thanks in advance!
This is the real code:
float waveform = getParameter(6);
switch(waveform)
{ case 1: Sine signalGenerator break;
case 2: SawWave signalGenerator break;
case 3: SquareWave signalGenerator break;
default: Sine signalGenerator
}
what you want is probably this:
int waveform = (int) getParameter(6);
WaveBase* sigGen;
switch(waveform) {
case 1: sigGen = new Sine; break;
case 2: sigGen = new SawWave; break;
case 3: sigGen = new SquareWave; break;
default: sigGen = new Sine;
}
// use sigGen here
delete sigGen;
this assumes that Sine, SawWave and SquareWave have a common base (they should)
and that its destructor is virtual (it should)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
The switch statement in C/C++ has an interesing feature that all subsequent blocks will be executed if a condition is met
For example,
int a = 2;
int b = a;
switch(b)
{
case 1:cout<<1;
case 2:cout<<2;
case 3:cout<<3;
case 4:cout<<4;
};
The above code will output 234 unless I put a break statement in case 2.
In 3 years(quite small,yeah) of my C/C++ programming experience, I have never encountered a problem where I had to use switch without putting break statments in every case. But judging by the fact that this feature has been stuck for so long, there might be some utility of it.
Question: What are some clever uses of switch statement as to utilize the above mentioned feature in C/C++?
Probably one of the most interesting use cases I have seen would be Duff's Device the case where you extend a scope within a switch over multiple cases which would look something like this:
void send( int *to, const int *from, int count)
{
int n = (count + 7) / 8;
switch(count % 8)
{
case 0: do { *to = *from++; // <- Scope start
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while(--n > 0); // <- Scope end
}
}
This is usually used when you want to apply a similar action to a set of values. For instance, the following :
switch (event) {
case DEVICE_DISCONNECTED:
case CONNECTION_ERROR:
case CONNECTION_TIMEOUT:
transitionTo(disconnectedState);
break;
case CONNECTION_SUCCESS:
transitionTo(connectedState);
break;
}
is much more concise and readable in my opinion than :
switch (event) {
case DEVICE_DISCONNECTED:
transitionTo(disconnectedState);
break;
case CONNECTION_ERROR:
transitionTo(disconnectedState);
break;
case CONNECTION_TIMEOUT:
transitionTo(disconnectedState);
break;
// ...
}
In my current project, I have the following enumeration:
enum NodeType
{
SCALAR, COMPOSITE, ARRAY, RESTRICTED_ARRAY
};
Thus, quite a few node-processing routines use this pattern:
switch (nodeType)
{
case SCALAR:
processScalar();
break;
case COMPOSITE:
processComposite();
break;
case RESTRICTED_ARRAY:
if (!handleRestrictions())
return false;
// continue to next case
case ARRAY:
processArray();
break;
}
Note that it's almost necessary to always mark the lack-of-break as explicitly intended with a comment (like I did above) - future maintainers (including yourself in 3 months) will thank you.
I've often used a construct to parse command line arguments like this:
switch (argument) {
case arg_h:
case arg__help:
case arg_questionmark:
printf("Help\n");
break;
case arg_f:
case arg__file:
//...
}
where argument is an enum type.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm wondering how to use the switch statement, and I have a couple questions.
What is the case '': for? as in, how do I set the conditions?
How many cases can I have?
How do I set the conditions?
It's very simple: assuming an integral type Type you can use:
Type i;
switch (i) {
case x:
// ...
break;
case y:
// ...
break;
// ...
default:
// ...
break; // optional
};
where x, y, ... etc. are values convertible to the integral type Type that you want to check for equality in i.
So for example:
int x = 3;
switch (x) {
case 1:
std::cout << '1';
break;
case 2:
std::cout << '2';
break;
case 3:
std::cout << '3';
break;
default: break;
};
would print:
3
For more detailed informations on the switch statement, please visit this page.
How many cases can i have?
As many as you want.