How to capture strings or char inputs using atoi/atof if your range is 0?

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

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    How to capture strings or char inputs using atoi/atof if your range is 0
    becuse my programs continue if the input is a char or string due to that the
    string or char is read or equal to 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. arunlalds

    arunlalds Banned

    Joined:
    Mar 12, 2010
    Messages:
    43
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    student
    Location:
    India
  3. itstimetojazz

    itstimetojazz New Member

    Joined:
    Sep 9, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    what characters or strings are given as input to the atof in the above program?
     
  4. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    in c++ you can do this

    Code:
    
    #include <iostream>
    #include <conio.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");
                
          
          std::cin>>sales;
    
          if (std::cin.fail()){             
              std::cin.clear();
              std::cin.ignore(std::numeric_limits<int>::max(),'\n');
              sales=-1;
          }
                if(sales<0){
            system("cls");
            printf("Invalid input for Sales, Please try again!!\n");
            system("pause");
            }else{
          
          printf("Please input your Separable Cost: \n");
          std::cin>>sepcost;
          if (std::cin.fail()){             
              std::cin.clear();
              std::cin.ignore(std::numeric_limits<int>::max(),'\n');
              sepcost=-1;
          }
          
    
          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();
    }
    
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice