1

Early on I am able to use cin.getline(input2, 40) to assign a line to input2. I do it as well so that I have two c strings with information in them.

Later I enter a switch where people can over write one of the c string lines. When it executes cin.getline(input2, 40); as the option, all it does is delete the information at that string.

I've tried removing assigning the cstring at the beginning, allowing it to be changed in the switch, but it isn't assigned, rather just goes back to the switch statement.

I've tried using cin.getline(input2, 40, '\n') so that cin waits for a new line before continuing, but it seems it is automatically getting one.

The switch option for input is 'i'.

I've struggled with it for the last hour or so, so now I humbly ask for your assistance.

Bonus points to someone that finds out why when I put in x, the switch statement doesn't end.

            #include<iostream>
            #include<conio.h>
            #include<string>
            #include<algorithm>

            using namespace std;

            char rev (char *);
            bool compareChar (char *, char *);
            int getLength(char *);
            string concat1 (char *, char *);

            int main()
            {
                char input1[80];
                char input2[80];
                char *input1Ptr = input1;
                char *input2Ptr = input2;
                int length = 0;
                string concat = "None";

                cout << "Input a line of text for line 1: ";
                cin.getline(input1, 40);
                cout << "Input a line of text for line 2: ";
                cin.getline(input2, 40);

                char in;
                while (in != 'x' || in != 'X'){

                cout << endl;
                cout << endl;

                cout << "Enter i for Input" << endl;
                cout << "Enter r for Reverse" << endl;
                cout << "Enter c for compare" << endl;
                cout << "Enter o for concatenate" << endl;
                cout << "Current concatenate is " << concat << endl;
                cout << "String a" << endl;
                cout << "String b" << endl;
                cout << "Enter x to exit" << endl;

                cin >> in;

                switch (in) {
                    case 'i':
                    case 'I':
                        cout << endl;
                        cout << "Rewrite Line 2: " << endl;
                        cin.getline(input2, 40, '\n');
                        cout << input2;
                        break;

                    case 'r':
                    case 'R':
                        length = getLength(input1);
                        reverse(input1, input1 + length);
                        cout << input1;
                        break;

                    case 'c':
                    case 'C':
                        if (compareChar(input1Ptr, input2Ptr) == 1){
                            cout << "True" << endl;
                        }
                        else {
                            cout << "False" << endl;
                        }
                        break;

                    case 'o':
                    case 'O':
                        concat = concat1(input1, input2);
                        cout << concat;
                        break;

                    case 'a':
                    case 'A':
                        cout << input1;
                        break;

                    case 'b':
                    case 'B':
                        cout << input2;
                        break;

                    default:
                        cout << "Syntax Error";
                        in = 'x';
                        break;
                    }
                }
            }

            string concat1 (char *a, char *b){
                string con;
                con += a;
                con += b;

                return con;
            }

            char rev (char *a){
                int length1 = 0;
                int counter = 0;
                int incr = 0;
                char backward[40];

                while (*a) {
                    cout << *a << " " ;
                    length1++;
                    a++;
                }

                counter = length1;

                while (counter > 0){
                    backward[incr] = a[counter];
                    counter--;
                    incr++;
                }

                return backward[40];
            }

            bool compareChar (char *a, char *b){
                int counter = 0;
                bool comparing = false;

                while (counter < 40){
                    if (a[counter] == b[counter]){
                        counter++;
                        comparing = true;
                    }
                    else {
                        counter = 40;
                    }
                }

                return comparing;
            }

            int getLength(char *input1)
            {
                int length = 0;

                while (*input1){
                    input1++;
                    length++;
                }

                return length;
            }
Greg
  • 107
  • 2
  • 14
  • `backward[40]` is out of bounds for `char backward[40]`. – crashmstr Jan 16 '17 at 22:50
  • [An oldie but a goodie](http://stackoverflow.com/q/5739937/1460794). – wally Jan 16 '17 at 22:51
  • Heh, yeah I stopped using that function and instead used a sort function I think. That function in there is legacy, and I couldn't figure out how that worked, which is why I switched from that. Good catch though, I couldn't figure it out. – Greg Jan 16 '17 at 22:52
  • I hard previously set it to just (in != 'x'), but that didn't seem to work. I gave it a try with both cap and lower case, still no success with that. Good to see that I had a logic error there when I tried doing something different. With it just being (in != 'x'), what would be required to fix it? – Greg Jan 16 '17 at 22:55
  • I don't understand. You use `string` as a return type, but you don't use `string` variables for input. You should be consistent, use `std::string` variables for input strings. – Thomas Matthews Jan 16 '17 at 23:00
  • The reason for it is that the book wants me to concatenate two c strings, but am having issues with doing so. Instead I used the two c-strings and assigned it to a string. Probably the wrong way to do it for sure. – Greg Jan 16 '17 at 23:11

2 Answers2

2

For your exit condition, let's just show part of the truth table:

in     |   in != 'x'     |  in != 'X'     |  (in != x || in != 'X')
--------------------------------------------------------------------
'a'            T                T                      T
'A'            T                T                      T
'b'            T                T                      T
'x'          false              T                      T
'X'            T              false                    T
'7'            T                T                      T

That's why the loop continues running for all possible inputs. You should probably read about DeMorgan's Theorem and think about how it applies here.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I hard previously set it to just (in != 'x'), but that didn't seem to work. I gave it a try with both cap and lower case, still no success with that. Good to see that I had a logic error there when I tried doing something different so I can narrow that down. – Greg Jan 16 '17 at 23:11
1

that is a problem with the input buffer so an endline character is still there so if the user enters i to input then he cannot to correct this you have to clear the input buffer:

    case 'i':
    case 'I':
        cout << endl;
        cout << "Rewrite Line 2: " << endl;
        cin.ignore(1, '\n'); // add this
        cin.getline(input2, 40, '\n');
        cout << input2;
        break;

also why checking the value of in before initializing it:

char in;
while (in != 'x' || in != 'X'){

correct it to:

char in = '\0'; // for example
cin >> in; // then evaluate

while (in != 'x' || in != 'X'){
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • 1
    Wonderful, thank you. The cin.ignore(1, '\n'); did the trick. I also set the defined value of 'in' and that seems to be working now as well. – Greg Jan 16 '17 at 22:58