dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2018
    Posts
    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 07:20 AM. Reason: Added code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,144

    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

  3. #3
    Join Date
    Mar 2017
    Posts
    105

    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 07:24 AM.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,221

    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 07: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++17 Compiler: Microsoft VS2017 (15.7.2)

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,221

    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++17 Compiler: Microsoft VS2017 (15.7.2)

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,518

    Re: how to change switch to if else statement

    Quote Originally Posted by A_Singh View Post
    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.

  7. #7
    Join Date
    Feb 2017
    Posts
    276

    Re: how to change switch to if else statement

    Quote Originally Posted by GCDEF View Post
    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 01:46 AM.

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,221

    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 06: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++17 Compiler: Microsoft VS2017 (15.7.2)

  9. #9
    Join Date
    Feb 2017
    Posts
    276

    Re: how to change switch to if else statement

    Quote Originally Posted by 2kaud View Post
    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.

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,221

    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++17 Compiler: Microsoft VS2017 (15.7.2)

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.