0

Here is my solution to the problem in the codeforces http://codeforces.com/problemset/problem/499/B . I am facing problem in inputting the string. It termintes after line 10 (see code) before i give input str to it and output some weird chars.

Input: 4 3
       codeforces codesecrof
       contest round
       letter message
Output:




#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main(){
    int N, M;
    string str;

    cin>>N>>M;
    string A[M], B[M];

    for(int i=0; i<M; i++)
    cin>>A[i]>>B[i];     // line 10

    getline(cin,str);

    char res[N+1];


    for(int i=0; i<M; i++){
        int j= str.find(A[i]);
        int k;
        int x=0;

        if(B[i].length() < A[i].length()){

            for(k=j; k<B[i].length(); k++){
                res[k]= B[i][x];
                x++;
            }
        }else{
            for(k=j; k<B[i].length(); k++){
                res[k]= B[i][x];
                x++;
            }

        }

        res[k]=' ';

    }

    for(int i=0; i<=N; i++ )
        cout<<res[i];

    cout<<endl;

    return 0;
}

1 Answer 1

3

There is a newline character left in the input stream after:

cin>>N>>M;

You need a line of code that will read and discard the rest of the line. Add a line;

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

after that line.

Add

#include <limits>

to be able to use std::numeric_limits.

Sign up to request clarification or add additional context in comments.

3 Comments

after adding above line i got bunch of errors like : 1. ‘numeric_limits’ is not a member of ‘std’ 2. no matching function for call to ‘max()’ 3. expected primary-expression before ‘>’ token
@surajbora need to #include <limits>
It worked but may i get the details of the errors for future refrence :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.