Parsing a number in a string in C++ -


i'm trying parse number stored in char[] variable can run if statement against it. ive tried using atoi doesnt work, maybe im implementing wrong.

i in c++ aswell here both codes snippets

c

char data[50];   do{     printf("enter data number between 1-50:\n");     scanf("%i", &ppacket->data);      atoi(&ppacket->data);      if(ppacket->data < 1 || ppacket->data > 50){         printf("incorrect input retry.\n");     } } while(ppacket->data < 1 || ppacket->data > 50); 

c++

char data[50];  do{ cout<<"enter port number between 1-1024"<<endl;;     cin>> data;      if(data < 1 || data > 50){         cout<<"incorrect input retry"<<endl;;     } }while(data < 1 || data > 50); 

since want user enter integer, want use int variable hold enter:

int port_number;  do{     cout<<"enter port number between 1-1024"<<endl;;     cin>> port_number;      if(port_number < 1 || port_number > 50){         cout<<"incorrect input retry"<<endl;;     } } while(port_number < 1 || port_number > 50); 

note, however, still little simplistic -- if user enters non-numeric (e.g., "abc") won't deal -- it'll stuck in infinite loop.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -