|  | 
|  | 
| back to board | AC but have a weird problem Posted by fyq123  19 Oct 2019 00:51I use c++,my code is behind. But there is a weird problem . When i define n as int and use cin>>n,the program can't receive the name have more than two word or have symbols like '-'. So i have to define n as a string and transfer the string into a int,why this strange thing happen?
 #include<iostream>
 #include<string.h>
 #include<cstring>
 using namespace std;
 
 int main()
 {
 char room[4][1000][210];
 memset(room,0,sizeof(room));
 int k=0,nth[4]={0};
 char n[10];
 cin.getline(n,10,'\n');
 int len=10;
 while(n[len]=='\0'){len--;}
 int temp=1;
 for(int i=len;i>=0;i--)
 {
 k+=(n[i]-48)*temp;
 temp*=10;
 }
 
 for(int i=0;i<k;i++)
 {
 char temp[210],temp1[20];
 cin.getline(temp,210,'\n');
 cin.getline(temp1,20,'\n');
 
 if(temp1[0]=='S')
 {
 strcpy(room[0][nth[0]],temp);
 nth[0]++;
 }
 else if(temp1[0]=='H')
 {
 strcpy(room[1][nth[1]],temp);
 nth[1]++;
 }
 else if(temp1[0]=='G')
 {
 strcpy(room[2][nth[2]],temp);
 nth[2]++;
 }
 else {
 strcpy(room[3][nth[3]],temp);
 nth[3]++;
 }
 }
 /* omit so this is not a correct solution*/
 }
 | 
 | 
|