Hi there!
i am have a problem with the following code.
i would like to check if the string is a positive decimal number.
if the string has a letter or minus sign it steps into the 'if' loop, which is perfect.
However if the string has a decimal point, it steps into the loop, which is not suppose to happen
the conditions for stepping into the 'if' loop are if a letter, minus sign are present.
if (!isdigit(principal)) excludes any non digit character i assume...
how do i exclude a decimal point in a string?
is there anyway to fix this using the isdigit or isalpha funtions?
thanks
#include<iostream>
#include<string>
using namespace std;
int main(){
string principal ;
int period = 0;
float interest = 0;
cout<<"Program to Calculate Sum of Money"<<endl;
cout<<"Enter Principal to 2 decimal places "<<endl;
cin>>principal;
for(int i= 0; i < principal.size(); i++){// checking for letter and minus sign
if( !isdigit(principal)){
cout<<"Principal is NOT a Positive number "<<endl;
cin>>principal;
i= -1;
}}
return 0 ;
}
i am have a problem with the following code.
i would like to check if the string is a positive decimal number.
if the string has a letter or minus sign it steps into the 'if' loop, which is perfect.
However if the string has a decimal point, it steps into the loop, which is not suppose to happen
the conditions for stepping into the 'if' loop are if a letter, minus sign are present.
if (!isdigit(principal)) excludes any non digit character i assume...
how do i exclude a decimal point in a string?
is there anyway to fix this using the isdigit or isalpha funtions?
thanks
#include<iostream>
#include<string>
using namespace std;
int main(){
string principal ;
int period = 0;
float interest = 0;
cout<<"Program to Calculate Sum of Money"<<endl;
cout<<"Enter Principal to 2 decimal places "<<endl;
cin>>principal;
for(int i= 0; i < principal.size(); i++){// checking for letter and minus sign
if( !isdigit(principal)){
cout<<"Principal is NOT a Positive number "<<endl;
cin>>principal;
i= -1;
}}
return 0 ;
}