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