为什么字符串用getline输入就是全错误的啊,求大佬解答

P1914 小书童——凯撒密码

gpygy @ 2022-03-23 08:23:50


#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    n = n % 26;
    string a;
   cin.get();
    getline(cin,a);
    for(int i = 0;i < a.length();i++)
    {
        int j = 'z' - a[i];
        if(j >= n)
        {
            a[i] = a[i] + n;
        }
        else
        {
            a[i] = 'a' + n - j - 1;
        }
    }   
    cout<<a;
    return 0;
 } 

by Jerrlee✅ @ 2022-03-23 08:30:33

@gpygy https://www.luogu.com.cn/record/72116091

两端 getline 即可通过,大概是第一次读入时读入了 \n


by rxjdasiwzl @ 2022-03-23 09:24:59

@gpygy 混用 std::cin >> nstd::getline


by gpygy @ 2022-03-23 09:34:33

@Jerrlee✅ 好的,谢谢


|