-
January 24th, 2018, 08:08 AM
#1
[RESOLVED] how to change switch to if else statement
Code:
void Logger::logging(const int slevels, const string smsgs)
{
string logging;
switch (slevels)
{
case 0:
logging = "[INFO]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
break;
case 1:
logging = "[DEBUG]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
break;
case 2:
logging = "[ERROR]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
break;
case 3:
logging = "[WARNING]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
break;
default:
Logger::logging(2, "Invalid\n");
ofs << logging;
break;
}
}
Last edited by 2kaud; January 24th, 2018 at 08:20 AM.
Reason: Added code tags
-
January 24th, 2018, 08:16 AM
#2
Re: how to change switch to if else statement
1. Please, use CODE tags while posting code snippets.
2. You may want to change the signature of Logger::logging to be
Code:
void Logger::logging(int slevels, const string& smsgs)
3. What's wrong with using switch?
Victor Nijegorodov
-
January 24th, 2018, 08:21 AM
#3
Re: how to change switch to if else statement
Seems quite easy huh..
Code:
void Logger::logging(const int slevels, const string& smsgs)
{
string logging;
if (slevels = 0)
{
logging = "[INFO]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
}
else if (slevels = 1)
{
logging = "[DEBUG]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
}
else if (slevels = 2)
{
logging = "[ERROR]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
}
else if (slevels = 3)
{
logging = "[WARNING]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
}
else
{
Logger::logging(2, "Invalid\n");
ofs << logging;
break;
}
}
Well, good advice, prefer switch over if else if.
If you like my answer, do rate my post.
Last edited by A_Singh; January 24th, 2018 at 08:24 AM.
-
January 24th, 2018, 08:31 AM
#4
Re: how to change switch to if else statement
Consider (not tried)
Code:
void Logger::logging(int slevels, const string& smsgs)
{
const int maxlog = 4;
const static string log[maxlog] = {"INFO", "DEBUG", "ERROR", "WARNING"};
if ((slevels < 0) || (slevels >= maxlog))
Logger::logging(2, "Invalid\n");
else {
const string logging = "[" + log[slevels] + "]" + smsgs + "\n";
vect.push_back(logging);
++lognum;
ofs << logging;
}
}
Last edited by 2kaud; January 24th, 2018 at 08:49 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 24th, 2018, 02:30 PM
#5
Re: how to change switch to if else statement
Why use an int for slevels? Why not an enum? Consider
Code:
enum class Levels {lInfo = 0, lDebug, lError, lWarning};
void logg(Levels slevels, const string& smsgs)
{
const static map<Levels, string> errs {{Levels::lInfo, "INFO"}, {Levels::lDebug, "DEBUG"}, {Levels::lError, "ERROR"}, {Levels::lWarning, "WARNING"}};
if (auto i = errs.find(slevels); i == errs.end())
logg(Levels::lError, "Invalid\n");
else {
const string logging = "[" + i->second + "]" + smsgs + "\n";
vect.push_back(logging);
++lognum;
//ofs << logging;
}
}
Note VS c++17 code.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 25th, 2018, 10:49 AM
#6
Re: how to change switch to if else statement
Originally Posted by A_Singh
Seems quite easy huh..
Code:
void Logger::logging(const int slevels, const string& smsgs)
{
string logging;
if (slevels = 0)
{
logging = "[INFO]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
}
else if (slevels = 1)
{
logging = "[DEBUG]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
}
else if (slevels = 2)
{
logging = "[ERROR]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
}
else if (slevels = 3)
{
logging = "[WARNING]" + smsgs + '\n';
vect.push_back(logging);
lognum++;
ofs << logging;
}
else
{
Logger::logging(2, "Invalid\n");
ofs << logging;
break;
}
}
Well, good advice, prefer switch over if else if.
If you like my answer, do rate my post.
Not quite that easy. Look at your if statements again.
-
January 27th, 2018, 01:43 AM
#7
Re: how to change switch to if else statement
Originally Posted by GCDEF
Not quite that easy.
Many logical operators have alternative representations. For example instead of != one can use not_eq and instead of ! one can use not. These are keywords. Strangely enough == does not have an alternative representation. There is no eq keyword.
If eq were a keyword I'm sure people would use it and also start using the other logical keywords to a greater extent as well. The error in #3 obviously is an oversight but it's a quite common mistake that would never happen if one could use eq.
I know == doesn't fit the criterion that was used to determine what logical operators should be keywords but I think they should've let eq slip in anyway for reasons of symmetry and beauty of design, and for the principle of least astonishment. I couldn't believe it when I realized exactly eq had been left out.
Last edited by wolle; January 27th, 2018 at 02:46 AM.
-
January 27th, 2018, 04:49 AM
#8
Re: how to change switch to if else statement
In the c++ standard, the full supported set is
Code:
and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=
These are defined for when certain keyboards for some languages do not support the required symbols (& ! etc). As the keyboards support =, having an alternative for == is not required - the same reason that there is no defined alternative to >, < etc.
In VS, these are just macro definitions (not an integral part of the language) (#include <ciso646>) so additional definitions could be provided.
PS. You could always use not not_eq instead of ==
Last edited by 2kaud; January 27th, 2018 at 07:27 AM.
Reason: PS
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 27th, 2018, 10:49 AM
#9
Re: how to change switch to if else statement
Originally Posted by 2kaud
PS. You could always use not not_eq instead of ==
Certainly, double negations are a great way not to not confuse the competition.
But I think it was a big blunder not to include eq among and, or, not and not_eq. It essentially killed the appeal of using any of those keywords.
-
January 27th, 2018, 11:18 AM
#10
Re: how to change switch to if else statement
But I think it was a big blunder not to include eq among and, or, not and not_eq. It essentially killed the appeal of using any of those keywords.
What was the quote re the design of the camel?
...
Oh yes. A camel is a horse designed by a committee!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|