#include <stdio.h> #define Correct 1 #define Error 0 enum eScale { binary, octal, hexadecimal, decimal, } scale_pat; void input_scale(int scale_pat); int main (void){ int cnt, check; char msg[10]; printf("Please choose the following\n\n"); printf("binary scale : 1\noctal scale : 2\nhexadecimal scale : 3\ndecimal scale : 4\n\n"); printf("Please input >>>"); do{ scanf("%d", &cnt); switch(cnt){ case 1: input_scale(binary); check = Correct; break; case 2: input_scale(octal); check = Correct; break; case 3: input_scale(hexadecimal); check = Correct; break; case 4: input_scale(decimal); check = Correct; break; default : printf("*Error*\n"); printf("Please input different value >>>"); check = Error; break; } }while(check == Error); return 0; } void input_scale(const int scale_pat){ int a; char msg[10]; gets(msg); printf("test is %s\n", msg); return; }
いま作っているプログラムは途中なので目的は伏せて起きます.
input_scale関数(現在はテスト中なので内容は変わりますが)内でgets(msg)がうまく動いてくれません。ですがscanf("%s", msg)とするとうまく動いてくれます。
どうしてこのような事態がおこってしまうんでしょうか?
あと、よかったらで良いのですがscanf(" %d", &val)でa(文字)を入力したとしたら暴走しますがこれを回避するために一度文字列として入力して(getsを用いて)それをisdigitを使って判断し、それからungetcを使ってバッファにもどしscanfを使ってとりこんだほうが効率てきなのでしょうか?