LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   atoi() not giving correct answer (https://www.linuxquestions.org/questions/programming-9/atoi-not-giving-correct-answer-815240/)

iqra 06-20-2010 03:50 AM

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

Regards!

ForzaItalia2006 06-20-2010 04:55 AM

Quote:

Originally Posted by iqra (Post 4009141)
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....

Could you please post some _extracts_ from your code? I think that would help us to better understand your problem ...

Andi

iqra 06-20-2010 05:10 AM

ok here the code
Code:

int snmpget()
{
char recv_data[1024];
// code to recieve msgs from client
          bytes_read = recvfrom(sock,recv_data,1024,0,
              (struct sockaddr *)&client_addr, &addr_len);
         

          recv_data[bytes_read] = '\0';
    printf("\n(%s , %d) requested :                      \n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
        printf("%s", &recv_data);
          fflush(stdout);
       
  int i=atoi(recv_data); // converting recv_data into integer
       
        switch(i)
        {
        case 1:       
                systm_time();
                break;
        case 2:
                no_of_process();
                break;
        default:
                printf("\tINCORRECT VALUE");
                break;
        }

}       
 //  this function is printing right values for hrs, min, sec, and days
        int systm_time()
                {       
                char send_data[1024];
                if(sysinfo(&sys_info) != 0)
                    perror("sysinfo");
               
                  // Uptime
                  days = sys_info.uptime / 86400;
                  hours = (sys_info.uptime / 3600) - (days * 24);
                  mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);
       
                  printf("Uptime: %ddays, %dhours, %dminutes, %ldseconds\n",
                                      days, hours, mins, sys_info.uptime % 60);
               
                }       
// this functions is returning 0
int no_of_process()
        {
                // Number of processes currently running.
                  printf("Number of processes: %d\n", sys_info.procs);
       
        }


Wim Sturkenboom 06-20-2010 08:01 AM

Your question is very confusing. You're talking about atoi() but also about systm_time() working properly and no_of_process() always printing 0.

If atoi() returned the incorrect value (0), you should see 'INCORRECT VALUE' on your screen, so I assume your problem is not with atoi() but with no_of_process(). Correct?

PS And you don't call sysinfo() in no_of_process() ! So the sysinfo structure contains random data (possibly zeroes but not guaranteed without seeing the rest of the code)

iqra 06-20-2010 08:20 AM

yes i knw its very confusing.... actually i didnt understand y this code is doing like this...

i included sysinfo struct in main also. and u know sometimes my program gives correct ans means correct no of process but when i run this code again it gives 0 no of process...

i have other paramters also in this program like total and free ram, swaped space, etc....
and same 0 i get even for total ram. and swaped space.

sometimes it gives corect ans and sometimes it gives 0....
:-(

Wim Sturkenboom 06-20-2010 08:32 AM

Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>

struct sysinfo sys_info;

void system_time();
void no_of_process();

int main(int argc, char *argv[])
{
int i;
    if(argc!=2)
    {
        printf("incorrect usage; please pass number 1 or 2\n");
        return -1;
    }

    i=atoi(argv[1]);
    printf("option %d selected\n", i);

    switch(i) {
    case 1:
        system_time();
        break;
    case 2:
        no_of_process();
        break;
    default:
        printf("\tINCORRECT value\n");
        break;
    }


    return 0;
}

void system_time()
{
char send_data[1024];
int days, hours,mins;
    if(sysinfo(&sys_info) != 0)
        perror("sysinfo");
    // Uptime
    days = sys_info.uptime / 86400;
    hours = (sys_info.uptime / 3600) - (days * 24);
    mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);

    printf("Uptime: %ddays, %dhours, %dminutes, %ldseconds\n",
    days, hours, mins, sys_info.uptime % 60);
}

void no_of_process()
{
    if(sysinfo(&sys_info) != 0)
        perror("sysinfo");
    printf("Number of processes: %d\n", sys_info.procs);
}

I can not look in your code to see what exactly happens, so made a rough copy of yours and modified no_of_process(). I created a global struct sysinfo for the example as I guess that that is how your code works.

It runs properly on my system returning a consistent number of processes each time I run it.

PS: If all options relate to sysinfo, I would call sysinfo before entering the switch.

iqra 06-20-2010 08:43 AM

thanx!! :-)
its working fine now!! i exectued my code 3 times and is giving right answer now...!! i wish i dont create prob again...

thnx for ur help!

Regards!

Wim Sturkenboom 06-20-2010 08:47 AM

Great; was fun for a sunday afternoon ;)

Please mark your thread as solved using the thread tools just above the opening post.

One little note:
If your original program runs in an endless loop, it will work if you first use option '1' and next option '2'. Option '1' will initialize the sysinfo struct.

WimS

iqra 06-20-2010 08:58 AM

okk!!!


All times are GMT -5. The time now is 11:18 PM.