我想創建一個程序,提示用戶輸入班級中的學生人數,然後循環讀取那麼多姓名。一旦所有的名字都被讀入,報告哪個學生會在該行的前面,哪個學生會在該行的末尾。你可以假設沒有兩個學生有相同的名字。按字母順序顯示姓名
但是我在我的輸出程序不顯示字符串的右比較...
以下是我的輸出。 ...請幫幫我。
Enter the number of students(1 - 25): 5
Enter the name: ali
Enter the name: hamxa
Enter the name: hassan
Enter the name: anjum
Enter the name: java
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠java ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠java
Press any key to continue . . .
以下是我的代碼
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
const int size = 10;
char name[size], largest[size], smallest[size];
double num;
do
{
cout << "Enter the number of students(1 - 25): ";
cin >> num;
} while (num < 1 || num > 25);
cin.ignore();
for (int i = 0; i < num; i++)
{
cout << "Enter the name: ";
cin.getline(name, size);
if (strcmp(name, largest) > 0)
{
largest[size] = name[size];
}
if (strcmp(name, smallest) < 0)
{
smallest[size] = name[size];
}
}
cout << largest << " " << smallest << endl;
return 0;
}
這是C僞裝成C++。它也看起來像功課似的懷疑。我已經適當修改了你的問題標籤,直到代碼改變爲反映C++。 – Casey
@Casey這不是C語言,不會用C編譯器編譯,也不是跟隨C標籤的人可能有興趣回答這個問題。我會回滾你的不正確的編輯。 – Lundin
@Casey這是C++而不是C,這是事實。請堅持事實。這可能是寫得不好的C++,但是這仍然是C++。 –