I am clueless about strings

Hello,

I am longtime lurker and first time poster and I am in need of help.

I am working on a very simple program and am already stuck at the beginning. At this point I just want some help so I can continue on with my work.

What I am trying to do is input a string [a last name and a first name] and then have it output.

Thank you for any help you can give me.
closed account (4Gb4jE8b)
Well, what have you done so far? And have you read http://www.cplusplus.com/reference/string/string/ and any of the links that may apply to what you are doing?
I have gone through the reference articles about strings, but I haven't been able to find anything of help to me.

So far I have created a string and have possibly put in some words:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
#include <string.h>

using namespace std;

int main()
{
	const int SIZE = 20;
	string names[SIZE];
	
	cout << "Please type your name like this: Last Name (comma) First Name:  ";
	cin >> names[SIZE];

	cout << names[SIZE];
	
	return 0;
}


This code doesn't do what I want it to do, and thus far I am clueless as to how to get it to output what I just input.

EDIT: Updated code so it outputs part of the input.
Last edited on
That is not how you write/read in an array.
You need to loop from 0 to n-1, working per element.
closed account (4Gb4jE8b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> //didn't need iomanip
#include <string.h>

using namespace std;

int main()
{
	string names;
	
	cout << "Please type your name like this: Last Name (comma) First Name:  ";
	cin >> names;

 	cout << names;
	cin.get();
	cin.get();
	return 0;
}


strings are dynamic, so you only need to clarify how large they are if you specifically want them to be static.

I didn't compile it, but what exactly did your code do wrong? The only obvious thing i could see was a possible runtime error if names[i] had empty characters at the end of it.
What went wrong is the same for both my code and that which you posted: The output stops after the comma.
closed account (4Gb4jE8b)
oh, of course. Cin doesn't take spaces. use getline for that. My apologies about not posting that in the first place.

getline(cin, names);

http://www.cplusplus.com/reference/string/getline/
Ah, yes! Thank you, this has solved my problem.
Topic archived. No new replies allowed.