Floating Point isalpha();

This is a discussion on Floating Point isalpha(); within the C++ Programming forums, part of the General Programming Boards category; Can isalpha() check floating point variables, here is my code. Code: #include <iostream.h> #include <conio.h> #include <stdlib.h> #include <string.h> #include ...

  1. #1
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    Floating Point isalpha();

    Can isalpha() check floating point variables, here is my code.

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    
    int main(void)
    
    {
    
    	//Declare the variables
    	float cost, quant, price, vat, full, temp;
    	int length, count;
    	char yesno, vats[10];
    
    		  while (yesno != 'n' && yesno != 'N')
    		  {
    				//Get info from user
    				clrscr();
    				gotoxy(20, 12);
    
    				cout<<"Please enter the price of the product: ";
    				cin>>cost;
    
    				if (isalpha(cost))
    				{
    					 cout<<"Thats Wrong";
    				}
    
    				clrscr();
    				gotoxy(20, 12);
    
    				cout<<"Please enter the quantity of products: ";
    				cin>>quant;
    
    				if (isalpha(quant))
    				{
    					 cout<<"Thats Wrong";
    				}
    
    				clrscr();
    				gotoxy(20, 12);
    
    				cout<<"Please enter your rate of vat: ";
    				cin.ignore();
    				cin.getline(vats, 10);
    
    				length=strlen(vats);
    
    				if (length == 0)
    				{
    					 temp = 17.5;
    				}
    
    				else
    				{
    					 temp=atof(vats);
    				}
    
    
    				//do the calculation
    				temp= temp / 100;
    				price= cost * quant;
    				vat= cost * quant * temp;
    				full= price + vat;
    
    				clrscr();
    				gotoxy(20, 12);
    
    				//give the answer
    				cout<<"The price of "<<quant<<" product(s) at "<<cost<<" each = "<<price;
    
    				gotoxy(20, 14);
    
    				cout<<"The vat at "<<temp*100<<"% = "<<vat;
    
    				gotoxy(20, 16);
    
    				cout<<"Price plus vat = "<<full;
    
    				gotoxy(20, 19);
    
    				cout<<"Would you like to use the program again? ";
    				cin>>yesno;
    				cout<<"\n";
    
    		  }
    		  clrscr();
    		  gotoxy(20, 12);
    
    		  cout<<"Thankyou Bye!";
    
    		  for (count=0; count<=20000; count++)
    		  {
    				for (int j=0; j<=15000; j++)
    				{
    				}
    		  }
    
    return 0;
    }
    don't worry about the for loop at the end and all that its only the isalpha I'm having trouble with.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    I thought isalpha() was used to check for characters and not digits. Isn't isdigit() what you want?
    Be a leader and not a follower.

  3. #3
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    You have to read them as strings if you want to use that function. Check all the characters in the string including one dot. Or use cin.fail() to determine if assignment to float succeeded...

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    There is a function called atof that will return a floating point number from a string agrument. This should help you hopefully.

    Code:
    #include <iostream.h>
    
    int main(void)
    {
      const int MAX_BUFFER=512;
    
      double Converted=0;
      char Buffer[MAX_BUFFER];
    
      cout << "Please enter a floating point number: ";
      cin.get(Buffer, sizeof Buffer);
    
      Converted=atof(Buffer);
    
      cout << "\n\nThe value of double is: " << Converted;
      getchar();
    
      return 0;
    }

  5. #5
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Code:
    $man atof
    
    ATOF(3)             Linux Programmer's
    Manual             ATOF(3)
    
    NAME
           atof - convert a string to a double.
    
    SYNOPSIS
           #include <stdlib.h>
    
           double atof(const char *nptr);
    
    DESCRIPTION
           The  atof()  function  converts the initial portion of the
           string pointed to by nptr to double.  The behaviour is the
           same as
    
                  strtod(nptr, (char **)NULL);
    
           except that atof() does not detect errors.
    atof does not check if input is invalid, so that should be remembered.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    What you're looking for is something like this:

    Code:
    cin >> value; 
           
    while (cin.fail())
    { 
               cin.clear(); 
               cin.ignore(0xFF,'\n');
               cout << "An error has occoured! Please re-enter your value: ";
               cin >> value;
    }

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Code you explain what "cin.ignore(0xFF,'\n');" does by any chance, thanks.

  8. #8
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    istream& ignore (streamsize n = 1, int delim = EOF );
    Extract and discard characters.
    Extracts characters from input stream and discards them.
    Extraction ends when n characters have been discarded or when delim is found, whichever comes first. In this last case delim is also extracted.

    Parameters:

    n
    Maximum number of characters to be extracted and ignored.
    This is an integer value of type streamsize
    delim
    Delimiter character.

    Return Value:

    The function returns *this

    Example:

    Code:
    // istream ignore
    #include <iostream>
    using namespace std;
    
    int main () 
    {
        char first, last;
    
        cout << "Enter your first and last names: ";
     
        first=cin.get();
        cin.ignore(256,' ');
    
        last=cin.get();
    
        cout << "Your initials are " << first << last;
    
        return 0;
    }
    This example shows the use of ignore to ignore input characters until a whitespace is found.
    Last edited by Dual-Catfish; 06-29-2002 at 09:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 12:56 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 11:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 11:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 01:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21