Standard C, atoi

When comparing string items, lets say: '0' and '-'

atoi(0) = 0
atoi(-) = 0

How can I make an exception, so I could use '0' but not '-'. It seem like atoi doesn't make difference between error and number 0.
Last edited on
atoi() and similar functions shouldn't be used if you are not sure if string contains correct value. Use streams and stream operations.
Is there any alternatives that will return NULL or something.
int test = atoi(besede[4]); besede[4] is a '-' character

NULL constant is actually 0, like #define NULL 0 . There isn't any standard alternatives in native C++, aside from using proper C++ functions insteod of legacy C-like ones.
You might try strtol
http://www.cplusplus.com/reference/cstdlib/strtol/
Although this too returns zero for invalid input, you can check the value of the endptr parameter afterwards, to deduce how many characters were consumed in the conversion, and compare it with the length of the original string, using strlen().

Or better, use c++ stream operations, e.g. stringstream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <sstream>

    using namespace std;
    
bool strToInteger(string, int &);    

int main()
{
    string list[5] = { "0", "-", "0a", "-1", "    12   " };    
    int number;
    
    for (int i=0; i<5; i++)
    {
        if  (strToInteger(list[i], number))
            cout << "number is:     " << number << endl;
        else
            cout << "invalid input: " << list[i] << endl;
    }

    return 0;
} 

bool strToInteger(string str, int & number)
{
    istringstream ss(str);
    string test;
    
    // get the integer
    if (!(ss>>number))
        return false; 
        
    // get any remaining string    
    if (ss >> test)
        return false;
        
    return true;
}


Output:

number is:     0
invalid input: -
invalid input: 0a
number is:     -1
number is:     12
Last edited on
If you are using a C11 compliant compiler you may also be able to use the stox() series of functions. These functions throw an exception you can catch if they have an error in processing the string.

See this link: http://en.cppreference.com/w/cpp/string/basic_string/stol
How about int to char. You can compare char, so if i turn my int to char I can make an if statement. So my question is. Is there a int to char function of some sort in standard c?
1
2
char c = 'a';
int x = c;

In plain old C there is no more differences berween int and char than between int and short

char is an integral type, value of which correspond to a character it represents.
http://www.asciitable.com/

If you want to know if you can transform char '1' to int 1:
1
2
char c = '3'; //or anything else
int x = c - '0'; //x will equals 3 here 


If you want to test if your character is digit:
1
2
#include <cctype> //or <ctype.h> for C equivalent
isdigit(c);
Last edited on
It depends what you mean by "int to char". If you mean convert an int to a single character, then there are two ways to look at this.

This is one, a direct conversion of an int in the range -128 to +127. (or 0 to 255 for unsigned values).
1
2
int i = 123;
char ch = i;


The other, is to convert the integer into an ASCII character. If the int is in the range 0 to 9, then this will work:
1
2
int i = 7;
char ch = '0' + i;

Well this isn't working?

I have:

1
2
3
4
5
6
7
8
9
char besede[3250][9]; //besede[4] is a char '-'
int znak = atoi(besede[4]); //transform char to int
    char ch = znak; //make that int a char again
    if(besede[4] == ch){ //check if a new char is the same as char before
        printf("Yes");
    }
    else{
        printf("No");
    }


I get: forbids comparison between pointer and integer error.

This doesn't seem to work either. It returns No every time even if besede[i] == 0.

1
2
3
4
5
6
7
8
9
int znak = atoi(besede[4]);
    char ch[1];
    sprintf(ch,"%s",znak);
    if(besede[4] == ch){
        printf("Yes");
    }
    else{
        printf("No");
    }
Last edited on
besede is an array of 3250 arrays of 9 chars.
besede[4] is an array of 9 chars.
cesede[4][5] is a char.

so in besede[4] == ch you are comparing char[9] (equivalent to char*) and char, which isn't making any sense

Second example:
arrays is basically a pointers and both besede and c are arrays, so in besede[4] == ch you are comparing pointers.
Do you know, why
1
2
3
int* x = new int(10);
int* y = new int(10);
std::cout << x == y;

will return false? Same here.
Last edited on
I changed it to:

1
2
3
4
5
6
7
8
int znak = atoi(besede[4]); //transform char to int
    char ch = znak; //make that int a char again
    if(besede[4][0] == ch){ //check if a new char is the same as char before
        printf("Yes");
    }
    else{
        printf("No");
    }


But it always returns No even if besede[i][0] == 0;
for example if besede[4][0] == '8';

1
2
3
int znak = atoi(besede[4]);//znak equals to 8 (if we lucky and there is \0 in besede[4][1])
char ch = znak;//ch equals to 8 or BACKSPACE symbol from ascii table
if(besede[4][0] == ch)//No '0' != BACKSPACE 

http://www.asciitable.com/
Hmm. What about this. If I transform character to string is this ok?

1
2
3
4
5
6
7
8
9
int znak = atoi(besede[6]);
    char ch[1];
    sprintf(ch,"%s",znak);
    if(besede[6][0] == ch[0]){
        printf("Yes");
    }
    else{
        printf("No");
    } 
Why don't you use isdigit()? Or just do something like:
1
2
//char c
if(c >= '0' && c <= '9')
Oh I didn't know you can check chars like this. Let me try this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdio>
#include <cstring>
#include <cstdlib>

    using namespace std;

int main()
{

    char str[9] = "76543";

    // Convert char string to int
    int num = atoi(str);
    printf("num = %d\n", num);

    // Convert int to char string
    char buf[30];
    sprintf(buf, "%d", num);
    printf("buf = %s\n", buf);

    // Compare original string with new string
    if (!strcmp(str, buf))
        printf("strings are equal\n");
    else
        printf("strings are NOT equal\n");

    return 0;
}
What about if a number is negative like -1 to 1. I am working with -1, 1, 0, -
couse this works:
1
2
3
4
5
6
if(besede[6][0] >= '0' && besede[6][0] <= '1'){
        printf("Yes ");
    }
    else{
        printf("No ");
    }


and this doesn't
1
2
3
4
5
6
if(besede[6][0] >= '-1' && besede[6][0] <= '1'){
        printf("Yes ");
    }
    else{
        printf("No ");
    }
Last edited on
Oh I am an idiot. I could just do if(besede[i][0] == '-') and if its not then its a number.
Thank you for your help. :)
Topic archived. No new replies allowed.