Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(void) {
- char buf[7];
- int i, number;
- while (1) {
- printf("入力: ");
- fgets(buf, 7, stdin);
- buf[strlen(buf) - 1] = '\0';
- if (strlen(buf) == 0) {
- fprintf(stderr, "入力がありません\n");
- goto error;
- }
- for (i = 0; i < strlen(buf); i++) {
- if (i == 0 && (buf[0] == '+' || buf[0] == '-')) continue;
- if (!isdigit(buf[i])) {
- fprintf(stderr, "半角正数以外は全て入力無効です\n");
- goto error;
- }
- }
- number = atoi(buf);
- if (number < 0) {
- fprintf(stderr, "マイナスの値には絶対になりません\n");
- goto error;
- }
- break;
- error: ;
- }
- printf("input: %d\n", number);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement