Simple C++ code (what's wrong here?)

Posted by JW on Stack Overflow See other posts from Stack Overflow or by JW
Published on 2009-08-29T16:53:08Z Indexed on 2012/05/31 16:40 UTC
Read the original article Hit count: 183

Filed under:

Noob to C++.

I'm trying to get user input (Last Name, First Name Middle Name), change part of it (Middle Name to Middle Initial) and then rearrange it (First Middle Initial Last).

Where am I messing up in my code?

--Thanks for ANY help you can offer!

...

#include <iostream>
using std::cout;
using std::cin;

#include <string>
using std::string;

int main()
{

  string myString, last, first, middle;

  cout << "Enter your name: Last, First Middle";

  cin >> last >> first >> middle;

  char comma, space1, space2;

  comma = myString.find_first_of(',');
  space1 = myString.find_first_of(' ');
  space2 = myString.find_last_of(' ');

  last = myString.substr (0, comma); // user input last name
  first = myString.substr (space1+1, -1); // user input first name
  middle = myString.substr (space2+1, -1); // user input middle name

  middle.insert (0, space2+1); // inserts middle initial in front of middle name
  middle.erase (1, -1); // deletes full middle name, leaving only middle initial

  myString = first + ' ' + middle + ' ' + last; //  

  return 0;
}

© Stack Overflow or respective owner

Related posts about c++