-
December 26th, 2015, 04:56 PM
#16
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)
-
December 27th, 2015, 02:26 AM
#17
Re: C++ Console Even/Odd Control Without %,/,+ and -
Same code i am using but error in 2013 visual studio
-
December 27th, 2015, 05:29 AM
#18
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)
-
December 27th, 2015, 08:51 AM
#19
Re: C++ Console Even/Odd Control Without %,/,+ and -
Originally Posted by 2kaud
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.
-
December 27th, 2015, 10:37 AM
#20
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)
-
December 27th, 2015, 11:37 AM
#21
Re: C++ Console Even/Odd Control Without %,/,+ and -
You saved the day again. Thanks mate.
-
December 27th, 2015, 01:58 PM
#22
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)
-
December 28th, 2015, 08:07 AM
#23
Re: C++ Console Even/Odd Control Without %,/,+ and -
Originally Posted by Ramees219
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.
-
December 28th, 2015, 09:43 AM
#24
Re: C++ Console Even/Odd Control Without %,/,+ and -
Originally Posted by GCDEF
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)
-
December 28th, 2015, 09:57 AM
#25
Re: C++ Console Even/Odd Control Without %,/,+ and -
Originally Posted by 2kaud
Ramees219 post #5 didn't mention anything about homework.
and a Happy new Year
Seems pretty obvious.
Happy new year to you too.
-
December 28th, 2015, 10:03 AM
#26
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
-
December 31st, 2015, 01:11 PM
#27
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.
-
December 31st, 2015, 01:48 PM
#28
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.
-
December 31st, 2015, 02:52 PM
#29
Re: C++ Console Even/Odd Control Without %,/,+ and -
Originally Posted by GCDEF
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.
-
December 31st, 2015, 05:32 PM
#30
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)
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
|