Mar 8, 2013 at 3:38pm UTC
I'm trying to combine first name with last name, then output the full name, and then output full name in reverse. Also I will be replacing all vowels with z after I figure this out.
The space in between fname and lname in the name variable is leaving the fname behind. Why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int main()
{
string fname;
string lname;
string name;
cout << "Enter your first name: " ;
cin >> fname;
cout << "Enter you last name: " ;
cin >> lname;
name = fname + " " + lname;
stringstream ss(name);
string temp;
for (int i=0; i < name.size(); i++){
ss >> temp;
}
cout << "Name combined: " << temp << "\n" ;
cout << "Name reversed: " ;
for (string::reverse_iterator rit = temp.rbegin(); rit != temp.rend(); ++rit){
cout << *rit;
}
cout << "\n" ;
}
I tried the std::noskipws operator but then temp is just blank?
Last edited on Mar 8, 2013 at 3:40pm UTC
Mar 8, 2013 at 3:46pm UTC
Not sure why you are using a stringstream at all. You already have the full name in the name
variable.
Mar 8, 2013 at 3:47pm UTC
I have to replace every vowel in 'name' with 'Z' in a sec. Wouldn't that require something like stringstream?
Last edited on Mar 8, 2013 at 3:48pm UTC
Mar 8, 2013 at 3:50pm UTC
Last edited on Mar 8, 2013 at 3:51pm UTC
Mar 8, 2013 at 4:34pm UTC
Got it! Thanks Lynx!!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string fname;
string lname;
string name;
string vowelArr[6] = { "a" , "e" , "i" , "o" , "u" , "y" };
string vowel;
unsigned found;
cout << "Enter your first name: " ;
cin >> fname;
cout << "Enter you last name: " ;
cin >> lname;
name = fname + " " + lname;
cout << "Name combined: " << name << "\n" ;
cout << "Name reversed: " ;
for (string::reverse_iterator rit = name.rbegin(); rit != name.rend(); ++rit){
cout << *rit;
}
cout << "\nName without vowels: " ;
for (int i=0; i < name.length(); i++)
{
for (int x=0; x < 6; x++) {
vowel = vowelArr[x];
found = name.find(vowel);
if (found!=string::npos)
name.replace(found, 1, "z" );
}
}
cout << name << endl;
}
ON TO GRAUDATION. I have no experience with classes at all, so I may be at it for a few weeks....
Last edited on Mar 8, 2013 at 4:35pm UTC