2

I'm trying to make my code work, actually it works but not that well. The code stops after 'enter' appeared. I want to make my code work until the end of file input from user.

 #include<iostream>
using namespace std;

int main(){
    char input[2000];


    cin.getline(input, sizeof(input));

    int lol = strlen(input);
    int boing = 0;
    for (int p = 0; p < lol; p++)
    {

        if (input[p] == '\"')
        {
            boing++;
            if (boing % 2 == 1)
            {
                cout << '\`'<<'\`';
            }
            if (boing % 2 == 0)
            {
                cout << '\''<<'\'';
            }
        }
        else
            cout << input[p];
    }

    system("pause");
}

if we enter input these words

Is branched in ""my up strictly "remember. " Songs but chief has ham widow downs. Genius or so up vanity cannot. '''```Large do tried goi"'''ng" about water defer by. "Silent" son man she wished mother. Distrusts allowance do knowledge eagerness assurance additions to. We """"diminution preference "thoroughly if. "Joy deal pain ';`392view" much her time. Led young gay would now state."

my output become

Is branched in ''my up strictlyremember. ''

but it should be

Is branched in ''my up strictlyremember. '' Songs but chief has ham widow downs. Genius or so up vanity cannot. '''```Large do tried goi'''ng'' about water defer by.Silent'' son man she wished mother. Distrusts allowance do knowledge eagerness assurance additions to. We ''''diminution preference thoroughly if. ''Joy deal pain ';`392view much her time. Led young gay would now state.''

1
  • You are not doing yourself a favor by giving variables "funny" names. Commented May 1, 2017 at 18:12

3 Answers 3

5

getline will take input until the "enter" button is pressed. Your code runs fine on my machine. Just add string.h header file like this:

#include<string.h>

This is for the strlen function.

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

Comments

4

for this code you you need to two more header file

#include <string.h>
#include <stdlib.h>

your full code will be

#include<iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main()
{
    char input[2000];


    cin.getline(input, sizeof(input));

    int lol = strlen(input);
    int boing = 0;
    for (int p = 0; p < lol; p++)
    {

        if (input[p] == '\"')
       {
           boing++;
           if (boing % 2 == 1)
           {
               cout << '\`'<<'\`';
           }
           if (boing % 2 == 0)
           {
              cout << '\''<<'\'';
           }
      }
      else
        cout << input[p];
   }

   cout<<endl;   //this line is for making ur code look nice

   system("pause");
}

but i personally suggest you to use c++ string class for string handling it easy and very useful

using string class your code will be

#include<iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{

   string input;

   getline(cin,input);

   int lol = input.size();

   int boing = 0;
   for (int p = 0; p < lol; p++)
   {

      if (input[p] == '\"')
      {
          boing++;
          if (boing % 2 == 1)
          {
             cout << '\`'<<'\`';
          }
          if (boing % 2 == 0)
          {
              cout << '\''<<'\'';
          }
      }
      else
          cout << input[p];
   }

   cout<<endl;
   system("pause");

   return 0;

}

Happy Coding

Comments

4

You need to wrap your getline in some form of loop. Right now you are getting 1 line, then manipulating input then exiting. Right now you are pulling it in from the user so you need to look for a terminator.

Comments

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.