Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // StrToFloat.cpp : main project file.
- #include "stdafx.h"
- #include<iostream>
- #include<string.h>
- #include<math.h>
- using namespace std;
- void conversion(long float &convert,char str[])//It is used to convert string into long float(eg--"1.2"=1.2)
- {
- int i=0;
- int dot=0,pw=1;
- while(str[i]!='\0')
- {
- if(str[i]=='.')
- {
- convert=convert*1.0;
- dot=1;
- i++;
- }
- else
- {
- if(dot==0)
- convert=convert*10+str[i++]-48;
- else
- convert=convert+pow(1/10.0,pw++)*(str[i++]-48);
- }
- }
- }
- void validation(char str[],int &id)//2 check the validation and c if it is alphabetic string or float string
- {
- for(int i=0;i<strlen(str);i++)
- {
- if(isalpha(str[i]))
- {
- id=1;
- break;
- }
- }
- }
- int main()
- {
- char str[20];
- cout<<"Please enter first string : ";
- cin.getline(str,20,'\n');
- int id=0,i;
- validation(str,id);
- if(id==1)
- {
- cout<<"Sorry, it is not convertible to a long float number.\nPlease enter a valid input..";
- cout<<"\nPress any key to exit..";
- cin.get();
- exit(1);
- }
- else
- {
- cout<<"Congrats!!! You entered a valid input "<<char(1);
- long float convert=0.0;
- conversion(convert,str);
- cout<<"\nAfter converting, the long float number is : "<<convert<<"\n";
- long float convert1=0.0;
- cout<<"\n\nNow, Please enter second string : ";
- cin.getline(str,20,'\n');
- id=0;
- validation(str,id);
- if(id==1)
- {
- cout<<"Sorry, it is not convertible to a long float number.\nPlease enter a valid value..";
- cout<<"\nPress any key to exit..";
- cin.get();
- exit(1);
- }
- else
- {
- long float convert1=0.0;
- cout<<"Congrats!!! You entered a valid input "<<char(1);
- conversion(convert1,str);
- cout<<"\nAfter converting, the long float number is : "<<convert1<<"\n";
- cout<<"\n\nAfter multiplication, the result is : "<<convert*convert1<<"\n\n\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement