converting char to int

This is a discussion on converting char to int within the C Programming forums, part of the General Programming Boards category; Hi, Is there any way of converting char to int or int to char? I want to put an int ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    14

    converting char to int

    Hi,
    Is there any way of converting char to int or int to char? I want to put an int into a char array.

    Thank you

  2. #2
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    >>char to int
    atoi or strtol functions (stdlib.h)

    >>int to char
    sprintf, snprintf (stdio.h)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    What sort of examples did you have in mind?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    somethine like this:
    want to put an int into a char array into this specific index.
    sprintf(s[i],"%c",num);

    but this gives me
    warning: passing arg 1 of `sprintf' makes pointer from integer without a cast

  5. #5
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Quote Originally Posted by saphiroth
    somethine like this:
    want to put an int into a char array into this specific index.
    sprintf(s[i],"%c",num);

    but this gives me
    warning: passing arg 1 of `sprintf' makes pointer from integer without a cast
    Well, in the case of
    Code:
    sprintf(s[i],"%c",num);
    sprintf expects a char array, or "string", for the first argument, and you are sending it one element of the array.

    If you want to put a int into a char array, I guess you can just force it.
    Code:
    carray[index] = someInt;
    /*or even typecast it */
    carray[index] = (char) someInt
    That probably won't have the effect you want though.
    Maybe you want the ascii value of an integer? I'm pretty sure this only works for numbers 0-9, but
    Code:
    carray[index] = '0' + someInt /* 0 <= someInt <= 9 */
    would store '7' in the array in the case that someInt == 7. If you want a two digit number, you're going to have to do a little bit more work.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    I tried to force it but I got this when righting the result to a file. The boxes should have number in them.

    a+b*c=+* ܁12565
    b*g+n=*+ ܁12565
    c*t+h=*+ ܁12565
    a+u*m=+* ܁12565
    t+w*i=+* ܁12565

  7. #7
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Quote Originally Posted by saphiroth
    I tried to force it but I got this when righting the result to a file. The boxes should have number in them.

    a+b*c=+* ܁12565
    b*g+n=*+ ܁12565
    c*t+h=*+ ܁12565
    a+u*m=+* ܁12565
    t+w*i=+* ܁12565
    Well, I said you could force the ints into the array, I also said it probably wouldn't have the effect you wanted.
    Ok, so in the first line, you have integers: a, b, and c, and you want to put them and their result in a string to print to a file?
    You do know that you can do this with fprintf().
    Can you please explain very clearly what it is you are trying to do?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    I simply want to replace the letters a,b,c,g,n...with numbers then put that results such as:
    "a+b*c=1+5*6" into a file and do the same thing with the next line. Get it in the form of b*g+n and output to file "b*g+n=6*4+8".

    Thank you

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something similar to this?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char line [ 80 ];
       fputs("Enter equation [a+b*c]: ", stdout);
       fflush(stdout);
       if ( fgets(line, sizeof line, stdin) != NULL )
       {
          double a, b, c;
          if ( sscanf(line, "%lf+%lf*%lf", &a, &b, &c) == 3 )
          {
             printf("%g+%g*%g=%g\n", a, b, c, a + b * c);
          }
       }
       return 0;
    }
    
    /* my output
    Enter equation [a+b*c]: 1+2*3
    1+2*3=7
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think they want it a bit more complex than that. This is I suspect what they want:
    Code:
    Output: "Please enter equation: "
    Input: "a+w-z*a"
    Output: "Ok, 'a+w-z*a' = '1+24-26*1'"
    Basicly, if I understand it right, they want to search through and find each letter, and turn it into the number of the place it falls in the alphabet.

    This should be easy to do for them, if they just think about it.
    Code:
    for each character of input
        if this is a letter
            convert to its "alphabetical value"
        else
            don't do anything to this character
    So there they have it. That's all you have to do. Now you simply write something that looks at a letter and gives you a value 1 through 26 for it. Surely you can think of some way to do that, right? How would you do it on paper? Walk through the process you yourself would take in your own mind to find out what a letter was.

    Now turn that into code.

    When you get stuck, post your code attempt, and we'll give you a few pointers.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    14

    char arrays the forking

    I want to thank all of you for your help. quzah you are correct about the program. Well almost. I do have to convert a letter into the number it falls in the alphabet and i did that by putting all the letter into an array and just incrementing the index by one so the letter b would be in the index 1 but its the second letter so I increment the index by one and get
    b = 2. What I have to do is: Create 2 forks. in file 1 i have the equations such as a+b*y that the parent process reads and then process them by exchanging the right number corresponding to that letter then it place this answer with its pid into file 2. The child process gets the file 2 and adds the stuff up and places its pid next to it and puts the answer into file 3. Then the grandchild process reads file 3 and prints out the answer to the screen. I wrote out most of the code but im having troubles cause Im not too familiar with C syntax. Since the 3 processes execute independently BUT file 2 has to be filled up so the child can process all the equations in it and grandchild can print all the answers, i am trying to find a way(without shared memory) so that the child will keep looping or something until the parent outputs the next equation to file 2 and so on. My other problem is putting the formatted(number into letter) equation into a char array since the output has to have '*' and '+'. Ill include the code if someone needs to look at it. Its not finished though.

    Code:
    #include <stdio.h>
    FILE *Rfp,*Wfp;
    char s[4];
    char letter[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    char c;
    int  main()
    {
      void GrandchildProc(void);
      void ParentProc(void);
      void ChildProc(void);
      int compareInsert(char *s[4],int nums[2]);
    
      if(fork()==0){
        if(fork()==0){        //Grandchild
          //GrandchildProc();
        }
        else{                   //Child
          ChildProc();
        }
      }
    //-------------------------------------------------------------------
      else{                     //Parent
        //ParentProc();
      }
    
    return 0;
    }
    
    //Functions----------------------------------------------------------
    void ParentProc(){
    	int x,i,num,k;
    	int nums[3];
    	char final[16+sizeof(getpid())];
    	Rfp = fopen("File1.txt","r");
    	Wfp = fopen("File2.txt","w");
    	if(!Rfp)exit(1);
    	else{
    	  while(fscanf(Rfp, "%s",s)!=EOF){
    	    for(i=0;i<=sizeof(s);i++){
    	      final[i] = s[i];
    	    }//for
    	    k = 0;
    	    for(i=0;i <= sizeof(s);i++){
    	      for(x=0;x <= sizeof(letter);x++){
    		    if(letter[x] == s[i]){
                  num = x+1;
                  nums[k] = num;
                  //printf("%c%d%d\n",s[i],i,num);
    	          k++;
    	        } //if
    		  }//for
            }//for
    	    compareInsert(s,nums);
    	    final[5] = '=';
    	    for(i=0,x=6;i<=sizeof(s);i++,x++){
    	      final[x] = s[i];
    	    }//for
            for(i=11;i<=15;i++){
    	      final[i] = ' ';
    	    }//for
    	    //printf("%d",sizeof(final));
    	    fprintf(Wfp,"%s%d%c",final,getpid(),'\n');
    	  } //while
    	  fclose(Rfp);
    	  fclose(Wfp);
        }//else
    }//parent
    
    int compareInsert(char *s[4],int nums[3]){
      int num1,num2,num3;
      num1 = nums[0];
      num2 = nums[1];
      num3 = nums[2];
      if(*s[1] == '*' && *s[3] == '*'){
        sprintf(*s,"%d*%d*%d",num1,num2,num3);
      }//if
      else if(*s[1] == '+' && *s[3] == '+'){
        sprintf(*s,"%d+%d+%d",num1,num2,num3);
      }//else if
      else if(*s[1] == '+' && *s[3] == '*'){
          sprintf(*s,"%d+%d*%d",num1,num2,num3);
      }//else if
      else if(*s[1] == '*' && *s[3] == '+'){
          sprintf(*s,"%d*%d+%d",num1,num2,num3);
      }//else if
    }//compareInsert
    
    
    
    void ChildProc(){
    	int start, end, i, j;
    	int startTF,endTF = 0;
    	char read[21];
    	char newRead[5];
    	Rfp = fopen("File2.txt","r");
        Wfp = fopen("Fiel3.txt","w");
    	if(!Rfp)exit(1);
    	else{
    	  while(fscanf(Rfp, "%s",read)!=EOF){
            printf("%s%c",read,'\n');
    		for(i=0;i<=sizeof(read);i++){
    		  if(read[i] == '='){
    		    start = i;
    	        startTF = 1;
    	        //printf("start: %d%c",start,'\n');
    	     }//if
              if(read[i] == ' '){
    		    end = i;
    		    endTF = 1;
    		    //printf("end: %d%c",end,'\n');
    		  }//if
              if(startTF == 1 && endTF == 1){
    			  break;
    		  }
            }//for
    	    j = 0;
    	    for(start;start<=end;start++){
    	      newRead[j]=read[start];
    	      j++;
    	  	}//for
    	    //printf("%s%c",newRead,'\n');
    	  }//while
    	  fclose(Rfp);
          fclose(Wfp);
    	}//else
    }//Child
    
    
    void GrandchildProc(){
    	 Rfp = fopen("File3.txt","r");
    	 if(!Rfp)exit(1);
    	 else{
    	   while(strcmp((fgets(s,20,Rfp)),"END")!=0){
    	     printf("Grandchild:  %s\n",s);
    	   }
    	   fclose(Rfp);
    	 }
     }//Grandchild

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21