我正在为我的计算机科学课开发一个项目,无论出于什么原因,一旦我的代码进入cin.getline代码,它就结束了,并且不允许我输入任何内容,我也不知道为什么。我认为问题与C字符串末尾的“空”字符有关,但是我不确定它是否可以正常工作,直到我添加了“确定是要编码还是要解码”部分和如果声明。这段代码还没有完全完成,我只需要修复导致此问题继续下去的任何原因即可。
因此,从本质上讲,它不需要上面的整个部分和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;
}
输出应该允许我自己编写代码行,然后将其加扰后再看到,而加扰器将起作用。
答案 0 :(得分:0)
我使用R Sahu发布的线程中发现的\ n字符的ignore函数修复了它。
我基本上只是添加了: cin.ignore(std :: numeric_limits :: max(),'\ n') 在我的cin.getline之前。