我以前从未遇到过这个问题。当我按 enter 接受 userInput 时,程序不执行。有什么理由吗?
#include <iostream>
using namespace std;
struct node{
char data;
struct node* left;
struct node* right;
};
struct node* newNode(int idata){
struct node* node = new struct node;
node -> data = idata;
node -> left = NULL;
node -> right = NULL;
return node;
};
void getUserInfo(string &userInput, int &lengthString){
cout << "A string that contains a dot(.) will be invalid!" << endl;
cout << "Enter a string ->";
cin >> userInput;
// getting the length of string
lengthString = userInput.length();
}
void stringToChar(char *& charArray, int &lengthString){
for(int i = 0; i < lengthString; i++){
cin >> *(charArray+i);
}
}
int main(){
string userInput;
int lengthString = 0;
char * charArray;
charArray = new char [lengthString];
getUserInfo(userInput, lengthString);
stringToChar(charArray, lengthString);
/*struct node *root= newNode(9);
root ->left = newNode(20);
(*root).right = newNode(3);
cout << root->right->data;
*/
return 0;
}
最佳答案
您的问题是您的 stringToChar
方法中有另一个 cin
。假设您想将字符串变成一个 char 数组,(根据您的参数和您声明的内容来判断,这就是您想要的)我更改了方法来实现它。
void stringToChar(char *& charArray, int &lengthString, string &userInput){
for(int i = 0; i < lengthString; i++){
charArray[i]=userInput[i];
}
}
关于c++ - 当我按 enter 时程序不会继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49848057/