CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32
  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    What does it return for an empty string? For VS2015 this code
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string EvenOdd_Finder(const string&);
    
    int main() {
    	string a = "h";
    	cout << "a = \"h\" is an " << EvenOdd_Finder(a) << " value" << endl;
    
    	a.clear();
    	cout << "a.clear() is an " << EvenOdd_Finder(a) << " value" << endl;
    
    	a = "";
    	cout << "a = \"\" is an " << EvenOdd_Finder(a) << " value" << endl;
    	return 0;
    }
    
    string EvenOdd_Finder(const string& input)
    {
    	const char lst = *input.crbegin();
    
    	if (string("02468").find_first_of(lst) != string::npos)
    		return "even";
    
    	if (string("13579").find_first_of(lst) != string::npos)
    		return "odd";
    
    	return "bad";
    }
    displays
    Code:
    a = "h" is an bad value
    a.clear() is an bad value
    a = "" is an bad value
    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)

  2. #17
    Join Date
    Dec 2015
    Posts
    48

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Same code i am using but error in 2013 visual studio
    Name:  P_20151227_124738_1_HDR_p.jpg
Views: 113
Size:  26.9 KB

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    The code works in release mode but gives this debug assertion message when compiled in debug mode as the original function assumed that input is not empty. Try

    Code:
    string EvenOdd_Finder(const string& input)
    {
    	if (input.size() == 0)
    		return "bad";
    
    	const char lst = *input.crbegin();
    
    	if (string("02468").find_first_of(lst) != string::npos)
    		return "even";
    
    	if (string("13579").find_first_of(lst) != string::npos)
    		return "odd";
    
    	return "bad";
    }
    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)

  4. #19
    Join Date
    Nov 2015
    Posts
    8

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Quote Originally Posted by 2kaud View Post
    What you posted is just a function, not a complete program. In my post #6 I only provided replacement code for the function, not a complete program. A complete compilable program would be something like
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string EvenOdd_Finder(const string&);
    
    int main()
    {
    	string a = "4521548568458745215a";
    
    	cout << "this is an " << EvenOdd_Finder(a) << " value" << endl;
    	return 0;
    }
    
    string EvenOdd_Finder(const string& input)
    {
    	const char lst = *input.crbegin();
    
    	if (string("02468").find_first_of(lst) != string::npos)
    		return "even";
    
    	if (string("13579").find_first_of(lst) != string::npos)
    		return "odd";
    
            return "bad";
    }
    If this doesn't compile then there is an issue with the compiler. However as RAD Studio XE8 is based upon the Clang c++11 compiler, the above should compile and run OK.
    Code:
    [bcc32 Error] File1.cpp(18): E2316 'crbegin' is not a member of 'string'
      Full parser context
        File1.cpp(17): parsing: string EvenOdd_Finder(const string &)
    I get this error now.

  5. #20
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    .crbegin() requires c++11. Try replacing
    Code:
    	const char lst = *input.crbegin();
    with
    Code:
    	const char lst = *input.rbegin();
    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)

  6. #21
    Join Date
    Nov 2015
    Posts
    8

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    You saved the day again. Thanks mate.

  7. #22
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    but this shows that the compiler you are using is not c++11 compliant. IMO you should consider upgrading to a version which is.
    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)

  8. #23
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Quote Originally Posted by Ramees219 View Post
    Thank u for correcting the code i hope u have to do that again
    He shouldn't. Typically we provide guidance, rather than actually doing somebody's homework for them.

  9. #24
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Quote Originally Posted by GCDEF View Post
    He shouldn't. Typically we provide guidance, rather than actually doing somebody's homework for them.
    Ramees219 post #5 didn't mention anything about homework.

    and a Happy new Year
    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)

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Quote Originally Posted by 2kaud View Post
    Ramees219 post #5 didn't mention anything about homework.

    and a Happy new Year
    Seems pretty obvious.

    Happy new year to you too.

  11. #26
    Join Date
    Dec 2015
    Posts
    48

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    I respect that . Look i am not a good programmer like u guys because i don't get the chance to goto college or university i like learning programming now i am trying to learn from internet i aske questions when i am stuck . When i am stuck i need an example to know how it can done . So i just showed what i know from that i learned my mistake .
    If someone take advantage of it in some time professor ask questions about the code right so he have to learn to answer it or he will regret later
    All i am saying is i need help. Good night

  12. #27
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Alternativelly, a lookup array (poor man's hash table) also works.

    You can use a "const char*" to save on the space used by the array, at the cost of not using std::string:

    Code:
    #include <iostream>
    #include <array>
    
    using namespace std;
    
    const auto Even_Odd_Array = [] {
        auto odd_even = array<const char*, 256>{};
        for (auto& p : odd_even)
            p = "bad";
        for (auto c : "02468")
            odd_even[c] = "even";
        for (auto c : "13579")
            odd_even[c] = "odd";
        return odd_even;
    }();
    
    const char* EvenOdd_Finder(const string&);
    
    int main() {
    	string a = "4521548568458745215";
    
    	cout << "this is an " << EvenOdd_Finder(a) << " value" << endl;
    	return 0;
    }
    
    const char* EvenOdd_Finder(const string& input) {
        return input.empty()
            ? "bad"
            : Even_Odd_Array[input.back()];
    }
    Last edited by monarch_dodra; December 31st, 2015 at 01:43 PM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  13. #28
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Also, note that all the solutions provided so far (mine included) are string based. Why not operate on actual numbers? You can easily test for parity with a bit tests (not forbidden according to requirements):

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int number;
    	std::cin >> number;
    	cout << number << " is an " << (number & 1 ? "odd" : "even") << " value" << endl;
    }
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  14. #29
    Join Date
    Jun 2015
    Posts
    208

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Quote Originally Posted by GCDEF View Post
    Typically we provide guidance
    Then I'd say there are two time-tested principles that apply in this case: Separation of Concern (SoC) and Keep it Simple Stupid (KISS).

    SoC suggests we first check whether the input string is a valid integer syntactically, and only then whether it's odd or even.

    KISS suggests we turn the rightmost digit into an integer using a standard C++ function and then apply a conventional odd/even test on that integer.
    Last edited by tiliavirga; January 1st, 2016 at 04:01 AM.

  15. #30
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: C++ Console Even/Odd Control Without %,/,+ and -

    Why not operate on actual numbers? You can easily test for parity with a bit tests (not forbidden according to requirements):
    Ramees219 post #5 - which included some code to answer the question posed in post #1 - specifically mentions that Ramees219 is using strings for numbers. Hence the following posts relate to testing strings.

    If an integer number is to tested for odd/even then monarch_dodra's code in post #28 can be used which is much more efficient than string testing.
    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)

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured