2016-11-24 7 views
0

私はCでメニュー入力プログラムを作成しています。ここでユーザー入力を求めてから、プログラムの入力の最初の文字のみを使用しなければなりません。以下のコードに加えて、私はまた、を試してみました。これは、とりわけ、の代わりに、whileループの代わりに使用しました。Cでgetcharを使用して1文字を読み取る方法は?

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

void print_menu(); 
void uppercase(char *string); 

int main() 
{ 
    char i = 0, c = 0; 
    char selection[0]; 

    print_menu(); 

     while((c = getchar()) != EOF) 
     { 
      selection[i++] = c; 
      selection[i] = '\0'; //insert NULL at end of string 
      uppercase(selection); 

      switch(selection[i]) 
      { 
       case 'c': 
       uppercase(selection); 
       printf("%s\n", selection); 
       print_menu(); 
       break; 

       case 'X': 
       printf("The program is exiting, Schuss!\n"); 
       exit(0); 

       default: 
       printf("\nYou entered: %s\n", selection); 
       print_menu(); 
       break; 
      } 

     } 
return 0; 
} 

void print_menu() //gives code for menu 
{ 
    printf("Select a menu item and hit enter: \n"); 
    printf("C) Convert the string to uppercase\n"); 
    printf("M) Display this menu\n"); 
    printf("X) Exit the program\n"); 
} 
void uppercase(char *string) 
{ 
    int c = 0; 

    while (string[c] != '\0') 
    { 
    if (string[c] >= 'a' && string[c] <= 'z') 
    { 
     string[c] = string[c] - 32; 
    } 
    c++; 
    } 

} 

プログラムを実行しているとき、私はYes!を入力すると、私は出力が You entered: Yし、メニューのプリントであることを期待しています。 現在、出力が、私はそれがwhileループに問題がありますかなり確信して

You entered: Y 
Select a menu item and hit enter: 
C) Convert the string to uppercase 
M) Display this menu 
X) Exit the program 

You entered: YE 
Select a menu item and hit enter: 
C) Convert the string to uppercase 
M) Display this menu 
X) Exit the program 

You entered: S 
Select a menu item and hit enter: 
C) Convert the string to uppercase 
M) Display this menu 
X) Exit the program 

You entered: S! 
Select a menu item and hit enter: 
C) Convert the string to uppercase 
M) Display this menu 
X) Exit the program 

You entered: 

Select a menu item and hit enter: 
C) Convert the string to uppercase 
M) Display this menu 
X) Exit the program 
^C 

である、しかしそれを修正する方法を考え出したていません。最終的には、より多くのメニュー項目とケースがあります。たとえば、ユーザはAを入力し、aを印刷し、ユーザが終了するために「X」を入力するまで次のメニュー選択を待つためにメニューを再度印刷します。また、私はuppercaseの2つの関数呼び出しがあり、この時点で意図的であることを知っています。

編集:私はgetchar()に1文字を読みたいと思っています。その後、その文字の大文字と小文字を区別します。次に、メニューをもう一度印刷します(この部分はそれぞれのcase文に含まれています)。次に、ステップ1(「1文字を読む」)から繰り返します。

私は、whileループの外getchar()を入れて、一つだけの文字を読みになるんどの代わりwhile(1)にループを設定するための私のコードを変更するだけでなく、You entered: Bを印刷する無限ループやメニューを作成しました。それは進歩です。

+2

'char selection [0];'が間違っています。 switch(selection [i]) ' - >' switch(* selection) ' – BLUEPIXY

+0

私はさまざまなサイズと同じ結果を試しました。私は配列のサイズを制限することが効果的だろうと思ったので、他のサイズが機能しなかったのでそのまま残しました。 –

+1

ユーザーが入力した行全体を読み込み、最初の文字のみを使用したいとします。 'fgets'は入力行を読み込むのに適しています。読みやすさと理解を容易にするために –

答えて

1

getcharでCの1文字を読み取るにはどうすればよいですか?

getcharを使用して1文字のみを読むことができます。

ユーザーの入力を求めてから、プログラムの入力の最初の文字のみを使用する必要があります。

行全体を読み取り、その行の最初の文字のみを使用すると、これを行うことができます。

char line[100]; // Make it large enough. 
while(fgets(line, 100, stdin) != NULL) 
{ 
    uppercase(line); 
    char selection = line[0]; 

    switch (selection) 
    { 
     ... 
    } 
} 
+0

@Rhymoidこの方法は、 'toupper'関数を使うことができればうまくいくでしょう。私は自分自身の '大文字'関数を使う必要があり、それは私に "互換性のない型"のエラーを与えます。 –

+0

@Rhymoid、おっと! –

+0

@InertialOberserverなぜあなたはtoupperを使用できませんでしたか?本当に不可能な場合は、char型(ASCIIテーブル参照)を使用すると、toupper関数を簡単に書くことができます。 – Fefux

0

Cでgetcharを使用して1文字を読み取るにはどうすればよいですか?ただ、このように

getchar(); 

ところで、あなたはここで、いくつかのメモリを上書きしています

char i = 0, c = 0; 
char selection[0]; // What?! An array of 0 elements?! 

... 

    selection[i++] = c; // Where are you storing the value? 
    selection[i] = '\0'; // Same here 

そして、いくつかのwhileループの後、あなたのi変数が値を持つことになりますのでご注意を12? 34?あなたがそれを行うたびにselection[i++] = cを実行する前に0に初期化しないので、1234?

+0

'[0]'は配列の最初の要素の位置なので、 'array [0]'は実際には1の配列ですか?私はまだ配列の仕組みを扱っているので、間違っている可能性があります。また、それは私がその設定で達成しようとしていたものだけを格納することに制限されていませんが、入力の値を格納するようです。 –

+0

'array [0]'はプレースホルダーになり、配列の実際の領域はありません – user3629249

0

次のコードには未定義の動作がなく、最大128文字まで入力できますが、OPコードには何もありません。また、最初の(最大)4文字を使用するコードは

#include <stdio.h> // getchar(), prinf() 
//#include <string.h> 
//#include <stdlib.h> // exit(), EXIT_SUCCESS 
#include <ctype.h> // toupper() 

void print_menu(void); 
//void uppercase(char *string); 

#define MAX_CHARS (128) 

int main(void) 
{ 
    size_t i = 0; 
    int c; // getchar() returns an int, not a char 
    //char selection[0]; 
    char selection[ MAX_CHARS+1 ]; // +1 to allow for NUL terminator byte 

    print_menu(); 

    while(i < MAX_CHARS && (c = getchar()) != EOF && 'X' != toupper(c)) 
    { 
     if('\n' == c) // ignore any newlines 
      continue; 

     selection[i++] = (char)toupper(c); 
     selection[i] = '\0'; //insert NUL byte at end of string 
     //uppercase(selection); 

     switch(selection[i-1]) // -1 because 'i' is the termination byte 
     { 
      //case 'c': 
      // //uppercase(selection); 
      // printf("%s\n", selection); 
      // print_menu(); 
      // break; 

      //case 'X': 
      // printf("The program is exiting, Schuss!\n"); 
      // //exit(0); 
      // exit(EXIT_SUCCESS); 
      // break; 

      default: 
       printf("\nYou entered: %s\n", selection); 
       print_menu(); 
       break; 
     } 
    } 

    printf("The program is exiting, Schuss!\n"); 
    //return 0; // not needed for 'main()' in modern C 
} // end function: main 


void print_menu() //gives code for menu 
{ 
    // note: C concatenates successive literal strings 
    printf("Select a menu item and hit enter: \n" 
      "X) Exit the program\n" 
      "any other key appends to input, except newline\n"); 
} // end function: print_menu 


//void uppercase(char *string) 
//{ 
// int c = 0; 

// while (string[c] != '\0') 
// { 
// if (string[c] >= 'a' && string[c] <= 'z') 
// { 
//  string[c] = string[c] - 32; 
//  } 
//  c++; 
// } 
//} 
関連する問題