I need the comma in my program to be removed in my output I messaged my instructor for assistance and this is what she messaged me back:
The problem is that when you read in last – it includes the comma. So your cin.ignore needs 2 arguments, 100, \n. In this case, you don’t need the cin.ignore. Just read in first and then middle. Then use string functions to “find” the comma and create a substring from last starting at position 0 to the place before the comma. problem is that I have no idea what she is talking about in regard to the substring part her is my program with the find and substring that are not working
#include <iostream>
#include <string>
using namespace std;
char chr;
int main()
{
string last, first, middle;
cout<< "Enter in this format your Last name comma First name Middle name."<<endl; //Input full name in required format
last.find(",");
last.substr(0);
cin>>last; //receiving the input Last name
cin>>first; //receiving the input First name
cin>>middle; //receiving the input Middle name
cout<<first<<" "<<middle<<" " <<last; //Displaying the inputed information in the format First Middle Last name
cin>>chr;
return 0;
}
I have tried putting the find and substr in different place but its not working.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string last, first, middle;
cout<< "Enter in this format your Last name comma First name Middle name."<<endl;
cin>>last; //receiving the input Last name
cin.ignore();
cin>>first; //receiving the input First name
cin.ignore();
cin>>middle; //receiving the input Middle name
cin.ignore();
last = last.substr(0, last.length() -1);
cout<<first<<" "<<middle<<" " <<last;
cin.ignore();
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string last, first, middle;
cout<< "Enter in this format your Last name comma First name Middle name."<<endl;
getline(cin, last, ',');
cin>>first;
cin.ignore();
cin>>middle;
cin.ignore();
cout<<first<<" "<<middle<<" " <<last;
cin.ignore();
return 0;
}
does my last.find look right? I am not sure what to put in the () for the last.substr because the length of the name will always be different. the way my program sits now the comma is still there and there is a 5 at he end of the middle name