C++ for 循环字符串比较逻辑存在缺陷

C++ for-loop string comparison logic is flawed

我的问题是:如何将 first_In_Line 和 last_In_Line 变量传递到 for 循环之外,以便我的最终语句接收变量并正确显示?

我假设学生的名字不同。

// This program allows a user to define class size, between 1 and 25
// students, and give a list of names. It does not store a list 
// of names, but does sort the names to determine alphabetically, 
// which student will be first in line, and who will be last.

#include <iostream>
#include <string>

using namespace std;

int main()
{
// Non-user defined variables
int     num_Students = 0;

string  first_In_Line = "",
        last_In_Line = "",
        previous_Name = "";

bool compare = true;

// User defined variable.
string  next_name;



// Get number of students from user between 1 and 25
cout << "Please enter the number of students in class between 1 and 25: ";
cin >> num_Students;

// Validate user input
while (num_Students < 1 || num_Students > 25)
{
    cout << "Please enter a number between 1 and 25.";
    cin >> num_Students;
}


for (int i = 1; i <= num_Students; i++)
{
    cout << "What is the name of student " << i << "? ";
    cin >> next_name;

    if (compare == true)
    {
        if (next_name < previous_Name)
        {
        first_In_Line = next_name;
        last_In_Line = previous_Name;
        }

        else if (next_name > previous_Name)
        {
        first_In_Line = previous_Name;
        last_In_Line = next_name;
        }
    }
    // Set compare to "true" to execute if statements next 
    // iteration of for-loop
    compare = true;
    previous_Name = next_name;
}

cout << first_In_Line << " is first in line." << endl;
cout << "And " << last_In_Line << " is last in line." << endl;


return 0;
}

输出是这样的,名字不正确:

请在class中输入1到25之间的学生人数:3 学生 1 的名字是什么?亚当 学生 2 的名字是什么?马特 学生 3 的名字是什么? zed 马特排在第一位。 zed 排在最后。

不测试学生是否同名。你应该。 您还想考虑用户只输入 1 名学生的情况。 话虽这么说,当我 运行 它时,你的程序工作得很好。

好的,既然你已经改变了你所说的输出-

if (next_name < previous_Name)
    {
    first_In_Line = next_name;
    last_In_Line = previous_Name;
    }

    else if (next_name > previous_Name)
    {
    first_In_Line = previous_Name;
    last_In_Line = next_name;
    }

您将 next_name 与以前的名称进行比较,而您应该将 next_name 与 first_in_Line 和 last_In_Line 进行比较。实际上,您的程序将始终使用 next_name 和 previous_Name 填充 first_in_Line 和 last_In_Line,即使两者都不应该是第一个或最后一个。

您可以不使用 previous_name,只使用循环遍历您的名字。您只关心排在第一位和最后一位的人。

for (int i = 1; i <= num_Students; i++)
{
    cout << "What is the name of student " << i << "? ";
    cin >> next_name;
    if (i == 1) // initialize your first entry as first and last in line (min, max)
    {
        first_In_Line = next_name;
        last_In_Line = next_name;
    }
    else // compare for last and first in line (min, max) after first iteration of for loop
    {
        if (next_name > last_In_Line)
            last_In_Line = next_name;
        else if (next_name < first_In_Line)
            first_In_Line = next_name;          
    }
}

您的代码存在几个问题。实际上它们与您的问题无关。您不必对 "pass the variables out of the loop" 做任何特殊的事情,只要它们在循环外声明即可(阅读 scope of a variable)。我一直在犹豫要不要给你一个答案,因为如果你自己修复它,你会学到更多。但是,我们开始吧:

  • 读取学生人数的代码重复。你不需要那个。
  • 您不需要变量 compare。无论如何,你必须在每次迭代中进行比较。
  • 您将新名字与姓氏进行比较。为什么?要找到 maximum/minimum,您必须与目前的 minimum/maximum 而不是最后一个值进行比较。

这是修改后的代码。我没有测试它,只是为了确保我没有为你做所有的工作:P

#include <iostream>
#include <string>

int main(int argc, char *argv[]) {
    int     num_Students = 0;
    string  first="aaa",last="xxx",name;

    while (num_Students < 1 || num_Students > 25) {
        cout << "Please enter a number between 1 and 25.";
        cin >> num_Students;
    }

    for (int i = 0; i < num_Students; i++) {
        cout << "What is the name of student " << i+1 << "? ";
        cin >> name;
        if (name < fist){first_In_Line = name;}
        if (name > last){last_In_Line = name;}
    }
    cout << first << " is first in line." << endl;
    cout << "And " << last << " is last in line." << endl;
    return 0;
}