0

I'm working on a project for my computer science class, and for whatever reason, once my code gets to my cin.getline code it just ends and doesn't allow me to enter anything in and I don't know why. I think the problem has something to do with the "null" character at the end of a c-string, but I'm not quite sure as it was working fine until I added the "Determining Whether to CODE or DECODE section" and the if statement. This code isn't fully complete yet, I just need to fix whatever is causing this to move on though.

So essentially, it works without the whole section above it and the if statement. But once I add those, my cin.getline function doesn't work. However, if I do a cin >> line, it will work even with the code above it. I need the getline function because I will be writing it to a file, and I also need to grab those sexy spaces ' '.

#include <iostream>
#include <cstring>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;

//Function Prototypes:
string fileAddress(string);
void swap(char &, char &);
string code(int, char [], char [], char []);

//Main Function:
int main()
{
        //Alphabet Section:
    //------------------------------------------------------------------------------------------------------------------
        //Declaring Variables Relating to Alphabet:
        int size = 29;
        char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ., ";
        char sAlphabet[size];
        int x;
        int y;

        //Copying Alphabet & Outputting Test Value:
        strcpy(sAlphabet, alphabet);

        //Scrambling Alphabet
        unsigned seed = time(0);
        srand(seed);

        for(int i = 0; i < 10000; i++)
        {
            //Initializing Random Numbers to Swap:
            x = rand()%29;
            y = rand()%29;

            //Swapping Values
            swap(sAlphabet[x], sAlphabet[y]);
        }

        //Testing Scrambled Alphabet:
        cout << sAlphabet << endl << endl;
    //------------------------------------------------------------------------------------------------------------------


        //Determining Whether to CODE or DECODE Section:
    //------------------------------------------------------------------------------------------------------------------
        //Response Variables:
        string response;

        //Gathering Choice From User:
        cout << "If you wish to code a message, please type CODE." << endl;
        cout << "If you wish to decode a message, please type DECODE.";
        cin >> response;
    //------------------------------------------------------------------------------------------------------------------


        //Writing Coded Message to File Section:
    //------------------------------------------------------------------------------------------------------------------
        if(response == "CODE")
        {
            //Code Variables:
            int length = 100;
            char line[length];
            char cline[length];
            string codedLine;

            //Gathering Line from User:
            cout << "Please enter a message in all CAPS that you wish to code.";
            cin.getline(line, length);

            //Copying Line:
            strcpy(cline, line);

            //Gathering length of Line:
            length = strlen(line);

            codedLine = code(length, line, cline, sAlphabet);
            cout << line << endl;
            cout << codedLine<< endl;
        }
        else
            cout << "You suck";

        return 0;
}

//Creating Swap Function:
void swap(char &value1, char &value2)
{
    char temp;
    temp = value1;
    value1 = value2;
    value2 = temp;
}

string code(int length, char line[], char cline[], char sAlphabet[])
{
    for(int i = 0; i < length; i++)
    {
        //Letter A:
        if(line[i] == 'A')
        {
            cline[i] = sAlphabet[0];
        }
            //Letter B:
        else if(line[i] == 'B')
        {
            cline[i] = sAlphabet[1];
        }
            //Letter C:
        else if(line[i] == 'C')
        {
            cline[i] = sAlphabet[2];
        }
            //Letter D:
        else if(line[i] == 'D')
        {
            cline[i] = sAlphabet[3];
        }
            //Letter E:
        else if(line[i] == 'E')
        {
            cline[i] = sAlphabet[4];
        }
            //Letter F:
        else if(line[i] == 'F')
        {
            cline[i] = sAlphabet[5];
        }
            //Letter G:
        else if(line[i] == 'G')
        {
            cline[i] = sAlphabet[6];
        }
            //Letter H:
        else if(line[i] == 'H')
        {
            cline[i] = sAlphabet[7];
        }
            //Letter I:
        else if(line[i] == 'I')
        {
            cline[i] = sAlphabet[8];
        }
            //Letter J:
        else if(line[i] == 'J')
        {
            cline[i] = sAlphabet[9];
        }
            //Letter K:
        else if(line[i] == 'K')
        {
            cline[i] = sAlphabet[10];
        }
            //Letter L:
        else if(line[i] == 'L')
        {
            cline[i] = sAlphabet[11];
        }
            //Letter M:
        else if(line[i] == 'M')
        {
            cline[i] = sAlphabet[12];
        }
            //Letter N:
        else if(line[i] == 'N')
        {
            cline[i] = sAlphabet[13];
        }
            //Letter O:
        else if(line[i] == 'O')
        {
            cline[i] = sAlphabet[14];
        }
            //Letter P:
        else if(line[i] == 'P')
        {
            cline[i] = sAlphabet[15];
        }
            //Letter Q:
        else if(line[i] == 'Q')
        {
            cline[i] = sAlphabet[16];
        }
            //Letter R:
        else if(line[i] == 'R')
        {
            cline[i] = sAlphabet[17];
        }
            //Letter S:
        else if(line[i] == 'S')
        {
            cline[i] = sAlphabet[18];
        }
            //Letter T
        else if(line[i] == 'T')
        {
            cline[i] = sAlphabet[19];
        }
            //Letter U:
        else if(line[i] == 'U')
        {
            cline[i] = sAlphabet[20];
        }
            //Letter V:
        else if(line[i] == 'V')
        {
            cline[i] = sAlphabet[21];
        }
            //Letter W:
        else if(line[i] == 'W')
        {
            cline[i] = sAlphabet[22];
        }
            //Letter X:
        else if(line[i] == 'X')
        {
            cline[i] = sAlphabet[23];
        }
            //Letter Y:
        else if(line[i] == 'Y')
        {
            cline[i] = sAlphabet[24];
        }
            //Letter Z:
        else if(line[i] == 'Z')
        {
            cline[i] = sAlphabet[25];
        }
            //Character Period:
        else if(line[i] == '.')
        {
            cline[i] = sAlphabet[26];
        }
            //Character Comma:
        else if(line[i] == ',')
        {
            cline[i] = sAlphabet[27];
        }
            //Character Space:
        else if(line[i] == ' ')
        {
            cline[i] = sAlphabet[28];
        }
        else
            cout << "Error" << endl;
    }
    return cline;
}

The output should be allowing myself to write in the line of code and see it back scrambled, which the scrambler works.

  • [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – R Sahu Mar 24 '19 at 04:34
  • That's too much code. If the above post does not help you resolve your problem, please post a [mcve]. – R Sahu Mar 24 '19 at 04:35
  • You can collapse your whole `if .. else if .. else` condition into one line as `cline[i] = sAlphabet[(int) line[i] - 'A']` but you need `if` condition for all non-alphabet character like period comma and space – Mohit Mar 24 '19 at 04:46
  • Thanks guys, very helpful. Sorry about the excess code. – Christian Martinez Mar 24 '19 at 04:56

1 Answers1

0

I fixed it using the ignore function for the \n character found in the thread that R Sahu posted.

I basically just added: cin.ignore(std::numeric_limits::max(), '\n') before my cin.getline.