-3

I wrote a program to fetch all the phone numbers from a file which has other text like at commands and other error from other child process. Here when I try to convert the string to integer using a user-defined function I facing the problem. The converted value stored in the function is not properly returned to main program, instead its returning some unusual and it seems to be the same for every execution. Its surprising me. Can someone advice me.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char lic[128];
unsigned long long sum=0ULL;

unsigned long long stringtoint(char str[])
{
    int i=0;
    sum=0;
    if(str[strlen(str)]!='\0')
        return -1;
        //puts("Im in function");
    while(str[i]!='\0'){
        //printf("- %c -\n",str[i]);
         if(str[i] >= 48 && str[i] <= 57){
             sum = sum*10 + (str[i] - 48);
             //printf("%c and %llu\n",str[i],sum);
         }
         i++;
    }
    if(sum>0)
    printf("\nIn function passed string is %s and integer value is %llu\n",str,sum);
    return sum;
}


FILE *file;     
int main(){
        //long long int inte;
        int64_t inte;        file = fopen("receive","r"); 
        if(file!=NULL)
                while(fscanf(file,"%s",lic)!=EOF){
                        inte = 0;
                        inte=stringtoint(lic);
                        if(inte !=0){
                                printf("In Main %llu is the value of the string %s",inte,lic);
                        if(inte==sum)
                                printf("\n%llu and %llu are same\n",inte,sum);
                }
        }
        printf("\n");
        fclose(file);
        return 0;
}

The result I was getting for this program was given below.

In function passed string is 8939095683 and integer value is 8939095683 In Main 349161091 is the value of the string 8939095683

shameerariff@shameerariff-Satellite-L450:~/Workinffolder/ivr/IVRReporting$ ./datadecoder

In function passed string is 8939095683 and integer value is 8939095683 In Main 349161091 is the value of the string 8939095683

shameerariff@shameerariff-Satellite-L450:~/Workinffolder/ivr/IVRReporting$ ./datadecoder

In function passed string is 8939095683 and integer value is 8939095683 In Main 349161091 is the value of the string 8939095683

Your valuable advice are needed, Thank you in advance for your support.

Shameerariff
  • 165
  • 1
  • 11
  • Welcome to StackOverflow. Please provide a minimal testcase, that is a program where you delete everything which does not create your problem. This will help the people looking at your problem and save their time. – Micha Wiedenmann Feb 24 '15 at 14:41
  • 1
    You sum a `long long` but return an `int`, why don't you return a `long long`? – David Ranieri Feb 24 '15 at 14:42
  • Thanks Alter Mann Its working now Its a silly mistake of mine and I did not notice that. – Shameerariff Feb 24 '15 at 14:49
  • @Shameerariff: Thinking about this: the silly mistake here is that you try to parse telephone numbers as *integers* at all. For example, if you're calling a German number in Hamburg, the telephone number would be `0049 40 6776776`; the result of the integer conversion would be `49406776776` which looks exactly like a local number. Don't do that! Just stick with the char string as it is; it doesn't even use that much more memory (even if applied with a fixed length). – Marcus Müller Feb 24 '15 at 14:52
  • @MarcusMüller: Are you assuming the international access prefix is `00` ? If you want to highlight the dangers of unfounded assumptions, you should be careful. Did you perhaps mean `+49 40 6776776` ? But yeah, telephone numbers are best handled as strings. Don't even assume they're all numeric as `+` shows. – MSalters Feb 24 '15 at 15:01
  • @MSalters: totally agree: not really valid, but it's actually what most Europeans would dial (therefore note down in their books). Also, IMHO, phone numbers that contain more characters can be massively useful.Typically,a well-equipped German phonebook might contain `+49 (0) 40 / 677 67 -76` to denote that the country prefix is `49` (so skip that if you're in Germany), the area code is `040` (of which you must skip the first 0 if using the country code), the subscriber has the number block `677 67*`, and the guy you're trying to call has the internal number `76`. – Marcus Müller Feb 24 '15 at 15:06
  • Also, even the `+49 (0) 40 / 677 67 -76` beast of a phone number would need only 24B of storage (including trailing `0x00`); for everything but the largest phone directories, the overhead of storing that in memory would be negligible. – Marcus Müller Feb 24 '15 at 15:10
  • @MarcusMüller: Luckily that trend of (0) is going away as people move to mobile phones. It's quite annoying if your contact list is unreachable from abroad, whereas using +49 40 inside Germany is harmless. `( )` in a phone number is pretty bad for automated systems. – MSalters Feb 24 '15 at 15:32
  • @MSalters: I can feel your pain. – Marcus Müller Feb 24 '15 at 15:35
  • @MarcusMüller: Yeah, if you worked on these things for real then you do start to develop opinions on these things ;) Area codes aren't `040` but `40` dammit. – MSalters Feb 24 '15 at 15:40
  • @MSalters: I'm of the opinion that Hamburg has the area code `040`, and I claim being right about that, having grown up there :P – Marcus Müller Feb 24 '15 at 15:41
  • certainly you are correct, here the I have to process the phone number which was stored in the mysql database was in the integer format and so I was forced to convert the numbers to integer. In the original databse they removed the whitespaces and + sign for the phone numbers. – Shameerariff Feb 24 '15 at 16:00

2 Answers2

1

Point 1. You need to change

int stringtoint(char str[])

to

unsigned long long stringtoint(char str[])

Point 2. %lld is not the correct format specifier for unsigned long long. Use %llu

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Can someone advice me.

Yes. Don't write your own function, but use the atoi function available in the C standard library if you really want to operate on char arrays, or use the stoi function to work on std::string, or use any C++ iostream to read ints from your strings. With the latter, you can basically just use the file stream you can directly get when opening a ifstream with C++'s standard library.

EDIT: I should mention you shouldn't use atoi/stoi, but atoll/stroul to actually reflect the fact that your numbers could be bigger than whatint can hold.

Also, phone numbers are not integers. In many countries, city area codes start with 0, which you can't represent in any numeric type. In fact, telephone numbers are not numbers, but sequences of digits, if you ask me.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • Do not use atoi, it will not provide errors, please read http://www.parashift.com/c++-faq/convert-string-to-num.html – Micha Wiedenmann Feb 24 '15 at 14:42
  • @MichaWiedenmann: I agree, and I think we might have come to the same conclusion at the same moment, so I added the comment. Do you like the edit? – Marcus Müller Feb 24 '15 at 14:46
  • I do not particularly like your edit, but I will revoke my down vote. I personally think that one should follow the answer of the linked FAQ but you are free to disregard that advice. Thanks for putting in the effort though. – Micha Wiedenmann Feb 24 '15 at 15:23
  • Actually question is poor, and Marcus suggestion is correct --Phone numbers are not integer no one like to do arthmatic on phone number +1 for that – Grijesh Chauhan Feb 24 '15 at 15:25
  • @MichaWiedenmann thanks for your feedback :) actually, your criticism inspired me to take a step back, and recommend the not-doing-the-conversion-at-all approach. – Marcus Müller Feb 24 '15 at 15:34
  • I accept it was a silly mistake of mine and I do accept it. But when it comes to a large program I did not notice that function declaration type. But Its a good experience for me. – Shameerariff Feb 24 '15 at 16:02