试图从时间字符串中获取 hour int 但在 c++ 中出现此错误
Trying to get hour int from time string but getting this error in c++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int h;
string s1;
getline(cin,s1);
cout<<s1;
h=10*(s1[0]-'0')+1*(s1[1]-'0');
cout<<h;
}
}
我在下面给出了输入
1
11:01 PM
得到这个作为输出
-560
我想将时间的小时部分从字符串转换为整数。
检查这个:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int h;
string s1;
getline(std::cin.ignore(), s1);
//getline(cin,s1);
cout<<s1 << endl;
h=10*(s1[0]-'0')+1*(s1[1]-'0');
cout<<h << endl;
}
}
我强烈建议您阅读 this post 以了解不同之处。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int h;
string s1;
getline(cin,s1);
cout<<s1;
h=10*(s1[0]-'0')+1*(s1[1]-'0');
cout<<h;
}
}
我在下面给出了输入
1
11:01 PM
得到这个作为输出
-560
我想将时间的小时部分从字符串转换为整数。
检查这个:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int h;
string s1;
getline(std::cin.ignore(), s1);
//getline(cin,s1);
cout<<s1 << endl;
h=10*(s1[0]-'0')+1*(s1[1]-'0');
cout<<h << endl;
}
}
我强烈建议您阅读 this post 以了解不同之处。