I have a c++ program with a menu of 9 cases. I need that the case 7 is only executed if the case 3 has been used before case 7. How can I do it?
Thanks.
I don't know how to use a case in an "if".
Here's an example of using an if inside a case statement:
case 7:
{
if (previous_selection == 3)
{
Do_Operation();
}
}
break;
This one of many possible solutions, there are others.
You have to flag somehow that case 3 has been executed. For example, with a bool variable, setting it as part of the case 3 block of instructions, and checking it as part of the case 7 block of instructions.
[Demo]
#include <iostream> // cout
int main() {
bool case_3_already_used{ false };
for (auto index : { 7, 6, 5, 3, 2, 1, 8, 7, 9 }) {
switch (index) {
case 0: { std::cout << "0\n"; break; }
case 1: { std::cout << "1\n"; break; }
case 2: { std::cout << "2\n"; break; }
case 3: {
std::cout << "3\n";
case_3_already_used = true;
break;
}
case 4: { std::cout << "4\n"; break; }
case 5: { std::cout << "5\n"; break; }
case 6: { std::cout << "6\n"; break; }
case 7: {
if (case_3_already_used) {
std::cout << "7\n";
} else {
std::cout << "Cannot execute case 7 since case 3 has not been executed yet.\n";
}
break;
}
case 8: { std::cout << "8\n"; break; }
case 9: { std::cout << "9\n"; break; }
default: break;
}
}
}
// Outputs:
//
// Cannot execute case 7 since case 3 has not been executed yet.
// 6
// 5
// 3
// 2
// 1
// 8
// 7
// 9
Now, were the switch logic in a separate function, you would need to keep the state of that boolean variable between different calls. A way to do that would be to mark the flag as static.
[Demo]
Related
i'm following this tutorial and they give us this code to test the function isLowerVowel:
#include <iostream>
bool isLowerVowel(char c, bool yIsVowel)
{
switch (c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
case 'y':
return (yIsVowel ? true : false);
default:
return false;
}
}
int main()
{
std::cout << std::boolalpha;
std::cout << isLowerVowel('a',false) << "\n";
std::cout << isLowerVowel('a',true) << "\n";
std::cout << isLowerVowel('q',false) << "\n";
std::cout << isLowerVowel('q',true) << "\n";
std::cout << isLowerVowel('y',false) << "\n";
std::cout << isLowerVowel('y',true) << "\n";
return 0;
}
I dont understand what the use of yIsVowel is for, shouldnt just testing isLowerVowel be enough? Sorry i asked them but got no replies
I dont understand what the use of yIsVowel is for, shouldnt just testing isLowerVowel be enough?
If you were to use the isLowerVowel fuction to implement the isLowerVowel function you would have recursion. It is unclear how this recursion should be terminated.
yIsVowel appears to be used to set whether y is a vowel or not.
int c = 0;
while (c = getopt(argc, argv, "p:t:e:") != -1) {
std::cout<<"c: "<<c<<std::endl;
switch (c) {
case 'p':
if (optarg) {
std::cout << "lol" << std::endl;
person = atoi(optarg);
}
break;
case 't':
if (optarg) {
time = stod(optarg);
std::cout << "ll" << std::endl;
}
break;
case 'e':
if (optarg) {
ecg = atoi(optarg);
std::cout << "2dasf" << std::endl;
}
break;
}
}
Been trying to make getopt work on my Mac. C is printed as 1, but None of the print statements within the switch are printed. This works perfectly in Linux. What is wrong with my Mac?
I have an idea to make super fast command parser.
I have more than 100 pairs of command - function, and some commands have same prefixes.
Down below there is example of my idea. I can make a program that will generate C++ code like in this example, but i think this can be realized with templates.
I'm not strong in templates. May be some one can help with it?
static const string_view s1{"hello"};
void f1() { cout << "f1" << endl; }
static const string_view s2{"helly"};
void f2() { cout << "f2" << endl; }
static const string_view s3{"hi jo"};
void f3() { cout << "f3" << endl; }
static const string_view s4{"hoyMo"};
void f4() { cout << "f4" << endl; }
void sw(string_view& hw){
switch(hw.size()){
case 5: {
switch(hw[0]){
case 'h': {
switch(hw[1]){
case 'e': {
switch(hw[2]){
case 'l': {
switch(hw[3]){
case 'l': {
switch(hw[4]){
case 'o': {
f1();
break;
}
case 'y': {
f2();
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
case 'i': {
if(hw.substr(2) == s3.substr(2)){
f3();
}
break;
}
case 'o': {
if(hw.substr(2) == s4.substr(2)){
f4();
}
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
default: cout << "command not found" << endl; break;
}
break;
}
case 6: {
//...
break;
}
default: cout << "command not found" << endl; break;
}
}
int main(){
string_view myCommand("hi jo");
sw(myCommand);
string_view myCommand2("hoyMo");
sw(myCommand2);
string_view myCommand3("ha ha");
sw(myCommand3);
}
You should probably be using a parser library, such as Boost.Spirit. This wil allow you to write simple code, like
string("hello")
| string("helly")
| string("hi jo")
| string("hoyMo")
and do all the heavy lifting for you to generate a parser that will probably be faster than something you would write yourself.
Consider the following code:
int main()
{
const int a = 9;
switch (a)
{
case 9:
// since a is 9, this ("good") should be printed
std::cout << " good " << std::endl;
case 4:
// since a is not 4, this ("bad") should not be printed
std::cout << " bad " << std::endl;
// for both a==9 or a==4, this should be printed
{
std::cout << " always print me " << std::endl;
break;
}
}
}
The result should then be:
good
always print me
However this is not working. It there a way to do that in C++? Thanks!
There is no way of doing what you ask for inside a switch-statement short of using goto:
#include <iostream>
int main()
{
const int a{ 9 };
switch (a)
{
case 9:
std::cout << "good\n";
goto foo;
case 4:
std::cout << "bad\n";
goto foo;
foo:
std::cout << "always print me\n";
break;
}
}
I have some code that detects which tab is selected in a QListWidget
int currentTab=ui->tabWidget->currentIndex();
if (currentTab==0)
{
// Code here
}
else if (currentTab==1)
{
// Code here
}
else if (currentTab==2)
{
// code here
}
else if (currentTab==3)
{
// code here
}
How do i use Enums instead of if(currentTab==0) or if(currentTab==1) or if(currentTab==2) or if(currentTab==3)
I would handle the same in the following way (with using an enum type):
enum Tabs {
Tab1,
Tab2,
Tab3
};
void foo()
{
int currentTab = ui->tabWidget->currentIndex();
switch (currentTab) {
case Tab1:
// Handle the case
break;
case Tab2:
// Handle the case
break;
case Tab3:
// Handle the case
break;
default:
// Handle all the rest cases.
break;
}
}
Example to use the Enums given below.
if you want to use same enum element in two enumerations, then you can use the enum classes (strongly typed enumerations) C++11.
#include <iostream>
#include <cstdint>
using namespace std;
//enumeration with type and size
enum class employee_tab : std::int8_t {
first=0 /*default*/, second, third, last /*last tab*/
};
enum class employee_test : std::int16_t {
first=10 /*start value*/, second, third, last /*last tab*/
};
enum class employee_name : char {
first='F', middle='M', last='L'
};
int main(int argc, char** argv) {
//int currentTab=ui->tabWidget->currentIndex();
employee_tab currentTab = (employee_tab)1;
switch (currentTab) {
case employee_tab::first: //element with same name
cout << "First tab Selected" << endl;
break;
case employee_tab::second:
cout << "Second tab Selected" << endl;
break;
case employee_tab::third:
cout << "Third tab Selected" << endl;
break;
case employee_tab::last: //element with same name
cout << "Fourth tab Selected" << endl;
break;
}
employee_name currentName = (employee_name)'F';
switch (currentName) {
case employee_name::first: //element with same name
cout << "First Name Selected" << endl;
break;
case employee_name::middle:
cout << "Middle Name Selected" << endl;
break;
case employee_name::last: //element with same name
cout << "Last Name Selected" << endl;
break;
}
return 0;
}
output:
Second tab Selected
First Name Selected