473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

atoi() not giving correct answer

hii

i m using atoi() to convert char value to int. and then passing that int as a swtich variable.

switch goes in a case and exectuing that particualr function but returning 0 value....

for example: i am finding no of process() and system up time().
when switch program goes in system up time it gives right values means correct no of days, hrs , min and sec but when it goes in no of process() it gives 0 answer....

why is it so???? plz help!!
Expand|Select|Wrap|Line Numbers
  1. int snmpget()
  2. {
  3. char recv_data[1024];
  4. // code to recieve msgs from client
  5.           bytes_read = recvfrom(sock,recv_data,1024,0,
  6.           (struct sockaddr *)&client_addr, &addr_len);
  7.  
  8.  
  9.       recv_data[bytes_read] = '\0';
  10.     printf("\n(%s , %d) requested :                       \n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
  11.          printf("%s", &recv_data);
  12.       fflush(stdout);
  13.  
  14.   int i=atoi(recv_data); // converting recv_data into integer
  15.  
  16.     switch(i)
  17.     {
  18.     case 1:    
  19.         systm_time();
  20.         break;
  21.     case 2:
  22.         no_of_process();
  23.         break;
  24.     default:
  25.         printf("\tINCORRECT VALUE");
  26.         break;
  27.     }
  28.  
  29. }    
  30.  //  this function is printing right values for hrs, min, sec, and days
  31.     int systm_time()
  32.         {    
  33.         char send_data[1024];
  34.         if(sysinfo(&sys_info) != 0)
  35.             perror("sysinfo");
  36.  
  37.           // Uptime
  38.           days = sys_info.uptime / 86400;
  39.           hours = (sys_info.uptime / 3600) - (days * 24);
  40.           mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);
  41.  
  42.           printf("Uptime: %ddays, %dhours, %dminutes, %ldseconds\n",
  43.                               days, hours, mins, sys_info.uptime % 60);
  44.  
  45.         }    
  46. // this functions is returning 0
  47. int no_of_process()
  48.     {
  49.          // Number of processes currently running.
  50.           printf("Number of processes: %d\n", sys_info.procs);
  51.  
  52.     }
Jun 20 '10 #1
5 4528
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. int i=atoi(recv_data); // converting recv_data into integer 
This is converting recv_data[0] to an int. Is that your intent?

If recv_data is a string to convert ot an int, then you use strtol.

BTW: A char is an integer. Therefore, there is no need to convert a char to an int for a switch. You can just use the char directly for the cases of the switch.
Jun 20 '10 #2
thanx for ur reply...
my prob is solved. there was nothing wrong in atoi().

i was missing 2lines code in no_of_process().
that was creating prob.

:-)
Regards!
Jun 20 '10 #3
johny10151981
1,059 1GB
@weaknessforcats
Even though the problem is solved. I need to as a question to weaknessforcats

Expand|Select|Wrap|Line Numbers
  1. int i=atoi(recv_data); // converting recv_data into integer 
why above code will work only on
recv_data[0]. Its not clear to me. From knowledge and understanding atoi function was suppose to work until it finds '\0' i.e. end of string character.
Besides if it was suppose to work only on one byte compiler would generate an error message.

Please let me know.
Best Regards,
Johny
Jun 21 '10 #4
Banfa
9,065 Expert Mod 8TB
The function atoi converts a C string (a NULL terminated series of printable characters in the execution character set) of digits into its corresponding int value.

It discards any leading white space and then converts as many characters as it can stopping only when it finds a non digit character.
Jun 21 '10 #5
@ johny10151981

well in recv_data we are geting data from client. it recieves string from client. its just a char array that stores messge from client side..


well there was nothing wrong with atoi().. :-) i was doing a mistake in another function.

Regards!
Jun 21 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Henry Jordon | last post by:
This is a piece of code that I have left to complete my project. I have hopefully one small error that needs to be fixed. This portion of the code evaluates the postfix notation that is passed to...
12
by: mike cox | last post by:
I'm running PostgreSQL 8.0 beta 1. I'm using the earthdistance to find the distance between two different latitude and logitude locations. Unfortunately, the result seems to be wrong. Here is...
2
by: mike_li | last post by:
4) If the following commands are executed: UPDATE DBM CFG USING DFT_MON_LOCK OFF UPDATE MONITOR SWITCHES USING lock ON And the DB2 instance is stopped and restarted, the monitor switch for the...
1
by: Kush | last post by:
Hi. I just installed db2 fixpak 4 for DB2 8.1 on AIX 5.3. The previous filesets for db2 were at 8.1.1.24 level and the new ones are at 8.1.1.40 after the fixpak installation. I have rebooted the...
3
by: kashifabbasi786 | last post by:
I have the following code in vb.net,which work very fine,but i want to convert it in C#.net(which is also pasted below),But it it have error on "item" and on "Index" some one help me plz VB.Net...
7
mb60
by: mb60 | last post by:
sir In my database I have one column for question. Four columns are meant for options 1,2,3,4. The 5th column is YES/NO column. The answers for the questions are entered into 5th column by a...
1
Claus Mygind
by: Claus Mygind | last post by:
I am doing some work with displaying the local time. I would like to get this format: 2:18:00 pm Using the sample code from w3Schools.com below, I can get the correct results from IE and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.