What's the difference between PAT1078 string compression and decompression of scanf ("% c\ n", & ch) and cin > > ch + getchar ()?

title
https://pintia.cn/problem-set.

related codes

-sharpinclude <stdio.h>
-sharpinclude <string>
-sharpinclude <iostream>
using namespace std;
int read()
{
    char ch = getchar();
    int f = 1;
    int x = 0;
    while (ch < "0" || ch > "9") { if (ch == "-")f = 0; ch = getchar(); }
    while (ch >= "0" && ch <= "9") { x = x * 10 + ch - "0"; ch = getchar(); }
    return f ? x : x * -1;
}
int main()
{
    char c;
    // scanf("%c\n",&c); //  scanf  cin+getchar
    cin >> c;
    getchar();
    string s;
    getline(cin,s);
    if(c == "C")
    {
        for(int i = 0;i < s.length();)
        {
            int count = 0;
            char t = s[i];
            while(s[i] == t)
            {
                i PP;
                count PP;
            }
            if(count != 1)
            {
                printf("%d",count);
            }
            printf("%c",t);
        }
    }
    else
    {
        for(int i = 0;i < s.length();i PP)
        {
            if(s[i] >= "0" && s[i] <= "9")
            {
                int temp = 0;
                while(s[i] >= "0" && s[i] <= "9")
                {
                    temp = temp * 10 + s[i] - "0";
                    i PP;
                }
                for(int j = 0;j < temp;j PP)
                {
                    printf("%c",s[i]);
                }
                if(temp == 0)
                {
                    printf("%c",s[i]);
                }
            }
            else
            {
                printf("%c",s[i]);
            }
        }
    }
    printf("\n");
    
    return 0;
}


what result do you expect? What is the error message actually seen?

I don"t quite understand what metaphysics this is. After asking several people, they all said it was the same. At first, I found a blog with a format error-wa on the Internet like me. But it doesn"t say why. Later, after reading several problem solutions, I tried to change scanf into cin. What"s the difference between scanf ("% cn") and cin+getchar? don"t they all end up eating newline characters in order to read a line string

?

update: I tried to change it to scanf ("% c", & c); + getchar (); can ac the title. I want to know the n in scanf ("% cn", & c). My original intention is to use it to read line breaks. Can"t I read it

?
CPP c
Jul.30,2021

cin is not real-time because it uses buffers and usually flushes when it is full.
for characters: cin ignores spaces and carriage returns. Scanf ("% c", & I) is equivalent to I = getchar (), newline characters and carriage returns are read in.
scanf is the formatted input and printf is the formatted output.
cin is the input stream and cout is the output stream. It is a little less efficient, but it is easy to write.
formatting output is more efficient, but it is troublesome to write code.
stream output is less efficient, but easy to write.

int i; 
cout<<'a'; 
cin>>i; 
cout<<'b'; 

the above programs are usually encountered, and the running results do not see any output. Enter an integer, such as 3, and then press enter and ab is displayed at the same time. However, this situation does not happen very often, but occasionally occurs in some large-scale projects, because the character a goes to the buffer first, but it is not output, and so on, after the input iMagne b enters the buffer zone, it is output together. Stream input is pretty much the same.
Please like it or accept it if it is helpful. Thank you.

Menu