「入力した文字列が文字か数字かを判断する関数」を作ったのですが、実際にmain関数内で動かしたところ全て-1で返す結果となりました。
どこか間違えている部分があるでしょうか?
str=入力した文字列
n=チェックを行う文字列のバイト数
int numeric_chk(char *str, int n) { int i; for(i=0;i<n;i++) { if ((str[i]>='0')&&(str[i]<='9')) continue; else return(-1); } return 0; コード
追記
閲覧ありがとうございます。
下記がnumeric関数使用までのソースファイルとなります。
#include <stdio.h> #include <fcntl.h> #include <memory.h> #include <string.h> #include <io.h> #include <errno.h> #include <math.h> #include <stdlib.h> #include <sys\types.h> #include <sys\stat.h> /*********************************************************************/ /* 定数の定義 */ /*********************************************************************/ #define CR 0x0D #define LF 0x0A #define DATA SYAIN.MAS /*********************************************************************/ /* 構造体の宣言 */ /*********************************************************************/ struct syain_k { char sno[5]; /* 社員番号 */ char name[20]; /* 氏名 */ char salary[7]; /* 給与 */ char crlf[2]; /* CR/LF */ }; /*********************************************************************/ /* 静的変数の定義 */ /*********************************************************************/ /*********************************************************************/ /* 内部関数のプロトタイプ宣言 */ /*********************************************************************/ int numeric_chk(char *str,int n); int search(char *sno,struct syain_k *rec); void data_disp(struct syain_k *rec); void touroku(int recno, char *sno,char *name,char *salary); /*********************************************************************/ /* main() メイン関数 */ /*********************************************************************/ int Fd; initial_syori() { Fd = open("SYAIN.DAT",O_CREAT); } main() { struct syain_k rdbuf; int a; long d; int recno; char sno; char name; char salary; struct syain_k *rec; while(1) { printf("社員番号を入力してください\n"); scanf("%d",&sno); a=numeric_chk(&sno, 5); if (a==-1) { printf("NUMERICエラー\n"); continue; } else d=atol(&sno); if (d==99999) { return 0; } else if (d>=1000&&d<=90000) { printf("範囲エラー\n"); continue; } コード
サイズ(n)に文字列のサイズではなく終端(\0)も含んだサイズを入れていませんか?
sozeofでやると終端(\0)まで対象になるので−1になりますよ。

回答3件
あなたの回答
tips
プレビュー