I want to code a program which receives a number between zero and ten and shows a message saying it's valid or not. If not, it should keep asking for a valid number.
I could code everything and it seems pretty okay for me, but it's not working properly.
I have seen many topics with the similar problems, but I couldn't solve my problem. I mean, I had some progress, but my code isn't working yet.
What I did: I created a variable to store the inserted value, used ctype library to make sure it will just accept numbers, converted my char to float and checked if it meets the requirements.
I tried some different codes, I googled it a lot and now I have no idea of what I should do.
CODE 1:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
main(){
char grade;
float grade2;
printf("Please, insert a grade between 0 and 10.\n");
printf("Grade: ");
scanf("%c",&grade);
while(isalpha(grade) || ispunct(grade) || isspace(grade) || iscntrl(grade)){
printf("Please, insert a valid value.\n");
fflush(stdin);
printf("Grade: ");
scanf("%c",&grade);
}
if(isdigit(grade)){
grade2 = atof(grade);
while(grade2 < 0 || grade2 > 10){
printf("\nPlease, insert a valid value.\n");
fflush(stdin);
printf("grade: ");
scanf("%c",&grade2);
}
printf("Valid value.\n");
}
else{
printf("Restart the program and try again.");
}
system("PAUSE");
}
The great problem with code 1 is that I get this:
[Error] invalid conversion from 'char' to 'const char*' [-fpermissive]
I just can't make it work, but it works here (code found on the internet):
#include <stdio.h>
#include <stdlib.h>
int main(){
char a[10] = "3.14";
float pi = atof(a);
printf("Value of pi = %f\n", pi);
return 0;
}
I thought it would be the scanf and initialization, but it worked receiving a value with scanf as well.
Finally, I thought it could be the array. I did:
CODE 2:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
main(){
char grade[50];
float grade2;
printf("Please, insert a grade between 0 and 10.\n");
printf("Grade: ");
scanf("%c",&grade[50]);
while(isalpha(grade[50]) || ispunct(grade[50]) || isspace(grade[50]) || iscntrl(grade[50])){
printf("Please, insert a valid value.\n");
fflush(stdin);
printf("Grade: ");
scanf("%c",&grade[50]);
}
if(isdigit(grade[50])){
grade2 = atof(grade);
while(grade2 < 0 || grade2 > 10){
printf("\nPlease, insert a valid value.\n");
fflush(stdin);
printf("Grade: ");
scanf("%c",&grade2);
}
printf("The grade is: %f\n", grade2);
printf("Valid value.\n");
}
else{
printf("Restart the program and try again.");
}
system("PAUSE");
}
It was always printing it's a valid value, no matter what number I wrote. So, I changed the code to see the value it was comparing and the output was:
Please, insert a grade between 0 and 10.
Grade: 11
The grade is: 0.000000
Valid value.
I googled it and I saw atof can return zero if something is wrong.
So, the last thing I tried to do was using this model: (also found on the internet)
double atof(const char *str)
Finally,
CODE 3:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
main(){
char grade;
printf("Please, insert a grade between 0 and 10.\n");
printf("Grade: ");
scanf("%c",&grade);
while(isalpha(grade) || ispunct(grade) || isspace(grade) || iscntrl(grade)){
printf("Please, insert a valid value.\n");
fflush(stdin);
printf("Grade: ");
scanf("%c",&grade);
}
if(isdigit(grade)){
float atof(grade *grade);
while(grade < 0 || grade > 10){
printf("\nPlease, insert a valid value.\n");
fflush(stdin);
printf("Grade: ");
scanf("%c",&grade);
}
printf("The grade is: %f\n", grade);
printf("Valid value.\n");
}
else{
printf("Restart the program and try again.");
}
system("PAUSE");
}
The output was:
Please, insert a grade between 0 and 10.
Grade: 11
Please, insert a valid value.
Grade: 6
Please, insert a valid value.
Grade: 10
Please, insert a valid value.
Grade: 5
Please, insert a valid value.
Grade:
I couldn't get out of the while.
I debugged it and found out it was stuck in the second while.
I tried to print the value again and I got another 0.000...
Well, I just came here because I did a good research on the internet but I still can't solve my problem.
I am sorry if I am doing anything wrong (like, violating a rule). If so, tell me and I will try to fix it.
Thank you for your attention.
Please try below code. It is self explanatory. If you need any specific explanation, please let me know.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int isnumeric_array(char *string)
{
while( (char)(*string) != '\0') {
if( !isdigit( (char)(*string) ) ) {
return 0;
}
string++;
}
return 1;
}
int main(){
char grade[128];
float grade2;
read_input:
memset(grade, 0x0, sizeof(grade));
printf("Please, insert a grade between 0 and 10.\n");
printf("Grade: ");
scanf("%s", grade);
if(isnumeric_array(grade) == 0) {
goto read_input;
}
grade2 = atof(grade);
if((grade2 < 0) || (grade2 > 10)) {
goto read_input;
}
else {
printf("You have entered right value\n");
}
system("PAUSE");
}