Originally Posted by
tabstop
You can switch on a single character, not a string. You can't use strcmp on a string, you use ==.
Okay...so I created a new variable, a character, which I am initializing to the same thing as ch, which is lineToCheck[j], and running my switch on that. Also, I uncommented the other if statement that I had, which uses == instead of strcmp, but now I get these errors that make it seem like I can't use == either. So I don't know what to do now.
Thanks for all the help, tabstop!
Here are the errors I am getting now:
MatchChecker.h|21|warning: comparison between signed and unsigned integer expressions|
MatchChecker.h|23|error: invalid conversion from `char' to `const char*'|
MatchChecker.h|23|error: initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'|
MatchChecker.h|37|error: conversion from `void' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested|
MatchChecker.h|40|error: no match for 'operator==' in 'ch == '}''|
MatchChecker.h|40|error: no match for 'operator==' in 'popper == '{''|
MatchChecker.h|40|error: no match for 'operator==' in 'ch == ']''|
MatchChecker.h|40|error: no match for 'operator==' in 'popper == '[''|
MatchChecker.h|40|error: no match for 'operator==' in 'ch == ')''|
MatchChecker.h|40|error: no match for 'operator==' in 'popper == '(''|
MatchChecker.h|42|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|
MatchChecker.h|48|error: no match for 'operator+' in 'std:: operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)" at the position ")) + j'|
And here is my code:
Code:
#include <stdio.h>
#include <sstream>
#include <string>
#include <stdexcept>
#include <stack>
// protections
#ifndef MatchChecker_H
#define MatchChecker_H
using namespace std;
class MatchChecker
{
public:
static string matchChecker(string lineToCheck)
{
stack<string> myStack;
for (int j = 0; j < lineToCheck.length(); j++) // line 20
{
string ch = lineToCheck[j];
char switchValue = lineToCheck[j];
switch (switchValue)
{
case '{':
case '[':
case '(':
myStack.push(ch);
break;
case '}':
case ']':
case ')':
if (!myStack.empty())
{
string popper = myStack.pop();
//if (((strcmp (ch, '{') == 0) && (strcmp (popper, '}') == 0)) || ((strcmp (ch, '[') == 0) && (strcmp (popper, ']') == 0)) || ((strcmp (ch, '(') == 0) && (strcmp (popper, ')') == 0)))
if (((ch == '}') && popper == '{') || ((ch == ']') && (popper == '[')) || ((ch == ')') && (popper == '(')))
{
string returnValue = "Error: " + ch + " at the position " + j + " does not match";
return returnValue;
}
}
else
{
string returnValue = "Error: " + ch + " at the position " + j + " does not match";
return returnValue;
}
break;
default:
break;
}
}
if (!myStack.empty())
{
return ("Error: missing right delimiter");
}
}
};
#endif