【问题标题】:GetLine Function For C-String Not Working ProperlyC-String 的 GetLine 函数无法正常工作
【发布时间】:2019-03-24 04:31:48
【问题描述】:

我正在为我的计算机科学课做一个项目,无论出于何种原因,一旦我的代码到达我的 cin.getline 代码,它就会结束并且不允许我输入任何内容,我不知道为什么。我认为这个问题与 c 字符串末尾的“null”字符有关,但我不太确定它是否正常工作,直到我添加了“确定是编码还是解码部分”和if 语句。这段代码还没有完全完成,我只需要修复导致它继续运行的任何问题。

所以本质上,它可以在没有上面的整个部分和 if 语句的情况下工作。但是一旦我添加了这些,我的 cin.getline 函数就不起作用了。但是,如果我执行 cin >> 行,它甚至可以使用上面的代码。我需要 getline 函数,因为我会将它写入文件,而且我还需要抓取那些性感的空格 ' '。

#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;
}

输出应该允许我自己编写代码行并看到它被加扰,加扰器可以工作。

【问题讨论】:

标签: c++ getline c-strings


【解决方案1】:

我使用 R Sahu 发布的线程中的 \n 字符的忽略功能修复了它。

我基本上只是添加了: cin.ignore(std::numeric_limits::max(), '\n') 在我的 cin.getline 之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-28
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多