I have attempted [and made one] function to convert string to integer and catch errors in pure c
Working method
The whole working method is present in code itself
The header file
The str2num.h here:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int StrNumErr = 0;
int str2numErrCheck(){
if(StrNumErr>0){
int ab = StrNumErr;
StrNumErr = 0;
return ab;
}else{
return 0;
}
}
char * __restrict__ str2numErrText(int errnum){
if(errnum==22){
return "Alphabets or Symbols present in the String";
}
else if(errnum==23){
return "Two Points in one number Error";
}
else if(errnum==24){
return "Minus between two numbers Error";
}else{
return "";
}
}
void invalid_argument(const char * __restrict__ _err,int errcode)
{
StrNumErr = errcode;
}
float power(float a,int b)
//can also produce results to invalid powers [ex:- 0 to power 0 returns -1 [means invalid]]
//also can return negative powers [ex:- 2 to power -3 = 0.123]
//also calculates value for float numbers but power should be integer not **float**
{
if(a==0 && b==0){
return -1;
}
if(b==0){
return 1;
}
if(b<0){
float c=a;
for(int i=0;i<-(b-1);i++)
{
c/=a;
}
return c;
}
float c=a;
for(int i=0;i<b-1;i++)
{
c*=a;
}
return c;
}
float str2num_C(const char * __restrict__ _string)
{
/*
-=-=-=- Presenting the Str2Num_C Function -=-=-=-=-=-
Advantages =>
=> Can return float numbers
=> Can return negative numbers
=> Also throws error if the string is not a number
=> Can execute Fast
=> Also, The code is pretty straightforward
-=-=- How does this function Work -=-=-
Basically, it takes the string and loops around all the charachters
it checks if the charachter is '.' or '-' or among '0','1','2','3','4','5','6','7','8','9'
if the charachter is none of the above it throws error because if it is none of above then
alphabet or symbol is used!
if it finds '.' it checks if already point has been found or not
if yes
=> it throws error two points in number
if not
it will make point=1[means point has been found] and pointplace to 1
if it finds '-', it makes _minus = -1 which is multiplied at last to return minus number,
ex- "-98" returns 98 * minus = 98 * -1 = -98
also however if the minus is in between numbers it throws an error ex- "98-67" throws error
For all numbers it finds (0-9) as num
if it is first number, make num=number
else
it will multiply number by 10 and add the num
for ex-
98
num = 9
num = num * 10 + 8 [ which is 90 + 8 = 98 ]
*/
int _strlen = strlen(_string);
int _point=0;
int _minus = 1;
int _pointplace=0;
char _curchar;
float _return_num;
int _numfound = 0;
for(int _var=0;_var<_strlen;_var++)
{
//store current charachter
_curchar = _string[_var];
if(_curchar=='.'){
//if the value contains point!
if(_point==1){ //checking if already point is defined
//two points in one number huh?>
//error here :D
invalid_argument("Str2Num_C Error: Two Points in one number",23);
return 0;
}
//makes that _point is 1 [i.e point is true]
//and the pointplace is 1 like in 9.86 ['8' is in 1st pointplace]
_point=1;
_pointplace=1;
}
else if(_curchar=='-'){
//if the value contains minus
//check if its first position like "-98"
//if not then its something like "34-56" but that in whole is not number so throw error
if(_var==0){
_minus=-1;
}else{
invalid_argument("Str2Num_C Error: Minus in between numbers\n",24);
return 0;
}
}
else if(_curchar=='0'){
//if char is 0!
if(!(_numfound==1)){
_numfound=1;
_return_num = 0;
}
else if(_point==1){
_return_num+= 0/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=0;
}
}
else if(_curchar=='1'){
//if char is 1!
if(!(_numfound==1)){
_numfound=1;
_return_num = 1;
}
else if(_point==1){
_return_num+= 1/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=1;
}
}
else if(_curchar=='2'){
//if char is 2!
if(!(_numfound==1)){
_numfound=1;
_return_num = 2;
}
else if(_point==1){
_return_num+= 2/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=2;
}
}
else if(_curchar=='3'){
//if char is 3!
if(!(_numfound==1)){
_numfound=1;
_return_num = 3;
}
else if(_point==1){
_return_num+= 3/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=3;
}
}
else if(_curchar=='4'){
//if char is 4!
if(!(_numfound==1)){
_numfound=1;
_return_num = 4;
}
else if(_point==1){
_return_num+= 4/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=4;
}
}
else if(_curchar=='5'){
//if char is 5!
if(!(_numfound==1)){
_numfound=1;
_return_num = 5;
}
else if(_point==1){
_return_num+= 5/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=5;
}
}
else if(_curchar=='6'){
//if char is 6!
if(!(_numfound==1)){
_numfound=1;
_return_num = 6;
}
else if(_point==1){
_return_num+= 6/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=6;
}
}
else if(_curchar=='7'){
//if char is 7!
if(!(_numfound==1)){
_numfound=1;
_return_num = 7;
}
else if(_point==1){
_return_num+= 7/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=7;
}
}
else if(_curchar=='8'){
//if char is 8!
if(!(_numfound==1)){
_numfound=1;
_return_num = 8;
}
else if(_point==1){
_return_num+= 8/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=8;
}
}
else if(_curchar=='9'){
//if char is 9!
if(!(_numfound==1)){
_numfound=1;
_return_num = 9;
}
else if(_point==1){
_return_num+= 9/(power(10,_pointplace));
_pointplace+=1;
}
else{
_return_num*=10;
_return_num+=9;
}
}else{
invalid_argument("The string contains alphabetic charachters or symbol",22);
return 0;
}
}
return _return_num*_minus;
}
Example usage:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "str2num.h"
int main()
{
//declaring variables
char string[20];
float num;
int err;
//input a string
printf("Enter a number: ");
gets(string);
//convert into number and add 2
num = str2num_C(string);
num+=2;
//check errors
err = str2numErrCheck();
if(err>0){
printf("ERROR: %s",str2numErrText(err));
}
else{
printf("Number + 2 is: %f",num);
}
}
EDIT: the invalid_argument() also has a field to give string to it because when converting this code to c++ you just have to add throw and remove the numbers
gets()
??? \$\endgroup\$