我需要在不知道事先知道的情況下讀入一串字符串,並在讀取它們時打印它們。所以我決定使用while(!feof(stdin))
作爲EOF indicator.Here是我的代碼:如何讀取C++中的一串字符串?
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main(void)
{
char* str;
std::cout<<"\nEnter strings:";
while(!feof(stdin))
{
std::cin>>str;
std::cout<<"\nThe string you entered is"<<str;
}
return 0;
}
由於某種原因,上面的代碼表示segmentation fault
後,我進入第一string.Can有人建議應該是一個修正。
這不是c,它是C++。你需要用c或C++來完成嗎?差異是巨大的! –
您好fredik感謝標籤編輯! – station