I need to write a program in C++ that inputs in on format and outputs in another and ignore a comma

I have to write a program that inputs a name from a user; last, first, middle and output it first middle last. Do I have to use cout<<"Enter your name last, first middle name"; is that how I get the input to that format?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;
char chr;
int main()
{
	char first, middle, last;
	cout<< "Enter your last name, first name and middle name"<<endl;
	cin.get(last);
	cin.get(middle);
	cin.get(first);
	cout<<first<<middle<<last;
	cin.ignore(',');
	cin>>chr;

	return 0;
}

  

I know that this doesn't work correctly and I am probably missing a lot but I am new and I just need some guidance. Thank you
Last edited on
I believe you should use string instead of char.

Edit: also, cin >>.
Last edited on
Ok so this is what my program looks like with those changes

#include <iostream>
#include <string>
using namespace std;
char chr;
int main()
{
string first, middle, last;
cout<< "Enter your last name, first name and middle name"<<endl;
cin>>(last);
cin>>(middle);
cin>>(first);
cout<<first<<" " <<middle <<" "<<last;
cin.ignore(',');
cin>>chr;

return 0;
}

when it runs the output is middle first last ,
Where do I need to put the cin.ignore(',') so that the , ? and why it the order wrong?
Simple, your cin order is wrong. ;)

Also, no need for ( ) around those variable names. And sorry but I did not understand your question about , . What are you trying to do with it?
Last edited on
I need the input format to be last, first middle and the output to be first middle last no comma my output has the comma at the end.

what should I change my cin to I tried moving them and it didn't make a difference.

Thank you

I changed the cin to
cin>>first;
cin>>middle;
cin>>last;
and the output was the exact same as the input was
Last edited on
Your cout asks for last, first then middle name. Your cin order should be like that as well.

cin >> last;
cin >> first;
cin >> middle;

Then you can print it in any order you want.
Do I have to use a char to be able to use cin.ignore function?

Thanks for all the help!
Nope, you can just cin.ignore(); .
ok is the cin.ignore in the right place because its not working the comma is still showing up.
I have tried moving the cin.ignore to between the cinlast and first also before the cin>>last and my output doesn't display. Also changed to cin.ignore(100,',');
Topic archived. No new replies allowed.