早期我可以使用cin.getline(input2,40)为input2分配一行。 我也这样做,所以我有两个带有信息的c字符串。
稍后我输入一个开关,人们可以在其中写一个c弦线。当它执行cin.getline(input2,40)时;作为选项,它所做的只是删除该字符串的信息。
我尝试在开头删除分配cstring,允许在交换机中更改它,但它没有被分配,而只是返回到switch语句。
我尝试过使用cin.getline(input2,40,' \ n')以便cin在继续之前等待换行,但似乎它会自动获得一行。
输入的切换选项是' i'。
我在最后一个小时左右挣扎了,所以现在我谦卑地请求你帮助。
奖励指向某人发现为什么当我输入x时,switch语句不会结束。
#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;
}
答案 0 :(得分:2)
对于退出条件,让我们只显示真值表的一部分:
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
这就是为什么循环继续运行所有可能的输入。您应该阅读DeMorgan的定理,并考虑它在这里的应用。
答案 1 :(得分:1)
这是输入缓冲区的一个问题,因此endline character
仍然存在,所以如果用户输入i
进行输入,那么他无法纠正这个问题,你必须清除输入缓冲区:
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;
为什么在初始化之前检查in
的值:
char in;
while (in != 'x' || in != 'X'){
更正:
char in = '\0'; // for example
cin >> in; // then evaluate
while (in != 'x' || in != 'X'){