2016-11-14 9 views
-3

私はこの単純なプログラムを書いています。これはユーザーが入力した数値の階乗を計算することになっています。プログラムは、新しい番号の階乗を見つけるために、プログラムを停止または続行するようユーザーに尋ねる必要があります。Do while loop with Character input

ほとんどの場合、ユーザーはCapsLockに注意を払わないため、プログラムははいの答えとしてYまたはyを受け入れる必要があります。しかし、私がこのプログラムを実行するたびに、私がY/Yに入っても、それは終了します!

私はGoogleで検索して、問題が原因new line文字が私の文字入力を受け入れてもらうには可能性が出てたので、私は改行文字に対応するためにscanf("%c ", &choice);scanf("%c", &choice);からscanfのコードを変更しますが、私のプログラムはまだ終了しつつありますY/yを入力として受け入れた後

ここにコードがあります。可能であれば、これらの問題に対処するためのベストプラクティスと方法を必要な修正とともにお知らせください。

#include<stdio.h> 
#include"Disablewarning.h" // header file to disable s_secure warning in visual studio contains #pragma warning (disable : 4996) 

void main() { 
    int factorial=1;//Stores the factorial value 
    int i; //Counter 
    char choice;//stores user choice to continue or terminte the program 

     do {//Makes sure the loop isn't terminated until the user decides 
      do{ 
       printf("Enter the no whose factorial you want to calculate:\t"); 
       scanf("%d", &i); 
      } while (i<0); 

     if (i == 0) //calculates 0! 
      factorial = 1; 
     else {//Calculates factorial for No greater than 1; 
      while (i > 0) { 
       factorial = factorial*i; 
       i--; 
      } 
     } 

     printf("\nThe factorialof entered no is :\t%d", factorial);//prints the final result 

     printf("\nDo you want to continue (Y/N)?"); 
     scanf("%c ", &choice); 

    } while (choice =="y" || choice =="Y"); // Checks if user wants to continue 

} 
+4

'として間違った答えを与える再計算} while(choice == "y" || –

+1

'' Y ''は' char'へのポインタですが、 '' Y' 'は '' char''へのポインタです。 ''は単なる 'char'です。 – ForceBru

+0

@Biffenオススメです。私はタグを削除する必要がありますか? –

答えて

3

:それは改行や他のスペースを無視するために%cの前にスペースを追加

} while (choice == 'y' || choice == 'Y'); // Checks if user wants to continue 

注: 簡単な見積もり'は文字と二重引用符に使用されます"は文字列に使用されます

+0

ありがとうございました!これは期待どおりに働いていました:)なぜ私はこれが以前のものではなく、 "%c" 'ではないことを教えてください。また、この動作についてもっと読むことができる記事/リンクを教えてください。可能であれば、この種の状況に対処するための最良のテクニックと方法を教えてください。もう一度ありがとうございます。 –

+1

その場合、単純な 'printf()'を使用して 'scanf 'で入力値を確認することができます。 – developer

2

あなたの最後から2番目の行は、文字リテラルすなわち'y'あるべきリテラル"y"文字列を、持っているVisual Studioで、私はプログラミング初心者だと私はこのコードを実行しています:

} while (choice =="y" || choice =="Y"); 

これは次のようになります。

} while (choice =='y' || choice =='Y'); 

また、scanf()は空白を消費しません。

printf("\nDo you want to continue (Y/N)? "); 
scanf(" %c", &choice); //You should add the space before %c, not after 

あなたが使用する必要があります:ちょうど、次のようなあなたのscanfを変更

scanf(" %c", &choice); 
+0

@George答えはまだ真です。 – Biffen

+0

上記を参照してください私は私のために働いています変更%cの後にスペースを追加しました scanf( "%c"、&choice); – pravakar

0

いくつかのバグがコードにまだ存在していても、補正後、次のコードで
あなたは「Y」を入力する場合はやってみて、階乗それが

int factorial is already loaded with the previous value

#include "stdafx.h" 
#include <stdio.h> 
#include <iostream> 

using namespace System; 
using namespace std; 

int calculateFactorial(int i); 

int main() 
{ 
    int i; 
    char choice; 

    do{ 
     printf("Enter the no whose factorial you want to calculate:\t"); 
     scanf("%d", &i); 
     printf("\n The factorial of entered no is :\t %d", calculateFactorial(i)); 
     printf("\n Do you want to continue (Y/N)?"); 
     scanf(" %c", &choice); 
    } while (choice == 'y' || choice == 'Y'); 
    return 0; 
} 

int calculateFactorial(int i) { 
    int factorial = 1; 
    if (i == 0){ 
     factorial = 1; 
    }else { 
     while (i > 0){ 
      factorial = factorial*i; 
      i--; 
     } 
    } 
    return factorial; 
} 
+0

"私はプログラミングの初心者です。私はVisual Studio 2015でこのコードを実行しています "関数呼び出し部はまだありません。しかし、ありがとう。 –

+1

問題はありません。あなたが初心者のときに関数を使用したくない場合は、do {factorial = 1;}で変数int factorialをリセットします。 } while(---) –

+0

Yaa !!ループが予想どおりに動作するようになったらすぐに修正を加えました!! :) –