4 Replies - 150 Views - Last Post: 06 February 2012 - 05:38 PM Rate Topic: -----

Topic Sponsor:

#1 Sam109  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-February 12

c++ problem with string.at()

Posted 06 February 2012 - 05:11 PM

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main ()
{
    int firstMiddleLast;
    string name = "Jim Anderson Smith ";
    int index;
    char ch;
    
    cout << "enter a name in the format First Middle Last: ";
    cin >> firstMiddleLast;
    index = name. find(' ');
    cout << "First Name: " << name.substr(0, index) << endl;
    name = name.substr(index+1, name.length()-1);
    index = name.find(' ');
    cout << "Middle Name: " << name.substr(0, index) << endl;
    name = name.substr(index+1, name.length()-1);
    cout << "Last Name: " << name << endl;
    ch = toupper(firstMiddleLast.at(0));
    firstMiddleLast = ch + firstMiddleLast.substr(1, name.length());
    cout << "Initial:" <<
         << firstMiddleLast << endl;
    system("pause");
}




This is my code and I've been struggling with this for a long time now. Can someone tell me what I am doing wrong, I'm working with C++ and I'm suppose to get the Initials to appear but i can't seem to be able to get it. I'm still unsure as to how to use string.at, which separates the characters i believe.

:code:

This post has been edited by jimblumberg: 06 February 2012 - 05:19 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: c++ problem with string.at()

#2 tlhIn`toq  Icon User is online

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: c++ problem with string.at()

Posted 06 February 2012 - 05:16 PM

First, please use code tags like it says everywhere including under the textbox you filled out to post your question.
:code:

Second, please edit your title. Can you imagine if all 500,000 users posted "I need help" ?? Can you please change it to something meaningful?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: c++ problem with string.at()

Posted 06 February 2012 - 05:26 PM

The std::string.at() provides access to individual characters in your string. Example:
#include <string>
#include <iostream>

using namespace std;

int main()
{
   std::string yourString = "This is your string";
   for(int i = 0; i < yourString.length(); ++i)
      cout << yourString.at(i) << endl;
   return 0;
}


Should printout each character on it's own line. Also note you could use array notation[] instead of at() to access the individual characters;

Jim
Was This Post Helpful? 1
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 438
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: c++ problem with string.at()

Posted 06 February 2012 - 05:29 PM

I think your main problem is that cin > name will stop reading after the first space, so there will never be a second word. Try: std::getline(cin, name);

This post has been edited by Karel-Lodewijk: 06 February 2012 - 05:30 PM

Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: c++ problem with string.at()

Posted 06 February 2012 - 05:38 PM

Personally, I would do string.split on the space character.
That will yeild you an array of n words
The first character of each word is therfore the initials.
This then automatically adjusts to however they enter their name:
John Smith
John Q. Public
John Public Citizen

This post has been edited by tlhIn`toq: 06 February 2012 - 05:39 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1