-1

I'm taking my first coding class and I've hit a road block. I'm writing a program that will need to pick out the second and third characters in a set of either 4 or 5 characters that gives a corresponding output based off the values it finds.

Here's an example of some of what I have thus far.

include <iostream>
#include <string>
using namespace std;
int main()
{ //start of main function
system("cls");
string parts = " ";
char input = ' ';


cout << "******** Part Number Program ********" << endl << endl;
cout << "Enter the part number (x to end): ";
getline(cin, parts);

while (parts != "x" || parts != "X")
{   //start while



    if (parts.substr(1, 1) == "M" && parts.substr(2, 1) == "P")//start if
    {
        cout << "Mail - Prioity" << endl << endl;
    }//end if
    else if (parts.substr(1, 1) == "F" && parts.substr(2, 1) == "S")//start if
    {
        cout << "FedEx - Standard" << endl << endl;
    }//end if

This works except with the exception of lowercase letters. For example, if I were to enter 7MP7 I'd get the output Mail-Prioity. However, if I enter 7mp7 I'd get my error message. Any suggestion on how I can fix this?

John0121
  • 1
  • 3
  • https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jesper Juhl Feb 21 '18 at 20:05
  • 1
    You could use [`std::toupper`](http://en.cppreference.com/w/cpp/string/byte/toupper) to convert your characters to upper case. – François Andrieux Feb 21 '18 at 20:06
  • Rather than comparing 1 length substrings, it seem more intuitive to compare characters. For example, `parts[1] == 'M'` instead of `parts.substr(1, 1) == "M"`. – François Andrieux Feb 21 '18 at 20:07
  • 2
    `parts != "x" || parts != "X"` this will always be `true`. I think you wanted `parts != "x" && parts != "X"` – Killzone Kid Feb 21 '18 at 20:09
  • @FrançoisAndrieux I guess I'm a little lost on where I would add the toupper? I've tried it and it doesnt work, but I'm positive thats because I'm using it wrong – John0121 Feb 21 '18 at 20:12
  • 1
    Did you notice that `parts.substr(1, 1) == "M" && parts.substr(2, 1) == "P"` is the same as `parts.substr(1, 2) == "MP"` ? – Slava Feb 21 '18 at 20:18

4 Answers4

1
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

int main(){
    string data = "This Will Be All UpperCase When After The Next Line Code";
    transform(data.begin(), data.end(), data.begin(), ::toupper);
    cout << data << endl;
}
KarenAni
  • 64
  • 6
  • Search the internet for "c++ transform string tolower" for an example of how to use library functions to convert to all lower case. – Thomas Matthews Feb 21 '18 at 20:55
0

You can compare the characters using a case-insensitive comparison function such as strcasecmp. See documentation here.

if (srtcasecmp(parts.substr(1, 1).c_str(), "M") == 0 && srtcasecmp(parts.substr(2, 1).c_str(), "P") == 0) {//your code}

Or even cleaner:

if (srtcasecmp(parts.substr(1, 1).c_str(), "M") & srtcasecmp(parts.substr(2, 1).c_str(), "P") == 0) {//your code}
SoroushA
  • 2,043
  • 1
  • 13
  • 29
  • Why do you consider using binary `and` a cleaner solution? I think it is not only not cleaner but unnecessary less efficient. Plus you are trying to pass `std::string` where `const char *` is expected – Slava Feb 21 '18 at 20:29
  • @Slava fixed using c_str() – SoroushA Feb 21 '18 at 20:35
  • I still do not get why you consider using bitwise and "cleaner" – Slava Feb 21 '18 at 20:42
0

As pointed out in one the comments,

while (parts != "x" || parts != "X")

is not right. The conditional will always evaluate to true. It needs to be:

while (parts != "x" && parts != "X")

You can simplify the logic in the loop using:

char secondLetter = std::tolower(parts[1]);
char thirdLetter = std::tolower(parts[2]);

if ( secondLetter == 'm' && thirdLetter == 'p' )
{
  // ...
}
else if ( secondLetter == 'f' && thirdLetter == 's' )
{
  // ...
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You should change your input string to uppercase or lowercase using one of the methods you like from Convert a String In C++ To Upper Case and then just check:

// convert parts to uppercase here
while (parts != "X")
{   
    if (parts.substr(1, 2) == "MP" )
    {
        cout << "Mail - Prioity" << endl << endl;
    }
    ...

Note: though you should comment your code, but to comment obvious things, for example this is start of if (like you cannot get it from code itself) is considered bad practice.

Slava
  • 43,454
  • 1
  • 47
  • 90