c++ - How do you initialize struct size/length -


i have create struct , run in loop not problem has run specific number of times user chooses how do ?

ex:

struct employee {  name  department  salary }; 

if want run ten times can use

for (int x=0, x<10, x++) .... 

but if have ask user number , not keep @ 10 ?


also idea on how error check name using getline wrong input (numbers).

thanks ton guys. new c++ , trying learn if can give me examples explanations appreciated.

thank time.

thanks input. see how name checking can done apologize unclear question.

when declare struct in main asks me how many info intend put in struct. ex: employee e[10];

i dont know how ask user before , make his/her response const int . (10 in case const int).

let's assume variable n keep entered value of user.

int n; 

then loop like

for ( int x = 0; x < n; x++ ) {     // code } 

the value in n can entered means of operator >>.

std::cin >> n; 

to check name contains letters can apply algorithm std::all_of , standard c function isalpha

for example

#include <algorithm> #include <cctype>  //...  std::string name;  std::getline( std::cin, name );  if ( !name.empty() && std::all_of( name.begin(), name.end(),                                     []( char c ) { return std::isalpha( c ); } ) ) {     std::cout << name << " valid name" << std::endl; }! 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -