0

ユーザーに0から1000000までの数字を入力するように求めるプログラムを作成しようとしており、特定の数字の発生を出力します(ユーザーも入力しました)

私はこのプログラムを書きましたが、うまくいくと思いますが、while 式が真でない場合、特定のメッセージを出力したいのですが、どこに配置すればよいかわかりません。

これが私のプログラムです:

#include <iostream> 
using namespace std;
int main()
{ 
 int n,j=0,key; 
 cout << "Pleaser enter digits\n";
 cin >> n;
 cout << "please enter key number\n";
 cin >> key;

 while (n>0 && n<1000000)
 {
   if(n%10==key)j++; 
      n= n/10;
 }

 cout << "The number " << key << " was found " << j << " time(s)" << endl;
 return 0;  
}

前もって感謝します!

4

3 に答える 3

2

使用する

if(n>0 && n<1000000)
{
    while(n)
    {
       if(n%10==key)
       j++; 
       n= n/10;
    } 
}
else 
cout<<"n is supposed to be between 0 and 1000000";
于 2014-10-06T11:24:08.903 に答える