Jump to content

Strings Problem

- - - - -

  • Please log in to reply
7 replies to this topic

#1
FriarNotch7

FriarNotch7

    Newbie

  • Members
  • PipPip
  • 20 posts
Ok so I'm working on an exercise given to us in class and I can't seem to figure out why this isn't working the way I'd like it to.


#include <iostream>
#include <string>
using namespace std;
int main(void) {
string firstname,middlename,lastname,full;
cout << "Input firstname\n";
getline(cin,firstname);
cout << "Input middlename\n";
getline(cin,middlename);
cout << "Input lastname\n";
getline(cin, lastname);

//full = lastname + ", " + firstname + ' ' + middlename[0] + '.'; //John Quentin Public output as Public, John Q.
full = firstname[0] + ',' + middlename[0] + "," + lastname;
cout << "full is " << full << endl;
return 0;
}


You input a first, middle, and last name and is output as "Public, John Q."

We have to change it so it outputs as "J.Q. Public"

full = firstname[0] + ',' + middlename[0] + "," + lastname;

^ I tried this, but the output is just the last name.

Does someone see something I don't? :confused:

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Why are you using getline instead of cin?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
FriarNotch7

FriarNotch7

    Newbie

  • Members
  • PipPip
  • 20 posts
We're using a specific class that has different functions for strings.
It's also the way my professor wants us to do it.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I have a feeling getline is grabbing some extra characters.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
FriarNotch7

FriarNotch7

    Newbie

  • Members
  • PipPip
  • 20 posts
What do you mean?
I tried cout << firstname[0];
and it does output "J" as it's supposed to when John is used as the first name.

Edit: I figured it out.. turns out that the first output can't be a character, for whatever reason.

Edited by FriarNotch7, 17 November 2011 - 10:51 AM.


#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Because + isn't overloaded for char + std:string
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
FriarNotch7

FriarNotch7

    Newbie

  • Members
  • PipPip
  • 20 posts
I appreciate that you were able to provide ample reasoning for it when my professor couldn't.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It's one of those oddities I suddenly remembered when you described the solution.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users