Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何期望来自重定向和用户输入的输入_C++_File_Stdin_Cin_Io Redirection - Fatal编程技术网

C++ 如何期望来自重定向和用户输入的输入

C++ 如何期望来自重定向和用户输入的输入,c++,file,stdin,cin,io-redirection,C++,File,Stdin,Cin,Io Redirection,所以我也在努力 a) 允许用户输入字符串,直到他们键入exit 或 b) 将文件从标准输入(a.out

所以我也在努力

a) 允许用户输入字符串,直到他们键入exit

b) 将文件从标准输入(
a.out
)重定向到文件末尾,然后终止

我对代码的尝试:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(){

    string input = "";
    while(true){
       cout << "Enter string: ";
       while(getline(cin, input)){
       if(input == "exit" || cin.eof()) goto end;
       cout << input << "\n";
       cout << "Enter string: ";                       
     }

    }
    end:
    return 0;



}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串输入=”;
while(true){

cout好的,在第一种情况下,不建议使用goto,您可以使用布尔数据类型来实现您的目标:

int main(){
bool flag = true;

string input = "";
while(flag){
    cout << "Enter string: ";
    while(getline(cin, input)){
        if(input == "exit" || cin.eof()) {
            flag = false;
            break;
        }
        cout << input << "\n";
        cout << "Enter string: ";
    }

  }


 return 0;
}
intmain(){
布尔标志=真;
字符串输入=”;
while(旗帜){
不能包含
#包括
#包括
使用名称空间std;
int main(){
字符串输入=”;
当(cin&&COUTY)您的代码为我工作时
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main(){
  string input = "";
  while(cin && cout<<"Enter string: " && getline(cin, input)){
    //check both cout and cin are OK with the side effect of
    // writing "Enter string" and reading a line

    if(input == "exit") return 0; //no need for a goto
    cout << input << "\n";
  }
  if(cin.eof()) return 0; //ended because of EOF
  return 1; //otherwise, the loop must have broken because of an error
}