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.

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

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.

Recommended Answers

All 5 Replies

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?

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.!!

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

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

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.

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.

if (arth= strchr(buf,'+')) 			
		{								
				num1= strtol(buf, &bad, 10);		
				num2 = strtol(arth+1, NULL, 0);		
				printf("you are adding %ld + %ld\n", num1, num2);
				sleep(1);
				sum = num1 + num2;
				printf("the answer is: %ld\n", sum);
			
		}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.