快速变脸的cin
											萌新发帖是这样的,我在国外回复了一个小伙的疑问,他想编译出一个随机提问器。
我最终发出的代码是这样的:
 程序代码:
程序代码:#include<iostream>    //input and output
#include<stdlib.h>     //rand() and srand()
#include<time.h>      //time()
#include<string.h>    //use strcmp() to compare two strings
using namespace std;
int main(){
    char* questions[4]={"2*37-6","1+1","8*7*6*5*4*3*2*1*0*(-1)","0-9"};
    char* answers[4]={"68","2","0","-9"};
    srand(time(0)*3);   //set random number seed
    int choice=rand()%4;   //a random number between 0 and 3,actually
    cout<<"Now I'm gonna ask you a question!\n";
    cout<<questions[choice];   //output the random question
    cout<<"\nPlease input your answer:";
    char input[100];
    cin>>input;
    if(strcmp(input,answers[choice])==0)  //0 means they are the same
        cout<<"Oh my god!You answered it correctly.\n";
    else
        cout<<"Haha!That's not correct.The correct answer is:"<<answers[choice]<<".\n";
    system("pause"); //pause
    return 0;
}虽然有一些警告,但这些代码在我的Visual Studio 2010和C++ Shell中都能正常运行。
发送的代码没问题,可是我在编辑代码的时候,发现了一个问题——
当我把
char input[100];
改成
char* input;
时,程序在中间就卡出去了。
我马上做了一个简单的实验:
 程序代码:
程序代码:#include<iostream>
using namespace std;
int main(){
    char* buffer;
    cin>>buffer;
    cout<<buffer;
    return 0;
}结果在上面的代码中,buffer能够正常读取,程序能正常退出。
为什么一个cin能正常用,一个cin直接把我程序卡退了呢



求大佬帮助!
[此贴子已经被作者于2021-2-2 20:55编辑过]



 
											





 
	    

 
	


