Error Reading Characters of String

I am currently using command line inputs in conjunction with several hardware inputs from my io ousb board...

I am trying to execute code where my second command-line argument, 'c1' ,'c2' 'c3' or 'c4' executes the next part of my code that I've written. However, when I run the code, It only reads 'c', not 'c1'. Through debugging, it says there is an error in reading the characters of the string.

below is a segment of my code:

if (argc == 3)
{
char * choice = argv[2];




//cout << argv[2][0] << endl;
if (choice == "C1")
{
if (ReadPinC() > PortADC())
{
mainResult = ReadPinC();
printBinaryReverse(check_result(mainResult));
WritePortB(check_result(mainResult));
}
else if (PortADC() > ReadPinC())
{
mainResult = PortADC();
printBinaryReverse(check_result(mainResult));
WritePortB(check_result(mainResult));
}
return 0;
}

else if (choice == "c2")
{
Make choice a std::string if you want to compare with ==
Thanks, could you explain to me why this is so? I'm still fairly new to c++ and coding in general
Each operator, like ==, has to be defined for its operands and no such operation is built into the standard for C-strings (your char[] arrays): for those you could use the strcmp() function to compare two C-strings, but it is more messy.

std::string is a well-defined class to hold character sequences that behaves naturally and has comparison-for-equality operations (==) defined for it, as well as the convenience of building up strings with a + operation.

For more detail see:
http://www.cplusplus.com/reference/string/string/

Note that we can only see a few lines of your code, so it is not easy to comment, and you don't give the actual error message. It would also help if you put your code in code tags (first item on the format menu), as this would preserve indentation etc. and, for runnable code, allow us to compile and run it in an online shell.
Topic archived. No new replies allowed.