PDA

View Full Version : c++ : Arrays & cin


Serex
06-12-2004, 02:03 PM
ok i have this project that im building for my mums business. it calculates the wage earnings of her employees (tax, overtime ect ect) however i have it so that she can enter the name of the empoyee for that week and it stores it in an array.

the names are stored as strings. however when i run the program if i enter a space between the first and last names (ie "John Doe") it breaks the script. but when say "JohnDoe" was entered it works fine.

anyone know any pointers that could get me heading in the right direction?

thankyou

obiwanjabroni
06-15-2004, 09:04 PM
The problem is pretty common. You are probably using the "cin" command to take input for the name. The problem with cin is that it only reads characters it considers not to be delimiters. Unfortunately, a space, like hitting enter, is a delimiter for cin input. The way to get around this is to take the entire line and store it into your string. Look around for the "getline" function. It's slightly different on different implementations but I believe the most common implementations are:

getline([input stream],[char*]);
getline([input stream],[char[]]);
cin.getline(char*);
cin.getline(char[]);

Hope that helps!