Change char to int and store in an array

Thread Solved

Join Date: May 2009
Posts: 3
Reputation: ngibson12 is an unknown quantity at this point 
Solved Threads: 0
ngibson12 ngibson12 is offline Offline
Newbie Poster

Change char to int and store in an array

 
0
  #1
May 7th, 2009
Hello guys, I am new to this forum, but I am sure I will be an active one as I am a computer science major. I am still kindof new to c programming and am hitting a major issue.

I am writing a echo client server code and I am trying to make my program do arithmetic and to do so I need to change a characters called buf in my code to integers and them add them and save them back as characters and stdout them. Another problem is what if sum is larger than 1 char, what would I do to make it save say 13 in buf[0] = 1 and buf[1]=3.

If there is another way around this please tell me!

Here is my piece I am working on any suggestions are much appreciated.

  1. if (buf[1] == '+')
  2. {
  3. one= atoi(&buf[0]);
  4. two = atoi(&buf[2]);
  5. sum = (one + two);
  6.  
  7. buf[0] = itoa(sum); // THIS DOES NOT WORK HELP!!!
  8. Rio_writen(connfd, buf, n);
  9. }

To explain this a bit more. Rio_written is (the connection, the buf array of characters, and then size or bytes of what was inputted).

so I am not sure this is still right because I can't get it to work, but I think I am on the right track!!
by the way the reason I want to convert it back into characters is because i would have to edit all of my code. I thought it would just be easier to put it back in char.
Last edited by ngibson12; May 7th, 2009 at 5:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,606
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Change char to int and store in an array

 
0
  #2
May 7th, 2009
the prototype for itoa is:
char * itoa ( int value, char * str, int base );
but it's really a bad function to use as it's not portable.

use sprintf, instead:
sprintf(buf,"%d",sum);
another problem you may be having is, how is "buf" terminated? what do you expect is at the buf[3] position?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: ngibson12 is an unknown quantity at this point 
Solved Threads: 0
ngibson12 ngibson12 is offline Offline
Newbie Poster

Re: Change char to int and store in an array

 
0
  #3
May 7th, 2009
Thank you so much for the help jep I really appreciate it!!

One last question.
This is only working for single digit numbers that I enter because of the if statment with + in buf[1] slot. My question is, is there a way to check to see if the arrray of buf[] has a + anywhere or do I have specifically say in buf[1]. If there is a way to do that, is there a way to change the characters to int without saying one= buf[0] because that would mean I would have to make quite a few variables. I guess a solvable way for this is jsut to make a bunch of ifstatments but I hope there is a better way. Again thanks so much.!!

  1. if (buf[1] == '+') // I would like to change this statement to search for '+' in the whole arrary not just buf[1];
  2. {
  3. one= atoi(&buf[0]);
  4. two = atoi(&buf[2]);
  5. sum = (one + two);
  6. sprintf(buf,"%d",sum);
  7. Rio_writen(connfd, buf, n);
  8. }

NOTE: Jep you were right about termintating buf. For example when ever i enter in a number that would = grater than 10 is goes in a wierd loop and will give me the previous problem i just did!
Example is below
2+2
4
3+3
6
9+9
6
(if i hit enter again it will give me)
6
(and if I hit enter again it will finally give me the right answer)
18


after that happens the whole code gets mest up
Last edited by ngibson12; May 7th, 2009 at 7:18 pm. Reason: insert code
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,606
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Change char to int and store in an array

 
0
  #4
May 7th, 2009
i think strchr is what you're looking for

it will point to the character in question. everything up to that character can be assumed* to be the characters of the first number, everything after can be assumed* to be the characters of the second number.

atol or strtol will accept the first one or more numeric characters and ignore non-numerics following thereafter.

*Definition: ass-u-me: the behavior often preceding an event where someone gets shot in the foot.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: ngibson12 is an unknown quantity at this point 
Solved Threads: 0
ngibson12 ngibson12 is offline Offline
Newbie Poster

Re: Change char to int and store in an array

 
0
  #5
May 8th, 2009
Hey jeb thanks sooooo much I finally figured everything out and got it working almost perfectly, but it has such a minor glitch I dont even care! I thought you might want to see what my new code looked like that I made.

  1. if (arth= strchr(buf,'+'))
  2. {
  3. num1= strtol(buf, &bad, 10);
  4. num2 = strtol(arth+1, NULL, 0);
  5. printf("you are adding %ld + %ld\n", num1, num2);
  6. sleep(1);
  7. sum = num1 + num2;
  8. printf("the answer is: %ld\n", sum);
  9.  
  10. }
Last edited by ngibson12; May 8th, 2009 at 1:42 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,606
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Change char to int and store in an array

 
0
  #6
May 8th, 2009
I'm glad it worked for you. I'm even more glad you worked it out on your own. that's the best way to learn.

you'll figure out the "glitches" and "best practices" in time. I still am, myself.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC