CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Join Date
    Nov 2015
    Posts
    8

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

    Hi everyone I need to write that console program but I don't have any idea how. Can anybody help?

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

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

    Well a good starting point would to be explain in more detail what you are trying to achieve and the problem you are encountering so that we can provide guidance.

    If you are referring to your previous post here http://forums.codeguru.com/showthrea...t=#post2189745

    then from the provided guidance with what are still having problems?
    Last edited by 2kaud; December 24th, 2015 at 02:23 PM.
    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)

  3. #3
    Join Date
    Nov 2015
    Posts
    8

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

    It is unrelated to my previous topic. I need to write a console program that tells the input is odd or even. But I don't have the knowledge to write it. I guess it can be done with using strings, but I couldn't write it.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

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

    Quote Originally Posted by Sikkirigi
    I need to write a console program that tells the input is odd or even. But I don't have the knowledge to write it. I guess it can be done with using strings, but I couldn't write it.
    "The most important single aspect of software development is to be clear about what you are trying to build." -- Bjarne Stroustrup

    Hence, I reiterate 2kaud's point: "explain in more detail what you are trying to achieve and the problem you are encountering". What do you mean by "tells the input is odd or even"? For example, you could provide a few examples of test input, explain what the test input means, then provide the corresponding expected output, explaining how you arrived at the expected output from the input. After that, you can post your best attempt (i.e., code of a small and simple program), along with the actual output that differs from the expected output.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Dec 2015
    Posts
    48

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

    Quote Originally Posted by Sikkirigi View Post
    It is unrelated to my previous topic. I need to write a console program that tells the input is odd or even. But I don't have the knowledge to write it. I guess it can be done with using strings, but I couldn't write it.
    hi i am working on something same like that to . no matter how big the input value is we can find it even or odd from the last value of the input in this case we working on a string and also you can't use (% , / , - , + ) so i use an if statement to find its even or odd
    even values =02468 , odd values = 13579


    #include <iostream>
    #include <string>

    using namespace std;

    string EvenOdd_Finder(string);

    int main(){

    // string input
    string a = "45215485684587452152";


    cout << "this is a "<< EvenOdd_Finder(a) << " value"<< endl;


    return 0;

    }


    string EvenOdd_Finder(string input){

    // even and odd char in string
    string even = "02468";
    string odd = "13579";

    // input size -1 to find the last digit
    int size = input.size() - 1;

    for (int i = 0; i < 5; i++){

    if (input[size] == even[i]){

    return "even";

    }
    else if (input[size] == odd[i]){

    return "odd";
    }
    }



    }

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

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

    When posting code, please use code tags so that it is readable! Go Advanced, select the formatted code and click '#'.
    Code:
    string EvenOdd_Finder( const string& input) {
    
    	// even and odd char in string
    	const string even = "02468";
    	const string odd = "13579";
    
    	const char lst = *input.crbegin();
    
    	for (int i = 0; i < 5; ++i) {
    
    		if (lst == even[i])
    			return "even";
    
    		if (lst == odd[i])
    			return "odd";
    	}
    
            return "bad";
    }
    This is a slightly changed version by first setting lst to the last char of the input string and passing the argument as a const ref rather than by value. It also returns 'bad' if the string doesn't end with a number.

    However, the for loop is not needed. Consider
    Code:
    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";
    }
    and for the masochists consider
    Code:
    string EvenOdd_Finder(const string& input) {
    	const char lst = *input.crbegin();
    	return (string("02468").find_first_of(lst) == string::npos) ? ((string("13579").find_first_of(lst) == string::npos) ? "bad" : "odd") : "even";
    }
    Last edited by 2kaud; December 25th, 2015 at 07:38 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)

  7. #7
    Join Date
    Dec 2015
    Posts
    48

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

    Thank u for correcting the code i hope u have to do that again

  8. #8
    Join Date
    Nov 2015
    Posts
    8

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

    Thanks a lot for the help, but I received an error while compiling it, what is the problem?

    [bcc32 Error] File1.cpp(5): E2108 Improper use of typedef 'string'

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

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

    What exactly is the code that you attempted to compile?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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

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

    and which compiler are you using? This code is for c++11. If you are using an older compiler that doesn't support some of the newer features, then you'll receive compiler errors.
    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)

  11. #11
    Join Date
    Nov 2015
    Posts
    8

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

    I tried this code

    Code:
    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";
    }
    and that line gave the error

    Code:
    string EvenOdd_Finder(const string& input)
    I am using RAD Studio XE8 and I need to make it work on RAD.

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

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

    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.
    Last edited by 2kaud; December 25th, 2015 at 04:49 PM.
    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)

  13. #13
    Join Date
    Dec 2015
    Posts
    48

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

    When a blank string or if a cleared string get error it not showings
    Bad why

    string a = "h"; return bad
    a.clear(): error

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

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

    Please would you explain further as this code compiles and executes as expected
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	string a = "h";
    	cout << a << endl;
    
    	a.clear();
    	cout << a << endl;
    }
    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)

  15. #15
    Join Date
    Dec 2015
    Posts
    48

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

    Sorry my bad . If u pass a value into EvenOdd_Finder like a
    string a = "h"; It return bad but if i clear that or it empty it not return bad
    Last edited by Ramees219; December 26th, 2015 at 02:00 PM.

Page 1 of 3 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