字符串 std::logic_error: basic_string::_S_construct null 无效

-1 c++ stl

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string S = 0; 
    int T,R;
    
    cin >> S >> R;
    
    for(int i = 0; i < T; i++)
    {
        for(int k = 0; k < S.length(); k++)
        {
            for(int j = 0; j < R; j++)
            {
                cout << S[k];
            }
        }
        cout << endl;
    }
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误池语句为:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string S = 0; 
    int T,R;
    
    cin >> S >> R;
    
    for(int i = 0; i < T; i++)
    {
        for(int k = 0; k < S.length(); k++)
        {
            for(int j = 0; j < R; j++)
            {
                cout << S[k];
            }
        }
        cout << endl;
    }
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

m88*_*m88 5

string S = 0;被解释为std::string S{nullptr};禁止

就写string S;吧。

  • 下一个问题是“T”未初始化。 (5认同)