繁体   English   中英

为什么getline()需要按两次Enter键?

[英]why getline() need to press enter twice?

我的代码是

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(void)
{
int n0,n1,n2;
int move = 0;
long unsigned int arr[3][3];
string  str;
char color[4]={'B','G','C','\0'};
char col[40];
        while( getline(cin,str))
     {
           if(str[0]=='\0')continue;
           int temp=0,n=0;
           int i=0,j=0,k=0;
        do{     if(str[k]!=' ')temp=temp*10+(str[k]-48);
                else {
                    if(temp==0) {k++;continue;
                     }
                arr[i][j]=temp;
                temp=0;
                j++;
               if(j==3){j=0;i++;}}
              k++;
         if(str[k]=='\0' && str[k-1]!=' ')arr[i][j]=temp;
    }while(str[k]!='\0');
    str[0]='\0'; move = 0;


    for(;n!=3;n++)
    {


        for(j=0,k=0;j<3;j++)
        {
            temp=0;
            if(j==n)continue;
            if((j+1)%3==n)k=(j+2)%3;
            else k=(j+1)%3;

            temp= arr[1][n]+ arr[2][n]+arr[0][j]+ arr[2][j]+arr[0][k]+ arr[1][k];  //total movement

            if(n==0 && move==0){move = temp;
                    n0=n,n1=j,n2=k;}
            else if(move>temp){move = temp;
                            n0=n,n1=j,n2=k;}
            else if(move == temp){ if(color[n0]>color[n]){n0=n,n1=j,n2=k;}
                                    else if (color[n0]==color[n] && color[n1]>color[j]){n0=n,n1=j,n2=k;}
                                    }



    }
}


    col[0]=color[n0],col[1]=color[n1],col[2]=color[n2],col[3]=' ',col[4]='\0';


    cout<<col<<move;

}

    return 0;
}

在这里,当我输入一个字符串并按Enter时 ,Courser只会转到新行,而代码不会前进。 我必须按Enter两次才能继续运行程序。 但它应该可以一键输入。

这里有什么问题?

我的输入和输出以及它们之间的额外“输入”

我只是尝试运行您的代码。 它工作正常,只需要输入一次即可。 这就是我在编译器上运行的内容。

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
int n0,n1,n2;
int move = 0;
long unsigned int arr[3][3];
string  str;
char color[4]={'B','G','C','\0'};
char col[40];
    while( getline(cin,str) )
      {
        cout<<"hello";
  }
return 0;

}

输出值

输出量

输出2

在此处输入图片说明

输出3

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM