【问题标题】:C++ for-loop string comparison logic is flawedC++ for-loop 字符串比较逻辑有缺陷
【发布时间】:2015-09-22 16:33:19
【问题描述】:

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

我假设学生的名字不同。

// 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;
}

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

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

【问题讨论】:

  • 呃,你提到了一个问题......它是什么?
  • 好点...将编辑
  • 您的变量是在 for 循环之外声明的,它们在循环之后仍然存在,您不必“传递”它们。你是如何使用你的程序的? (但是,是的,您的逻辑是错误的:您永远不会将新名称与您的 first_In_Linelast_In_Line 进行比较。)
  • 好的,我来改一下
  • 我正在为教育目的而上课的课程中使用该程序。

标签: c++ variables for-loop logic string-comparison


【解决方案1】:

你可以去掉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;          
    }
}

【讨论】:

    【解决方案2】:

    您不测试学生是否同名。你应该。 您还想考虑用户仅输入 1 个学生的情况。 话虽如此,你的程序在我运行时运行良好。

    【讨论】:

    • 我假设学生的名字不同。
    • 输入 1 个学生时,他的名字与“”进行比较
    • 它适用于某些情况,但不是全部。试试亚当,夏娃,佐伊。我得到了第一名
    • 这就是代码的作用。他应该改为针对 first_In_Line 进行测试。
    【解决方案3】:

    好的,现在你已经改变了你所说的输出-

    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 与 first_in_Line 和 last_In_Line 进行比较时,您正在将 next_name 与以前的名称进行比较。事实上,您的程序将始终使用 next_name 和 previous_Name 填充 first_in_Line 和 last_In_Line,即使它们都不应该是第一个或最后一个。

    【讨论】:

      【解决方案4】:

      您的代码存在几个问题。实际上,它们与您的问题无关。你不必做任何特殊的事情来“将变量传递出循环”,只要它们是在循环之外声明的(阅读scope of a variable)。我犹豫要不要给你写一个答案,因为如果你自己解决它,你会学到更多。但是,我们开始:

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

      这是修改后的代码。我没有测试它,只是为了确保我没有为你做所有的工作: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;
      }
      

      【讨论】:

      • 这根本行不通。你永远不会改变第一个或最后一个,而且你首先让它们倒退 - “aaa”应该是最后一个(这样它总是会被第一个输入的学生替换)并且“xxx”应该是第一个,所以它也将永远被第一个学生取代。除非她叫佐伊或塞尔达。或者 Aa 先生,因为虽然不太可能,但“aaa”仍在有效输入的约束范围内。
      • @DarkSquirtings 我提到代码没有经过测试。你是对的:这不会编译,但那些只是拼写错误,应该很容易修复
      猜你喜欢
      • 2015-07-06
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      相关资源
      最近更新 更多