C++ using char in an if statement - c++

I'm building a standard four-function calculator and I've come across a confusing bug.
char Engine::AskUser(){
char type;
std::cout << "'a'dd, 'm'ulitply, 's'ubract, or 'd'ivide ?\n";
std::cin >> type;
CheckUser(type);
return type;
}
void Engine::CheckUser(char uType){
if(uType != 'a' || uType != 's' || uType != 'm' || uType != 'd'){
std::cout << "Type 'a', 'm', 's', or 'd'\n";
AskUser();
}
else
return;
}
What happens is, even if I enter a, s, m, or d, the if statement still iterates as if those conditions were true, which is clearly not the case. I don't get it. Is uType not carrying the value of type from AskUser(), or something similar?

if(uType != 'a' || uType != 's' || uType != 'm' || uType != 'd')
A char is either not equal to 'a', or it is equal to 'a', in which case it's not equal to 's', so this condition is always true.
It should be logical AND:
if(uType != 'a' && uType != 's' && uType != 'm' && uType != 'd')

your if conditional is doing the following check:
if value is not equal to a --
or
if value is not equal to s --
or
if value is not equal to m --
or
if value is not equal to d --
regardless of what char variable uType is, it will always NOT BE at least 3 of the previous 4 variables, hence the if conditional will always result in a TRUE value.
Edited: What I believe you wanted to do was the following:
if ( (uType == 'a') || (uType == 's') || ......)
{
return;
}
else
{
...//The code you previously had if it were true
}
In this case, you would return whenever uType was 1 of the 4 values, otherwise you would implement your code.

Change this line -
if(uType != 'a' || uType != 's' || uType != 'm' || uType != 'd')
to this -
if(uType != 'a' && uType != 's' && uType != 'm' && uType != 'd')
It will surely work.

Related

iterate char by char through vector of strings

I want to iterate char by char in a vector of strings. In my code I created a nested loop to iterate over the string, but somehow I get an out of range vector.
void splitVowFromCons(std::vector<std::string>& userData, std::vector<std::string>& allCons, std::vector<std::string>& allVows){
for ( int q = 0; q < userData.size(); q++){
std::string userDataCheck = userData.at(q);
for ( int r = 0; r < userDataCheck.size(); r++){
if ((userDataCheck.at(r) == 'a') || (userDataCheck.at(r) == 'A') || (userDataCheck.at(r) == 'e') || (userDataCheck.at(r) == 'E') || (userDataCheck.at(r) == 'i') || (userDataCheck.at(r) == 'I') || (userDataCheck.at(r) == 'o') || (userDataCheck.at(r) == 'O') || (userDataCheck.at(r) == 'u') || (userDataCheck.at(r) == 'U')){
allVows.push_back(userData.at(r));
}
else if ((userDataCheck.at(r) >= 'A' && userDataCheck.at(r) <= 'Z') || (userDataCheck.at(r) >= 'a' && userDataCheck.at(r) <= 'z')){
allCons.push_back(userData.at(r));
}
else {
continue;;
}
}
}
}
The error here is in these lines:
allVows.push_back(userData.at(r));
allCons.push_back(userData.at(r));
the r variable is your index into the current string, but here you're using it to index into the vector, which looks like a typo to me. You can make this less error prone using range-for loops:
for (const std::string& str : userData) {
for (char c : str) {
if (c == 'a' || c == 'A' || ...) {
allVows.push_back(c);
}
else if (...) {
....
}
}
}
which I hope you'll agree also has the benefit of being more readable due to less noise. You can further simplify your checks with a few standard library functions:
for (const std::string& str : userData) {
for (char c : str) {
if (!std::isalpha(c)) continue; // skip non-alphabetical
char cap = std::toupper(c); // capitalise the char
if (cap == 'A' || cap == 'E' || cap == 'I' || cap == 'O' || cap == 'U') {
allVows.push_back(c);
}
else {
allCons.push_back(c);
}
}
}
Since this question is about debugging actually, I think it is a nice illustration of how the usage of std::algorithms of C++ can decrease the effort needed to see what is wrong with a non working code.
Here is how it can be restructured:
bool isVowel(char letter)
{
return letter == 'A' || letter == 'a' ||
letter == 'E' || letter == 'e'||
letter == 'O' || letter == 'o'||
letter == 'Y' || letter == 'y'||
letter == 'U' || letter == 'u';
}
bool isConsonant(char letter)
{
return std::isalpha(letter) && !isVowel(letter);
}
void categorizeLetters(const std::vector<std::string> &words, std::vector<char> &vowels, std::vector<char> &consonants)
{
for( const std::string &word : words){
std::copy_if(word.begin(), word.end(), std::back_inserter(vowels), isVowel);
std::copy_if(word.begin(), word.end(), std::back_inserter(consonants), isConsonant);
}
}
With a solution like this, you avoid the error-prone access-with-index that lead to your problem. Also, code is readable and comprehensive

