#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    6
    Rep Power
    0

    Unhappy string to int conversion


    Here is code which i think convert character string to number and out -put written to int_equ.txt but it's not work

    #include <stdio.h>

    main()
    {
    char ch[5];
    FILE *fp;
    int hex[2];
    int i,int_equ;
    fp=fopen("int_equ.txt","w");
    printf("Enter number");
    scanf("%s",ch);
    for(i=0;ch[i];i++)
    {
    if(i==0)
    {

    int_equ=ch[i]-'0';
    }
    else
    {
    int_equ=int_equ*10+(ch[i]-'0');
    }

    }
    fputc(int_euu,fp);
    fclose(fp);
    }
    when i give 123 string in the file int_equ it's write {
    Please correct my logic
    Thanks
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    try

    atoi()

    or one of its related functions
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    PHP Code:
    for(i=0;ch[i];i++) 

    if(
    i==0


    int_equ=ch[i]-'0'

    else 

    int_equ=int_equ*10+(ch[i]-'0'); 



    why are you checking if (i==0)?? you dont have to do that check
    but i suggest you check if the characters are numbers your program will give you erroneous result if u enter abc.
    I am just sending you a logic here.. hope this helps
    PHP Code:
    char *str "123";
        
    int i,number0;
        for(
    i=0;str[i] != '\0'i++)
        {
            if(
    str[i] < '0' || str[i] > '9')
            {
                
    printf("Sorry not an integer");
                exit(-
    1);
            }
            
    number number 10 +(str[i]-'0');
        }
        
    printf("%d",number); 
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    TechNoFear is on to the right path here. There's no good reason to write your own code where the standard library already has this for you.

    In addition to atoi, you might want to look at strtol. In addition to converting the string to a long, it will also give you a pointer to where the numbers stop and the text begins. Useful if your parsing needs are a little more advanced.

    It sounds like you are a little new to the standard library. I found the GNU Standard library manual to be really useful for learning about all of the features of the library. Unlike the man pages, it also discusses how all of the functions interact for more complex actions.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "There's no good reason to write your own code where the standard library already has this for you."

    Really? How about if it's a programming assignment or you're doing it to practice your programming?

    abhishek,

    Among the many things wrong with your code is this line:

    int i,int_equ;

    int_equ equals some junk value after that line. Then you do this:

    int_equ=int_equ*10+(ch[i]-'0');

    so the junk value is used in your calculation.
    Last edited by 7stud; July 31st, 2003 at 10:52 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    int_equ gets initialized the first time through the loop.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    ...never mind.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Originally posted by 7stud
    Really? How about if it's a programming assignment
    For this there are office hours. Getting the complete answer here is considered academic dishonesty by many schools, and could get our inquirer into a lot of trouble. I don't particularly want to get him into trouble, since penalties for academic dishonesty are pretty harsh.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    6
    Rep Power
    0
    Have you tried typecasting? :)
  18. #10
  19. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by pratkata
    Have you tried typecasting? :)
    Can you show us what you mean?
  20. #11
  21. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    This is how I did string2num:

    Code:
    int s2n(char *str) 
    {
    	//accept a string, and return the number ("1016" will be 1016.. works for negative)
    	int n=0; 
    	int len=slen(str); 
    	int base=1; 
    	int neg=(str[0]=='-')?1:0; //1==neg,0==pOs 
    
    	for(;--len>=neg;n+=(str[len]-'0')*base,base*=10) //Compact :) 
    		if(str[len] > '9' || str[len] < '0')
    			return 0; 
    	return neg ? n * -1 : n; //if negative, times by -1 
    }
    int x = s2n("1016"); will make x=1016 (not "1016")
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    6
    Rep Power
    0
    I know I'm gonna regret posting this later, cuz I'm just starting out in C++, but I thought typecasting might work for this. Pretend you have two variable: a character, testChar, and an integer, testInt. IF we initialize testChar to 10 and try to change it to an integer, we would store it in testInt, with the following code, and it's stored as an integer instead of a character.

    #include <iostream>
    using namespace std;

    void main()
    {
    char testChar = 20;
    int testInt;
    testInt = (int)testChar;
    cout << testInt;
    }
  24. #13
  25. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    I think the OP was looking to convert a string to a number, rather than a single character to a number.

    [Minor point] By the way, in C++ it should be int main() rather than void main(), even though your compiler may accept the latter.

    Good luck with your C++.
  26. #14
  27. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    yeah, to change a "string" of numbers into their numerical value, my function would work, but replace slen() with strlen() they are the exact same function.

IMN logo majestic logo threadwatch logo seochat tools logo