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:
7 replies to this topic
#1
Posted 17 November 2011 - 09:01 AM
|
|
|
#2
Posted 17 November 2011 - 09:06 AM
Why are you using getline instead of cin?
#3
Posted 17 November 2011 - 09:07 AM
We're using a specific class that has different functions for strings.
It's also the way my professor wants us to do it.
It's also the way my professor wants us to do it.
#4
Posted 17 November 2011 - 09:08 AM
I have a feeling getline is grabbing some extra characters.
#5
Posted 17 November 2011 - 09:14 AM
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.
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
Posted 17 November 2011 - 11:49 AM
Because + isn't overloaded for char + std:string
#7
Posted 17 November 2011 - 12:04 PM
I appreciate that you were able to provide ample reasoning for it when my professor couldn't.
#8
Posted 17 November 2011 - 01:22 PM
It's one of those oddities I suddenly remembered when you described the solution.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









