LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2010, 04:50 AM   #1
iqra
Member
 
Registered: May 2010
Posts: 34

Rep: Reputation: 15
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!
 
Old 06-20-2010, 05:55 AM   #2
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by iqra View Post
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
 
Old 06-20-2010, 06:10 AM   #3
iqra
Member
 
Registered: May 2010
Posts: 34

Original Poster
Rep: Reputation: 15
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);
 	
	}

Last edited by iqra; 06-20-2010 at 06:53 AM.
 
Old 06-20-2010, 09:01 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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)

Last edited by Wim Sturkenboom; 06-20-2010 at 09:08 AM.
 
Old 06-20-2010, 09:20 AM   #5
iqra
Member
 
Registered: May 2010
Posts: 34

Original Poster
Rep: Reputation: 15
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....
:-(
 
Old 06-20-2010, 09:32 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
1 members found this post helpful.
Old 06-20-2010, 09:43 AM   #7
iqra
Member
 
Registered: May 2010
Posts: 34

Original Poster
Rep: Reputation: 15
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!
 
Old 06-20-2010, 09:47 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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
 
Old 06-20-2010, 09:58 AM   #9
iqra
Member
 
Registered: May 2010
Posts: 34

Original Poster
Rep: Reputation: 15
okk!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
giving the answer from the script basak Linux - Software 3 08-14-2006 10:36 AM
Can you tell me the correct answer? hkl8324 Linux - General 3 03-29-2006 10:26 AM
giving apache the correct permissions dflorence Linux - Newbie 3 11-06-2003 09:27 AM
Java - System.in.read() not giving correct value. Jose Muņiz Programming 4 09-15-2003 10:27 AM
How do I setup Wildcard MX - Will PayPal $100 to first correct answer. 360 Linux - Networking 0 11-04-2002 11:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration