Problem Create a personal library of data entry functions. I need help setting u
ID: 3764941 • Letter: P
Question
Problem
Create a personal library of data entry functions.
I need help setting up this personal library since this is my first time doing so and I'm still really lost in performing the programming for this project.
Specification
Implement the following functions:
bool isValidInt( string str )
Returns true if str can be parsed into a valid int, false otherwise.
int getInt()
Returns a valid integer entered from the keyboard.
bool isValidReal( string str )
Returns true if str can be parsed into a valid double, false otherwise.
double getReal()
Returns a valid real (double) entered from the keyboard.
Each "get" function should do the following:
Prompt the user to enter the appropriate value.
Store the characters entered as a string.
Convert the string to the appropriate numeric value.
If the conversion fails, throw an exception and allow the user to re-enter the value.
Store these functions in namespace "dataChecks" stored in dataChecksNamespace.cpp:
"C:CSIS1600MyCppUtilsdataChecksNamespace.cpp"
Write a test application to validate that your functions work properly.
Explanation / Answer
/* c program converts string to integer */
#include<stdio.h>
int stringToInt(char[] );
int main(){
char str[10];
int intValue;
printf(" Enter any integer as a string: ");
scanf("%s",str);
intValue = stringToInt(str);
printf(" Equivalent integer value: %d",intValue);
return 0;
}
int stringToInt(char str[]){
int i=0,sum=0;
while(str[i]!=''){
if(str[i]< 48 || str[i] > 57){
printf("Unable to convert it into integer. ");
return 0;
}
else{
sum = sum*10 + (str[i] - 48);
i++;
}
}
return sum;
}
output:
Enter any integer as a string: 123
Equivalent integer value: 123
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.