Monday, May 24, 2010

In C++, how do I use either string or char to input first & last name using only one variable?

The console should ask for an author name, then the user enters both first and last name together (eg. John Doe), then the console should display both first and last name, but only one variable must be used. I've tried declaring char[20] or string but neither will allow for whitespace between the names. Any help is appreciated, thanks.

In C++, how do I use either string or char to input first %26amp; last name using only one variable?
#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;iostream%26gt;








using namespace std;





int main (int argc,char* argv[]){


char name[80];





do{





int opcion;


cout%26lt;%26lt;"Selecione lo que desea hacer: "%26lt;%26lt;endl;


cout%26lt;%26lt;"Push 1 for put name %26amp; last name."%26lt;%26lt;endl;


cout%26lt;%26lt;"Push 2 for view the name %26amp; last name, you entered."%26lt;%26lt;endl;


cout%26lt;%26lt;"Push 3 for exit."%26lt;%26lt;endl;


cin%26gt;%26gt;opcion;


switch(opcion){


case 1:


cout%26lt;%26lt;"write the name and the lastname"%26lt;%26lt;endl;


/*jump exact 1 character used for the opcion*/


cin.ignore(1);


/*you can use cin.getline for take a chain of characters


con.getline take teh char array for put in came,


the number of characters , and the last the cahracter for delimited


*/


cin.getline(name,80,'\n');


break;


case 2:


cout%26lt;%26lt;"the name and the lastname youe entered are: "%26lt;%26lt;endl;


cout%26lt;%26lt;name%26lt;%26lt;endl;


break;





case 3:


exit(0);


break;


default:


cout%26lt;%26lt;" invalid"%26lt;%26lt;endl;


break;


}





}while(true);


return 0;


}





with cin.getline() you can take all the incomming data bye


No comments:

Post a Comment