2

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.

HansMiku
  • 23
  • 3
  • You take input in half a dozen places with `scanf()`, but you never check the return value of scanf and never print the value that was input. Learn to debug--don't just assume that you're getting what you expect--print it out! – Lee Daniel Crocker Oct 20 '17 at 02:11

2 Answers2

0

fix your 2nd approach like this

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void){
    char grade[50];
    float grade2;//int grade2; ?
    int invalid;//flag
    int i;

    printf("Please, insert a grade between 0 and 10.\n");

    do {
        invalid = 0;//false
        printf("Grade: ");fflush(stdout);
        fgets(grade, sizeof grade, stdin);
        for(i = 0; grade[i] && grade[i] != '\n'; ++i){
            if(!isdigit((unsigned char)grade[i])){
                invalid = 1;//It contains illegal characters.
                break;
            }
        }
        if(!invalid){
            grade2 = atof(grade);
            if(grade2 < 0 || grade2 > 10){
                printf("\nPlease, insert a valid value.\n");
                invalid = 1;//Invalid input range
            }
        }
    } while(invalid);
    printf("The grade is: %f\n", grade2);
    printf("Valid value.\n");
    //printf("Restart the program and try again.");//never execute

    system("PAUSE");
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

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");
}
Austin
  • 1,709
  • 20
  • 40
  • I'm sorry I needed some time to answer. I needed to focus on other things during these days. Thank you for your help. Now I'm back. I got a problem with memset not being declare but I did some research and figured out I needed #include It worked perfectly, but I don't know enough programming to do any modification (and it doesn't need). I studied this code and learned new things (such as using "sizeof"). It was really helpful. I have a question: is your code C++? What about those I wrote? I was trying to do it using C, but it seems there's a tiny difference between these languages. – HansMiku Nov 01 '17 at 18:25