-4

I'm trying to parse a number stored in a char[] variable so i can run an IF statement against it. Ive tried using Atoi but this doesnt work, maybe im implementing it wrong.

i also what to do this in C++ aswell here are 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);
Adrian Panasiuk
  • 7,249
  • 5
  • 33
  • 54
user1949280
  • 75
  • 2
  • 8

2 Answers2

0

Since you want the user to enter an integer, you probably want to use an int variable to hold what they 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, that this is still a little simplistic -- if the user enters something non-numeric (e.g., "abc") this won't deal with it well -- it'll get stuck in an infinite loop.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    @user1949280: Given that you want a numeric result, why would it have to be a `Char` (whatever that is -- though presumably at least vaguely similar to a `char`). – Jerry Coffin May 09 '13 at 13:26
  • it need to be a string containing only number – user1949280 May 09 '13 at 13:29
  • Why do you need a string, if you then want to process it as a number? Would I be uncharitable in assuming that homework of some kind is involved? – microtherion May 09 '13 at 13:35
  • @user1949280 I had actually assumed that you wanted string input based on how you asked the question but the code you have given does not really explain why. Is that the real code or is your use case different or is it homework? – Shafik Yaghmour May 09 '13 at 13:36
0

Works just fine and is easier to read (C++):

int data;
std::cout << "Enter Port Number between 1-1024" << std::endl;

while (std::cin >> data && (data < 1 || data > 1024))
    std::cout << "Please re-enter the port number!";

std::cout << "port number: " << data << std::endl;

But if you really need the input to be a character string, use std::string and std::stringstream:

std::string data;
int portNr;
do
{
    std::cout << "Enter Port Number between 1-1024" << std::endl;
    std::cin >> data;

    std::stringstream sstr (data);
    if ((sstr >> portNr).fail())
        continue;
}
while (portNr < 1 || portNr > 1024);

Tipps:

  • Using raw arrays is a bad idea as buffer overflow might occur (in your case too!) Use std::vector (with #include <vector>) or std::array instead.
  • Avoid empty statements ";;" (they are just pointless).
  • Avoid using namespace std; (especially in the global scope and header files) (see this post)
  • Variables beginning with capitals are used for class and struct names.
Community
  • 1
  • 1
Sceptical Jule
  • 889
  • 1
  • 8
  • 26