Reduce cyclomatic complexity

I'm writing an NMEAParser library. As its name suggests, it parses NMEA sentences. Nothing crazy.
Its entry point is a function that accepts an NMEA string as its only parameter and looks at its beginning to pass it to the right decoder. Here is the function:
bool NMEAParser::dispatch(const char *str) {
if (!str[0]) {
return false;
}
//check NMEA string type
if (str[0] == '$') {
//PLSR245X
if (str[1] == 'P' && str[2] == 'L' && str[3] == 'S' && str[4] == 'R' && str[5] == ',' && str[6] == '2' && str[7] == '4' && str[8] == '5' && str[9] == ',') {
if (str[10] == '1')
return parsePLSR2451(str);
if (str[10] == '2')
return parsePLSR2452(str);
if (str[10] == '7')
return parsePLSR2457(str);
} else if (str[1] == 'G' && str[2] == 'P') {
//GPGGA
if (str[3] == 'G' && str[4] == 'G' && str[5] == 'A')
return parseGPGGA(str);
//GPGSA
else if (str[3] == 'G' && str[4] == 'S' && str[5] == 'A')
return parseGPGSA(str);
//GPGSV
else if (str[3] == 'G' && str[4] == 'S' && str[5] == 'V')
return parseGPGSV(str);
//GPRMC
else if (str[3] == 'R' && str[4] == 'M' && str[5] == 'C')
return parseGPRMC(str);
//GPVTG
else if (str[3] == 'V' && str[4] == 'T' && str[5] == 'G')
return parseGPVTG(str);
//GPTXT
else if (str[3] == 'T' && str[4] == 'X' && str[5] == 'T')
return parseGPTXT(str);
//GPGLL
else if (str[3] == 'G' && str[4] == 'L' && str[5] == 'L')
return parseGPGLL(str);
}
//HCHDG
else if (str[1] == 'H' && str[2] == 'C' && str[3] == 'H' && str[4] == 'D' && str[5] == 'G')
return parseHCHDG(str);
}
return false;
}
The problem I have is that this function's cyclomatic complexity is quite high, and my SonarQube complains about it:
It's not really a problem as the code is quite easy to read. But I was wondering how I could reduce its complexity while still keeping it simple to read and efficient.
You can simplify this quite a lot:
if (std::string_view{str, 10} == "$PLSR,245,")
{
switch (str[10])
{
case '1' : return parsePLSR2451(str);
case '2' : return parsePLSR2452(str);
case '7' : return parsePLSR2457(str);
}
}
else if (std::string_view{str + 1, 2} == "GP")
{
auto s = std::string_view{str + 3, 3};
if (s == "GGA")
return parseGPGGA(str);
if (s == "GSA")
return parseGPGSA(str);
// ... etc
}
else if (std::string_view{str + 1, 5} == "HCHDG")
{
return parseHCHDG(str);
}
return false;
There's no extra strings being constructed either, so it should be at least as efficient.

Pig Latin Program

I'm not getting errors, but the output is incorrect. I'm not sure what I'm doing wrong. I can only use functions from string library.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
string message, pig_message;
getline(cin, message);
unsigned int x = message.find_first_of("aeiou");
if (message[x] == 'a' || 'e' || 'i' || 'o' || 'u' ) {
pig_message = message + "yay";
cout << pig_message;
}
else if (!(message[x] == 'a' || 'e' || 'i' || 'o' || 'u' )) {
pig_message = message.substr(1) + message[0] + "ay";
cout << pig_message;
}
system("pause");
return 0;
}
The first if statement is always true. You should change it to
if (message[x] == 'a' || message[x] == 'e' || message[x] == 'i' || message[x] == 'o' || message[x] == 'u' ) {
Also, you could change the else if (...) { line to just
else {
if you want it to be executed every time the first if statement is not true.
Your comparison statement is incorrect.
Make sure your function is actually iterating through the letters, and that you're concatenating the strings correctly.
So:
unsigned int x = message.find_first_of("aeiou"); // Returns the first match
if(message[x] == 'a' || message[x] == 'e'...) // Currently your code reads as only checking for a.
Think of it as IF message[x] = a, IF e, IF i
vs
if message[x] = a, IF message[x] = i
What does your code do after it finds a match?
pig_message = message + 'yay' would add "yay" to the whole message string.
It would then print it out and move on, without doing anything to the other vowels.
I'm new to C++ myself but that's how I've understood your code.
It might be better to go through the whole input string one letter at a time in a for loop with your if else statements to add the strings inside the loop.

Evaluating a single char in an if statement: C++

I am having some troubles with my if loop.
First off I have I assigned char sign.
void evaluate_ps(istream& input)
{
char sign;
input >> sign;
cout << sign << endl;
check(sign);
}
That prints / so my sign has the value '/'
Then I go to my void check(char operation) function
void check(char operation)
{
if(operation != '-' || operation != '+' ||
operation != '*' || operation != '/')
{
return false;
}
else return true;
}
and it's returning false... WHY!!!! I can't seem to figure this out.
Thanks everyone.
This happens because you are using the || (OR) operator. When operation is / the check operation != '-' returns true. Since || is short circuited, the entire expression returns true.
Change it to && (AND):
if (operation != '-' && operation != '+' &&
operation != '*' && operation != '/')
Another way to write this is:
if (!(operation == '-' || operation == '+' ||
operation == '*' || operation == '/'))
You probably meant all your || to be &&:
if(operation != '-' && operation != '+' &&
operation != '*' && operation != '/')
Otherwise, it will always enter the if-statement since a character will always not equal one of 4 different things.
The if statement is responding to the / not equaling one of the other values
Think about "or" even in a general sense
if blue is not green or is not red or is not blue say nope
you would need to do something like the following:
if (operation != '+' && operation != '-' && operation != '/' && operation != '*') {
return false;
}
return true;
this way its like this
if blue is not green and is not red and is not blue say nope

Stupid C++ question about if-else

I have this code:
#include <iostream>
using namespace std;
int main()
{ char c='6';
if(c == '+' || '-' || '*' || '^' || '/' || '%')
{
cout<<"good";
}
else {cout<<"bad";}
return 0;
}
I want to write "good" if the char is '+' or '-' etc, and write "bad" if the char is anything else.
But this code writes "good" always with any char.
Where is the problem? Thanks.
if(c == '+' || '-' || '*' || '^' || '/' || '%')
parses to
if( (c == '+') || ('-'!=0) || ('*'!=0 || ('^'!=0) || ('/'!=0) || ('%'!=0))
It will always evaluate to true because '-' is indeed not equal to zero. Of course it's a sort of flaw with the type-safety that a char "degrades" to a boolean and evaluates to true. (The proper type-safe solution would be simply not to compile your code unless you explicitly cast).
What you wanted to know was whether c is one of those values. There are many ways to do that. Apart from an indented if you could use a library feature:
C function strchr:
if( strchr( "+-*^/%", c ) != NULL )
switch statement
switch (c )
{
case '+': case '-': case '*': case '^': case '/': case '%':
// true logic
break;
default:
// false logic
};
regex
(overkill here but purists would like it).
std::bitset
This takes a lot of "setup" but if you have a fixed set of chars and lots of variable chars to see if it exists in the set, this is the quickest way to do it.
// one-time setup
std::bitset<256> myCharSet;
myCharSet.set('+');
myCharSet.set('-');
myCharSet.set('*');
myCharSet.set('^');
myCharSet.set('/');
myCharSet.set('%');
// subsequently
if( myCharSet.test( static_cast<unsigned char>(c) ) )
{
// true logic
}
else
{
// false logic
}
static array
Similar solution to bitset but you don't mind wasting a few bytes.
static bool charset[256] = { false };
static bool init = false;
if( !init )
{
charset['+'] = true; // etc
init = true;
}
if( charset[ static_cast<unsigned char>(c) ] )
{
// true logic
}
else
{
// false logic
}
And you could make a class that does this that initialises from a string of the characters you want to check for (plus some logic as to whether a 0 byte is true or false, if the string you pass is null-terminated).
As with bitset this is constant-time lookup.
There are other options (eg with C++ std::string class using find and std::find) but these will do for now.
Change your if to:
if(c == '+' || c == '-' || c == '*' || c == '^' || c == '/' || c == '%')
or better yet:
switch (c)
{
case '+': case '-': case '*' : case '^' : case '/' : case '%':
cout << "good\n"; break;
default: cout << "bad\n"; break;
}
it should be
if(c == '+' || c == '-' || c == '*' || c == '^' || c == '/' || c == '%')
...
otherwise the expression is always evaluating to true. Any of those characters have a value different from 0, so true for c++.
Simple answer:'-' is the ASCII (the way the computer encodes character) value of that character - which is not 0 therefore it's true (computer's logic).
Same for all the other characters. So we got:
c == '+' || true || true || true || true || true which is always true.
What you should've done:
c == '+' || c == '-' || c == '*' || c == '^' || c == '/' || c == '%'
The mistake was probably you thinking "It needs to be equal to this or that or that or that" and so on. This happened to everyone, just remember that computers don't speak English.