0

I need to detect if the input is a new line. After that, I need to convert it to '\0'. Here is my code:

void number()
{
    printf(LEV3"Student No. (20XXXXXXX):\t");
    int x=0, i=0;
    fgets(studno[i], LEN, stdin);
    if('\n'==studno[LEN-1])
        {
        [LEN-1]='\0';
        }
    x = atoi(studno[i]);
    if (((x/10000000)>=21||(x/10000000)<=19))
    {
        printf("ERROR: Invalid Student Number. Format should be 20XXXXXXX.\n");
        number();
    }

    i++;
}

How can I do it? I always get a compiler error with this code.

Lloyd
  • 85
  • 1
  • 1
  • 11
  • 1
    Adding to @mbratch laundry list. variables that have no declarations (of which I count at least three). That said, [*read the documentation*](http://en.cppreference.com/w/c/io/fgets) of your apis, and check their results *before* relying on what they do only if they succeeded – WhozCraig Sep 07 '13 at 02:18

3 Answers3

0

Your code as shown has at least a couple of syntax errors.

  1. The string in printf(LEV3"Student No. ... has that stray "LEV3". Is that supposed to part of the format string or what is it?

  2. In your if that checks for newline, you left off the string name when attempting to set a null value in the string. Instead of [LEN-1] = '\0'; you should have studno[LEN-1] = '\0';.

  3. This function (number) calls itself... Eek! Did you mean for this function to be recursive? It's unclear what the purpose is. Your local variable i is being incremented as if the next call to number will see it, but it won't since it's a local variable. For something like this, you just want a loop, not recursion.

So using @Gangadhar's suggestion, your code ultimately should look like this for reading the line. I'll leave the rest for you to update since I'm not sure what your intention is with i etc, and you haven't explained what the format of the input is. I am assuming studno is an array of char pointers, but you're not showing it.

void number()
{
    printf("Student No. (20XXXXXXX):\t");
    int x = 0, i = 0;
    fgets(studno[i], LEN, stdin);

    if ('\n' == studno[i][strlen(studno)-1])
    {
        studno[i][strlen(studno)-1] = '\0';
    }

    ...
}
lurker
  • 56,987
  • 9
  • 69
  • 103
0

There are many many problems with the code you posted, and with the code below, but I think it answers your question.

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

#define LEV3 "\t\t\t"
#define LEN 30
char studno[LEN+1];

void number()
{
  printf(LEV3"Student No. (20XXXXXXX):\t");
  fgets(studno, LEN, stdin);

  int x = atoi(studno);
  if (((x/10000000)>=21||(x/10000000)<=19))
  {
    printf("ERROR: Invalid Student Number. Format should be 20XXXXXXX.\n");
    number();
  }

  int l = strlen(studno);
  if('\n'==studno[l-1])
  {
    studno[l-1]='\0';
  }
}

int main(int argc, char* argv[]) {
  number();
  printf("'%s'", studno);
  return 0;
}
Adam Burry
  • 1,904
  • 13
  • 20
  • 1
    +1, but although the original code uses recursion on error, I'm not convinced that's a good idea. Also, `fgets()` is perfectly safe with `sizeof(studno)`; it doesn't write beyond the end of the array and does always write a null byte unless the size is zero. (And since your code doesn't use the arguments, it is better to define `int main(void) { ... }`. I like that you explicitly return 0, though.) – Jonathan Leffler Sep 07 '13 at 02:39
  • @JonathanLeffler, I freely admit to problems with this code. But if I rewrote the original any further, the poster would not recognize it and it would be less helpful. – Adam Burry Sep 07 '13 at 02:45
  • Yes — it is a fine line to draw. The important issue is knowing that `char buffer[SIZE]; if (fgets(buffer, sizeof(buffer), stdin) != 0) { ... }` is perfectly safe — your code 'wastes' an extra byte. The recursion is, as I said, in the original code. The `main()` function definition is an issue if you use a modern enough compiler that complains about unused variables. It was fiddly because you have to guess so much; the original code was less than satisfactory as an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). – Jonathan Leffler Sep 07 '13 at 03:32
0

After using fgets(buffer,...),
1. The buffer ends with the first occurrence of a \n and then a \0.
2. It could instead end with just a \0 and no \n.
3. The buffer could be unchanged if no input was available or an I/O error.

char buffer[N];
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
  ; // handle no data read as file I/O error or EOF
}
else {
  size_t len = strlen(buffer);
  // Be sure to check for len > 0
  if ((len > 0) && (buffer[len - 1] == '\n')) {
    buffer[--len] = '\0';
  }
  // use buffer
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256