atoi() not giving correct answer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iqra123
    New Member
    • Jun 2010
    • 4

    #1

    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!!
    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);
     	
    	}
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Code:
    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.

    Comment

    • iqra123
      New Member
      • Jun 2010
      • 4

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

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        Originally posted by weaknessforcats
        Code:
        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.
        Even though the problem is solved. I need to as a question to weaknessforcats

        Code:
        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

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          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.

          Comment

          • iqra123
            New Member
            • Jun 2010
            • 4

            #6
            @ 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!

            Comment

            Working...