2

The code works for every if statement except for the first one where if the statement is true, it proceeds to create an infinite loop of "Enter a student mark [0.00, 100,00] : " and "No input accepted!".

#include <stdio.h>

#define MIN 0
#define MAX 100

int getMark(void) {
    int mark;
    char ch;
    int repeat = 1;

    printf("Enter a student mark [0, 100] :  ");
    int r = scanf("%i%c", &mark, &ch);

    if (r == 0) {
        printf("**No input accepted!**\n");           
    }
    else if (ch != '\n') {
         printf("**Trailing characters!**\n");
    }
    else if (mark < MIN || mark > MAX) {
        printf("**Out of range!**\n");
    }
    return mark;
}

int main() {
    int mark;
    do {
        mark = getMark();
    } while (mark != 0);
}

What's causing it to loop and how do I fix it?

DanielBogo
  • 23
  • 3
  • 2
    Another day, another developer failing to understand floating point... – leppie Oct 24 '14 at 06:20
  • 1
    What's the difference between 0 and 0.0...four hours! – Mike McMahon Oct 24 '14 at 06:25
  • 2
    Sidenote: the definition for `main()` you are using is [not legal in C99](http://stackoverflow.com/a/1765701/211160), and it's better to get into the habit of writing `int main()`. – HostileFork says dont trust SE Oct 24 '14 at 06:35
  • Why did u use `while (mark != 0);` – Anbu.Sankar Oct 24 '14 at 06:46
  • 1
    The program works correctly. When I type in 0 the loop ends and the program exits. What input are you giving? Btw contrary to what every other commenter is saying there are no issues with floating point numbers: 0 always has an exact representation. – Joni Oct 24 '14 at 06:47
  • @leppie Please expand on how floating-point has anything to do with this problem. – Pascal Cuoq Oct 24 '14 at 08:53
  • @MikeMcMahon What are you talking about? – Pascal Cuoq Oct 24 '14 at 08:56
  • @PascalCuoq: Before the edits, it was. – leppie Oct 24 '14 at 09:01
  • @leppie No, the problem in the original version is the same as in the edited version and has nothing to do with floating-point: http://stackoverflow.com/revisions/26542486/1 – Pascal Cuoq Oct 24 '14 at 09:08
  • @PascalCuoq: Do you not see it? `double getMark(void)` ??? – leppie Oct 24 '14 at 09:18
  • @leppie Are you saying that it is incorrect to return a `double` from a function? – Pascal Cuoq Oct 24 '14 at 09:25
  • @PascalCuoq: No, I am saying the assignment to int can cause a problem, ie `(int)0.000000000001 = 1` – leppie Oct 24 '14 at 09:26
  • 1
    @leppie I don't see any assignment of a floating-point value to an int variable in the first version of the question. Also, you are confused about the semantics of the conversion from floating-point to integer. `(int)0.00000001` is definitely 0. – Pascal Cuoq Oct 24 '14 at 09:30
  • @PascalCuoq: In `main`. But yeah, my bad, not sure why I thought it was NOT truncating on cast. Too little coffee perhaps ;p – leppie Oct 24 '14 at 09:35

2 Answers2

0

1) If you input a string or just a char value you must remove it from your input stream before using it.

#include <stdio.h>

#define MIN 0
#define MAX 100

int getMark(void) //Funtion for marks input
{
    int mark;
    char ch;
    //int repeat = 1;
    char c;

    printf("Enter a student mark [0.00, 100,00] :  ");
    int r = scanf("%d %c", &mark, &ch);

    if (r == 0) //Input begins with a letter
    {
        printf("**No input accepted!**\n");
    }

    else if(ch == '\n') //Input ends with a letter
    {
       printf("**Trailing characters!**\n");
    }

    else if( mark < MIN ||  mark > MAX) //Range
    {
        printf("**Out of range!**\n");
    }

    //Remove the previous string from the input stream
    while ( (c = getchar()) != '\n' && c != EOF );

    return mark;
}


int main()
{
    int mark;

    do
    {
        mark = getMark();
    }while (mark != 0);
    return 0;
}

Output

Enter a student mark [0.00, 100,00] :  -10 c
**Out of range!**
Enter a student mark [0.00, 100,00] :  101 b
**Out of range!**
Enter a student mark [0.00, 100,00] :  50 a
Enter a student mark [0.00, 100,00] :  abcd
**No input accepted!**
Enter a student mark [0.00, 100,00] :  0 q
ani627
  • 5,578
  • 8
  • 39
  • 45
0

The problem is that with no input accepted the variable mark will be uninitialized and might be something else that 0. Your while loop will then continue forever without updating mark.

Henrik Carlqvist
  • 1,138
  • 5
  • 6
  • Good point. You should clarify in your answer that you are talking about `mark` in function `getMark`, that `scanf` is supposed to write to it, but that it may fail to do so and leave it uninitialized. – Pascal Cuoq Oct 24 '14 at 08:55
  • Yes, mark will be uninitialized in function getMark if scanf was unable to fill that value, but mark will also get that uninitialized value in the main function where the loop might continue forever. – Henrik Carlqvist Oct 24 '14 at 21:01