2016-12-06 100 views
-2

我正在製作一個簡單的程序,它將要求輸入,將其放入一個數組(數字[i])並將其轉換爲三個不同的單位。將其轉換爲不同的單位後,我必須能夠顯示用戶提供的最低和最高號碼。顯示來自用戶輸入的數組的最小值和最大值

我使用的是do while循環,從用戶獲取輸入,以及do while loop後,我已經把for loop,將計算從輸入即[I]數組數內的最小和最大數量。但輸入2個輸入後,該程序只顯示了我的最高號碼,最低的號碼顯示我0

下面是它的樣子。

do{ 
clrscr(); 
cout<<"Enter a number:"; 
cin>>number[i]; 
cout<<"\nPlease choose a unit"; 
cout<<"\n<A> meter"; 
cout<<"\n<B> centimeter"; 
cout<<"\n<C> inch"; 
cout<<"\n<D> feet"; 
cout<<"\nPlease enter your chosen unit: "; 
cin>>unit; 
switch(unit) 
{ 
case 'a': 
case 'A': 
i=0; 
m = number[i] * 1; 
cm = 1000 * m; 
in = m/0.0254; 
ft = m/0.3048; 
cout<<"\nYou have chosen meter for unit"; 
cout<<"\nYou have entered "<<number[i]<<" meters"; 
cout<<"\n"<<m<<" meters is equal to\n"<<cm<<" centimeters"; 
cout<<"\n"<<in<<" inches"; 
cout<<"\n"<<ft<<" feet"; 
cout<<"\n\nDo you want to input another value? y/n:"; 
cin>>y; 
if(y=='y') 
{ 
x=1; 
} 
else if(y=='n') 

{ 
x=0; 
} 
break; 

case 'b': 
case 'B': 
cm = number[i] * 1; 
m = cm /1000; 
in = cm * 0.393701; 
ft = cm * 0.0328084; 
cout<<"\nYou have chosen centimeter for unit"; 
cout<<"\nYou have entered "<<number[i]<<" centimeters"; 
cout<<"\n"<<cm<<" centimeters is equal to\n"<<m<<" meters"; 
cout<<"\n"<<in<<" inches"; 
cout<<"\n"<<ft<<" feet"; 
cout<<"\n\nDo you want to input another value? y/n:"; 
cin>>y; 
if(y=='y') 
{ 
x=1; 
} 
else if(y=='n') 
{ 
x=0; 
break; 
} 
break; 
case 'c': 
case 'C': 
in = number[i] * 1; 
m = in * 0.0254; 
cm = in * 2.54; 
ft = in * 0.0833333; 
cout<<"\nYou have chosen inches for unit"; 
cout<<"\nYou have entered "<<number[i]<<" inches"; 
cout<<"\n"<<in<<" inches is equal to\n"<<m<<" meters"; 
cout<<"\n"<<cm<<" centimeters"; 
cout<<"\n"<<ft<<" feet"; 
cout<<"\n\nDo you want to input another value? y/n:"; 
cin>>y; 
if(y=='y') 
{ 
x=1; 
} 
else if(y=='n') 
{ 
x=0; 
} 

break; 
case 'd': 
case 'D': 
ft = number[i] * 1; 
m = ft*0.3048; 
in = ft*12; 
cm = ft * 30.48; 
cout<<"\nYou have chosen feet for unit"; 
cout<<"\nYou have entered "<<number[i]<<" feet"; 
cout<<"\n"<<ft<<" feet is equal to\n"<<m<<" meters"; 
cout<<"\n"<<cm<<" centimeters"; 
cout<<"\n"<<in<<" inches"; 
cout<<"\n\nDo you want to input another value? y/n:"; 
cin>>y; 
if(y=='y') 
{ 
x=1; 
} 
else if(y=='n') 
{ 
x=0; 
} 
break; 
default: 
cout<<"\nYou have entered an invalid choice!"; 
x=1; 
break; 

} 

}while(x==1); 

for(i=0; i<sizeof(number)/sizeof(*number);i++) 
{ 
if(mn>number[i]) 
{ 
    mn=number[i]; 
} 
else if (mx<number[i]) 
    mx= number[i]; 
} 

cout<<"\nHighest number is : "<<mx; 
cout<<"\nLowest number is: "<<mn; 
+0

評論是不適合擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/130032/discussion-on-question-by-james-bong-show-min-and-max-values-of-array-那意志)。 – deceze

回答

1

,如果你得到了最低的數字輸出中有你的第二個cout語句錯誤,你繼續對mx

輸出你目前正在使用mx的最低數量

數最高
cout<<"\nLowest number is: "<<mx; 

固定

cout<<"\nLowest number is: "<<mn; 
+0

我已經運行程序並輸入了2個不同的值,我的第一個輸入是10,然後按y再次輸入我輸入了20.如果我的程序是正確的,最低的數字應該是10,最高的數字應該是20,但是我得到的最低數字是0. –

+0

Plz注意這個問題已經被編輯了,這個答案是在downvoting之前爲問題的先前版本編寫的。 –

1

使用minmax

const auto result = minmax(cbegin(number), cend(number)); 

cout << "\nHighest number is : " << result.first << "\nLowest number is: " result.second << endl; 
1

您比較inumber[i],而是與number大小比較。

相關問題