Click to See Complete Forum and Search --> : char array input with whitespace in C++?


quip
02-08-2004, 10:28 PM
I am trying to learn some C++. I am inputting some data, using a character array. Is there any way to input data into a character array with whitespace, i.e. for a name put in "John Doe"? Currently, my program takes the input and puts John in the first variable and Doe in the second, before the prompt even comes up. Here's the snippet of the code:

int print_to_screen()
{
while ((counter <= user_number))
{

std::cout << entry[counter].name << "\n";
std::cout << entry[counter].address << "\n";
std::cout << entry[counter].city << "\n";
std::cout << entry[counter].state << "\n";
std::cout << entry[counter].zip << "\n";
std::cout << "\n";

++counter;

}
counter = 0; //reset the counter
return(0);
}

dboyer
02-08-2004, 10:54 PM
cin.getline() might be just what you need.

quip
02-08-2004, 11:03 PM
Thanks for the reply, and that is probably what I will end up doing. But I was wondering (should have mentioned this) if I could do it without the use of a string (C or C++ style). Any other ideas?

tecknophreak
02-08-2004, 11:28 PM
Without string? Strings great for dealing with C++ chars and whatnot. Well, how about cin.get() (http://www.cplusplus.com/ref/iostream/istream/get.html)?

dboyer
02-08-2004, 11:29 PM
im confused..

Is there any way to input data into a character array with whitespace, i.e. for a name put in "John Doe"?

a character array IS a string.

quip
02-08-2004, 11:41 PM
The reason I ask if it is possible to do it without a string is because the book I am using gives a sample of a structure that is very similar to the one I am using, and doesn't mention anything about a string. Of course, the snippet of code I am talking about also doesn't include anything concerning the reading of the input, so maybe the author is assuming that I will be using a string (at least C-style)
I was just curious if it was possible; if there was something I was missing concerning whitespace, but it seems that I am not, so I think I will just use the getline function.