1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

atoi/atof problem

Discussion in 'C' started by askmewhy25, Mar 11, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    How will I capture a string or char input if I have a checker that is equated to zero
    because atoi/atof reads the string or char as zero

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    int a(double sales,double sepcost)
    {
        double NRV;
        NRV=sales-sepcost; 
        printf("The Net Realizable Value is: %.3lf\n",NRV);
    
    }
    int main()
    {
        double sales,sepcost;
        char error[128];
      
        do{
          system("cls");
          printf("Please input your Sales Value: \n");
          scanf("%s",&error);
          sales=atof(error);
          printf("Please input your Separable Cost: \n");
          scanf("%s",&error);
          sepcost=atof(error);
          if(sales<0){
            system("cls");
            printf("Invalid input for Sales, Please try again!!\n");
            system("pause");
            }
          if(sepcost<0){
            system("cls");
            printf("Invalid input for Seperabe Cost, Please try again!!\n");
            system("pause");
            }
          }while(sales<0||sepcost<0);
          a(sales,sepcost);
    
    getch();
    }
    
    
     
  2. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    atoi and atof function will return the converted values.

    your checking the return value is 0 or not. instead of you can check the given string is digit or not using isdigit() or isalpha()
     
  3. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    How to do that?I don't know how to use the ctype.h
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    isdigit takes a char parameter and returns TRUE or FALSE depending on whether it's a digit or not. So the following will output 01:
    Code:
    printf("%c%c", isdigit('x')?'1':'0', isdigit('3')?'1':'0');
    
    To determine whether input is valid you have to scan the string and see what you find. "123a456" is not a valid integer because 'a' is not a digit. "-2.35e+17" is a valid floating point number and the validation for that is more complex: optional sign, a digit, a dot, some digits, an e, optional sign, some digits.
     

Share This Page