Good Afternoon,
I'm new to C++ programming and I have to do a program that deals with strings. Below is the code that I have so far, I have problems in this section of the program
Get three lines from the keyboards. Play with string copying, 
    concatenation, and input size controling.
    ******************************************************************/

    //Gets three lines from the user
    cin.get(ch);
    cout <<"Please enter a line"<<endl;
    getline(cin, line4, '\n');
    cout<<"The first line entered is:" <<line4<<endl;

    cout <<"Please enter a second line"<<endl;
    getline(cin, line5, '\n');
    cout <<"The second line entered is:" <<line5<<endl;

    cout <<"Please enter a third line"<<endl;
    getline(cin, line6, '\n');
    cout <<"The third line entered is:" <<line6<<endl;

    //String copying
    strcpy(str2, str1);
    cout <<"str1 is " <<str1 <<endl;
    cout <<"str2 is " <<str2 <<endl;

    //String concatenation
    strcat(str3, str1);                 
    getline(cin,line2);                 
    cout <<"str1 is " <<str1 <<endl;
    cout <<"str2 is " <<str2 <<endl;
    cout <<"str3 is " <<str3 <<endl; 

    //size controlling
    cout<<"Please enter FootballSeason."; 
    cin.width(5);                       
    cin>>str4; 
    cin>>setw(5)>>str4;                 

    cout<<"str3 is " <<str3 <<endl
        <<"str4 is " <<str4<<endl;




        //well done and exit
    `#include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <cmath> 
    #include <cstdlib>
    #include <iomanip>

    using namespace std; 

    int main( )
    {
        //declare string variables
        string line1, line2, line3, line4, line5, line6;            //three string objects
        char str1[50], str2[50], str3[50],  //three arrays of 100 charaters
             str4[5], str5[5],ch;           //two arrays of 5 characters

        //get string inputs
        cout<<"Enter one string without spaces => "; 
        cin>> line1;                        //cin cannot get spaces
            cin.get(ch);                    //this clears out the buffer i.e., takes out the "enter" character out of buffer//changed by sirisha.           
        cout<<"Enter a line with spaces => "; 
        getline(cin, line2, '\n');          //getline can get spaces

        //string concatenation
        line3 = line1 + line2;              //+ symbol is to concatenate two strings
        cout << "line1 is " <<line1 <<endl
            <<"line2 is " << line2 <<endl
            <<"line3 is " << line3 <<endl; 

        line1 += line3;                     // += symbol is to concatenate two strings and assigns the value
        cout<<"Now, line1 becomes " <<line1 <<endl; 

        //get strings
        cout<<"Enter one word => "; 
        cin>>str1;                          //cin cannot get spaces
        cout<<"Enter 'I like C++ very much.' =>"; 
        cin.getline(str2, 50, 'v');         //getline can get spaces

        //string copy
        strcpy(str3, str1);                 //copy str1 to str3

        //string concatenation
        strcat(str3, str2);                 //concatenate str2 to str3

        getline(cin,line1);                 //takes out “ery much.”out of bbuffer ad clears it. //changed by sirisha
        cout<<"str1 is " <<str1 <<endl
            <<"str2 is " <<str2<<endl
            <<"str3 is " <<str3<<endl; 

        //control the input string size
        cout<<"Enter 'CatandDogarefriends.' =>"; 
        cin.width(5);                       //set input upto 4characters
        cin>>str4; 
        cin>>setw(5)>>str4;                 //set input upto 4characters

        cout<<"str3 is " <<str3 <<endl
            <<"str4 is " <<str4<<endl;


        /******************************************************************

        Get three lines from the keyboards. Play with string copying, 
        concatenation, and input size controling.
        ******************************************************************/

        //Gets three lines from the user
        cin.get(ch);
        cout <<"Please enter a line"<<endl;
        getline(cin, line4, '\n');
        cout<<"The first line entered is:" <<line4<<endl;

        cout <<"Please enter a second line"<<endl;
        getline(cin, line5, '\n');
        cout <<"The second line entered is:" <<line5<<endl;

        cout <<"Please enter a third line"<<endl;
        getline(cin, line6, '\n');
        cout <<"The third line entered is:" <<line6<<endl;

        //String copying
        strcpy(str2, str1);
        cout <<"str1 is " <<str1 <<endl;
        cout <<"str2 is " <<str2 <<endl;

        //String concatenation
        strcat(str3, str1);                 
        getline(cin,line2);                 
        cout <<"str1 is " <<str1 <<endl;
        cout <<"str2 is " <<str2 <<endl;
        cout <<"str3 is " <<str3 <<endl; 

        //size controlling
        cout<<"Please enter FootballSeason."; 
        cin.width(5);                       
        cin>>str4; 
        cin>>setw(5)>>str4;                 

        cout<<"str3 is " <<str3 <<endl
            <<"str4 is " <<str4<<endl;





        //well done and exit
     return 0; 
    }

Here is the whole program

Inline Code Example Here

Comments
Yet another code post with no explanation. Get with it!!

I have problems in this section of the program

Are your problems a secret? Or would you care to enlighten us? We aren't psychic...

I've noticed most of your initial posts are a comlete waste of your and our time since all you do is post code and ask for help, but you never explain what your problem is. Help with what? It would be beneficial to learn how to ask a question rather than making us play 20 Questions with you until we pull the question out of you.

Edited 4 Years Ago by WaltP

The problem is that line 114 it doesn't let the user input anything and on line 120 it does output the sentence the user input on line 118, but instead output "darefriends" from line 97

You are missing endl or flush at the end of most of your output statements.

This is a joke, right? It has to be a joke, because neither is it true nor does it have anything to do with the OP's problem.

The OP's problem arises from mixing istream::operator >> with getline calls, and it's another very common mistake among beginners. The solution is to either use only one or the other, or make sure you clear the buffer from any leftovers of the one before using the other.

Here's an example that demonstrates the problem and an easy fix:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name;
    string color;

    cout << "Enter your name (one word): ";
    cin >> name;

    cout << "Enter your favourite color (e.g. light blue): ";
    //{ string temp; getline(cin, temp); } // uncomment to fix
    getline(cin, color);

    cout << "Hello, " << name << "!" << endl;
    cout << "Your favourite color is " << color << endl;

    cin.get();
}

Edited 4 Years Ago by m4ster_r0shi

This is a joke, right? It has to be a joke, because neither is it true nor does it have anything to do with the OP's problem.

Based on his actual problem statement, it looks like buffered input is his problem. Maybe his description was still ambiguous and confusing since it wasn't a linear description of the problem -- or was it?.

Edited 4 Years Ago by WaltP

Based on his actual problem statement, it looks like buffered input is his problem. Maybe his description was still ambiguous and confusing since it wasn't a linear description of the problem -- or was it?.

Did anybody else read this several times trying to make sense out of it, or should I feel bad about myself?

Comments
Nah, that was the problem.
Probably not, and no you shouldn't. If you wish to do that much work, go for it. I want a clear explanation.
This question has already been answered. Start a new discussion instead.