You are here:

C++/String Manipulation

Advertisement


Question
Hi I am having trouble with a string manipulation problem. I am a beginner with C++ and any help would be appreciated. The problem is i cannot cin properly the character frontslash, also is there a way to cout a specific data piece in a string?

Thanks for your help


Here is my code so far:

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  
  //declare Variables
  string givenname;
  string title;
  string familyname;
  char "";
  
  cout <<"================================================" << endl;
  cout <<"Welcome to Steve's String Manipulator" << endl;
  cout <<"================================================" << endl;
  cout << "Please enter title, given and family name to reformat. (Acceptable format title/given name/family name): ";
  cin >> title >> "" >> givenname >> "" >> familyname;
   cout << "*****reformatted name: ";
  cout << title << familyname << "(" << givenname << ")";
  return 0;  

Answer
Well for a start this code does not compile with VC++ 2005 for 3 reasons:

The line:

   char "";

I do not see what this is meant to be. I presume a char variable, but it seems not to be needed. First off there is no variable name, and secondly double quotes indicates a literal string not a literal character, literal characters are defined using single quotes:

   char a_character_object('#');

Secondly the line:

  cin >> title >> "" >> givenname >> "" >> familyname;

What are the empty string literals doing in this chained sequence of inputs? You cannot input into a literal. Removing these gives:

   cin >> title >> givenname >> familyname;

Finally, you left off the closing brace to main in your posting!

After fixing these problems your code will run as (I) expected. There is no problem entering front slash characters. They can form part or all of any single string value entered using the istream extraction operator (operator>>). However, and this you should understand now, values entered in this way are terminated by white space (that is space, tab or newline), not some random character of your choosing, which presumably you informed your program about by telepathy <g>. So rather than entering:

Mr/John/Smith

Your program will accept:

Mr John Smith

It prints:

MrSmith(John)

Thus:

"
================================================
Welcome to Steve's String Manipulator
================================================
Please enter title, given and family name to reformat. (Acceptable format title/given name/family name): Mr John Smith
*****reformatted name: MrSmith(John)
"

Entering the name as in the requested format will allow the name to be entered as a single string. This is what was happening. If you enter:

Mr/John/Smith

This will be read for the title string, you still have to enter values for your givenname and familyname strings.

I suspect that if this is an assignment then entering the title, given and family names in a format that is easily entered for a single string is probably the point of the exercise. Your program and the subject of this question talk about string manipulation, but all you are doing is reading separate string values and re-orfering how they are displayed, together with additional formatting. You are not manipulating the strings themselves.

So try this as a starting point:

#include <iostream>
#include <string>

using namespace std;

int main ()
{
   cout <<"================================================\n";
   cout <<"Welcome to Steve's String Manipulator\n";
   cout <<"================================================\n";
   cout << "Please enter title, given and family name to "
         "reformat.\n"
         "(Acceptable format title/given name/family name): ";

   string name;
   cin >> name;

   // >>>>> Add code to manipulate raw name string into
   // >>>>> a string containing name in required format
   
   cout << "*****reformatted name: " << name << '\n';
   return 0;
}

Notice the following:

- This code will get you up and running. I have left it so you have to add the code after cin >> name where the comments are to modify the name string to be in the required format. I suggest you refer to your reference material on the std::string class.

- You do not need to define variables at the start of a function and it is generally considered good practice to define them as close to the point of use as possible.

- C and C++ allow you to split literal strings across lines. I have shown this for the long user prompt - it is one literal string passed in a single operator<< call to std::cout. I also added an explicit line break at a reasonable point in this string as it was much too long for my command console window and wrapped anyway.

- You do not need to use std::endl all the time to just get a newline. std::endl incurs an extra overhead as it also flushes the stream buffer when called. In this case I do not think there is much in it as you probably need to have the buffer flushed to the console to read it, but on my Windows XP system just outputting newline characters is sufficient. I have replaced std::endl with newline characters to demonstrate this point. You can of course replace them with std::endl as you had before if you wish or if your output is not as expected.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.