ページ 1 / 1
while文をある文字が入力された時のみ抜け出す
Posted: 2012年12月20日(木) 23:49
by sachi
while文である文字(例えばstopなど)が入力されたらexit(1)でプログラムを終了させるにはどのように書けば良いのかわからなくて困っています。
1文字等の場合はgetch()などでわかるのですが… 初心者的な質問ですみません><;
Re: while文をある文字が入力された時のみ抜け出す
Posted: 2012年12月21日(金) 02:26
by かずま
"stop" は文字というよりも、文字列ですね。
コード:
#include <stdio.h> // scanf, puts, (printf)
#include <stdlib.h> // exit
#include <string.h> // strcmp
int main(void)
{
char word[256];
while (scanf("%255s", word) == 1) {
if (strcmp(word, "stop") == 0) exit(1);
puts(word); // printf("%s\n", word);
}
return 0;
}