已排序和搜索的数组没有输出

发布于 2024-12-20 05:06:22 字数 1852 浏览 2 评论 0原文

我一直在学习编程第一个学期所学到的一切。我即将完成期末考试,因此我一直在尝试结合我所学到的准备内容来编写示例程序。下面的程序应该从文件中读取姓名,通过冒泡搜索对它们进行排序,然后提示用户输入一个姓名,二分搜索将查找该姓名并告诉您该人是否是朋友。

我的问题是,当我输入名称时,系统只会提示我再次输入该名称。没有输出。

请记住,这里的所有内容大部分都是我迄今为止所学到的(所以我不知道如何使用向量、指针等)。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void bubblesort(string[], const int);
void search(string[], const int);
int sub = 0;

int main()
{
 const int maxsize = 100;
 string friendArray[maxsize];

 ifstream friends;
 friends.open("myFriends.dat");

 while (sub < maxsize && getline(friends, friendArray[sub]))
   sub++;


 bubblesort(friendArray, sub);
 search(friendArray, maxsize);


 system("pause");
 return 0;
}



void bubblesort(string *array, const int size)
{
    bool swap;
    string temp;

    do
    {
        swap = false;
        for (int count = 1; count < (size - 1); count++)
        {
            if(array[count-1] >array[count])
            {
                temp = array[count-1];
                array[count-1] = array[count];
                array[count] = temp;
                swap = true;
            }
        }
    }
    while(swap);

}

void search(string *array, int size)
{
    int first = 0;
    int last = size - 1;
    int middle;
    string name;
    bool friends = false;

    do
    {
     cout<<"Please enter a name or END to terminate:";
     cin>>name;
    }
    while(!friends && first <= last && name != "END");
    {
        middle = (first + last) / 2;
        if (array[middle] == name)
            {
                friends = true;
                cout<<array[middle]<<" is my friend."<<endl;
            }
        else if (array[middle] > name)
            last = middle - 1;
        else
            last = middle + 1;
    }
 }

I've been studying everything I've gone over in my first semester of programming. I have a final coming up so I've been trying to write sample programs combining everything I've learned to prepare. The program below is supposed to read in names from a file, sort them via bubble search, and then prompt the user to enter a name, which the binary search will look for and tell you if the person is a friend or not.

My problem is, when I type a name, I am only prompted to type the name again. There is no output.

Please keep in mind that everything in here is mostly what I've learned so far (so I do not know how to use vectors, pointers, etc).

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void bubblesort(string[], const int);
void search(string[], const int);
int sub = 0;

int main()
{
 const int maxsize = 100;
 string friendArray[maxsize];

 ifstream friends;
 friends.open("myFriends.dat");

 while (sub < maxsize && getline(friends, friendArray[sub]))
   sub++;


 bubblesort(friendArray, sub);
 search(friendArray, maxsize);


 system("pause");
 return 0;
}



void bubblesort(string *array, const int size)
{
    bool swap;
    string temp;

    do
    {
        swap = false;
        for (int count = 1; count < (size - 1); count++)
        {
            if(array[count-1] >array[count])
            {
                temp = array[count-1];
                array[count-1] = array[count];
                array[count] = temp;
                swap = true;
            }
        }
    }
    while(swap);

}

void search(string *array, int size)
{
    int first = 0;
    int last = size - 1;
    int middle;
    string name;
    bool friends = false;

    do
    {
     cout<<"Please enter a name or END to terminate:";
     cin>>name;
    }
    while(!friends && first <= last && name != "END");
    {
        middle = (first + last) / 2;
        if (array[middle] == name)
            {
                friends = true;
                cout<<array[middle]<<" is my friend."<<endl;
            }
        else if (array[middle] > name)
            last = middle - 1;
        else
            last = middle + 1;
    }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2024-12-27 05:06:22

我不想透露太多,因为这是家庭作业,所以我将移动您的一些代码,使其尽可能接近,并向您展示为什么它目前不起作用。

目前您可能认为 do...while 循环是某种双块,但事实并非如此。代码中 while(...); 之后的代码只会执行一次,在跳出 do...while 循环后,它就不再执行了。方式连接。您将需要两个循环,一个外部循环提示输入名称,另一个内部循环在列表中查找该名称。

在要求用户输入另一个名字后,您也不会重置 friendslast。一个简单的解决方法是将声明(包含初始化)移动到第一个循环内。

这就是您的代码在重新排列并应用上述更改后的样子:

void search(string *array, int size)
{
   string name;

   cout<<"Please enter a name or END to terminate:";
   cin>>name;

   while (name != "END")
   {
      bool friends = false;
      int first = 0;
      int last = size - 1;
      int middle;

      while(!friends && first <= last)
      {
         middle = (first + last) / 2;
         if (array[middle] == name)
         {
             friends = true;
             cout<<array[middle]<<" is my friend."<<endl;
         }
         else if (array[middle] > name)
            last = middle - 1;
         else
            last = middle + 1;
      }

      cout<<"Please enter another name or END to terminate:";
      cin>>name;
   }      
}

这次有两个不同的提示,因此如果用户输入 "END",外部循环会立即终止,而不是必须在循环内添加额外的检查。

另外,与您的其他问题一样, search(friendArray, maxsize); 应该是 search(friendArray, sub);,原因是我上次告诉您的 - sub 是数组中有效项的计数,maxsize 是数组的容量。

注意:如果列表中不存在该名称,则会导致无限循环。我会让你解决这个问题,因为这是作业,我不想改变你的任何实际逻辑。不过,一个提示是考虑一下实际发生的情况 - 如果某个值不存在,您只需在该值应该存在的区域周围不断递增和递减 last 即可。

也许如果你的逻辑在某处修改了 first ,那么条件 first <= last 将失败并且你会跳出循环......

I don't want to give too much away, since it's homework, so I'll move some of your code around, keeping it as close as possible, and show you why it currently won't work.

At the moment you're kind of thinking the do...while loop is some sort of double block, it's not. The code after the while(...); in your code will only be executed once, after you break out of the do...while loop, it's in no way connected. You're going to need two loops, an outer one that prompts for names, and an inner one that looks for that name in your list.

You're also not resetting friends and last after asking the user to enter another name. An easy fix is to move your declarations (which contain initialisations) inside the first loop.

This is what your code will look like after mostly rearranging it and apply the above changes:

void search(string *array, int size)
{
   string name;

   cout<<"Please enter a name or END to terminate:";
   cin>>name;

   while (name != "END")
   {
      bool friends = false;
      int first = 0;
      int last = size - 1;
      int middle;

      while(!friends && first <= last)
      {
         middle = (first + last) / 2;
         if (array[middle] == name)
         {
             friends = true;
             cout<<array[middle]<<" is my friend."<<endl;
         }
         else if (array[middle] > name)
            last = middle - 1;
         else
            last = middle + 1;
      }

      cout<<"Please enter another name or END to terminate:";
      cin>>name;
   }      
}

There's two different prompts this time, so that if the user enters "END", the outside loop terminates immediately, rather than having to add an extra check inside the loop.

Also, as with your other question, search(friendArray, maxsize); should be search(friendArray, sub);, for the reason I told you last time - sub is a count of valid items in the array, maxsize is the capacity of the array.

NOTE: If the name doesn't exist in your list, it'll cause an infinite loop. I'll let you work that out since it's homework and I don't want to change any of your actual logic. A hint though is to think about what's actually happening - if a value doesn't exist you'll just keep incrementing and decrementing last around the area where the value should be if it existed.

Perhaps if your logic incorporated first being modified somewhere, so that the condition first <= last would fail and you'd break out of the loop...

娇俏 2024-12-27 05:06:22

你的 do while 语句是错误的,它运行这个顺序:

do
    {
     cout<<"Please enter a name or END to terminate:";
     cin>>name;
    }
    while(!friends && first <= last && name != "END");

然后这个块:

{
    middle = (first + last) / 2;
    if (array[middle] == name)
        {
            friends = true;
            cout<<array[middle]<<" is my friend."<<endl;
        }
    else if (array[middle] > name)
        last = middle - 1;
    else
        last = middle + 1;
}

修改它:

do
{
    cout<<"Please enter a name or END to terminate:";
    cin>>name;
    first = 0;
    last = size - 1;
    middle=0;
    friends = false;
    while(!friends && first <= last && name != "END");
    {
        middle = (first + last) / 2;
        if (array[middle] == name)
            {
                friends = true;
                cout<<array[middle]<<" is my friend."<<endl;
            }
        else if (array[middle] > name)
            last = middle - 1;
        else
            last = middle + 1;
    }

}
while(name != "END");

Your do while statement is wrong, it run this order:

do
    {
     cout<<"Please enter a name or END to terminate:";
     cin>>name;
    }
    while(!friends && first <= last && name != "END");

And then this block:

{
    middle = (first + last) / 2;
    if (array[middle] == name)
        {
            friends = true;
            cout<<array[middle]<<" is my friend."<<endl;
        }
    else if (array[middle] > name)
        last = middle - 1;
    else
        last = middle + 1;
}

Modify it:

do
{
    cout<<"Please enter a name or END to terminate:";
    cin>>name;
    first = 0;
    last = size - 1;
    middle=0;
    friends = false;
    while(!friends && first <= last && name != "END");
    {
        middle = (first + last) / 2;
        if (array[middle] == name)
            {
                friends = true;
                cout<<array[middle]<<" is my friend."<<endl;
            }
        else if (array[middle] > name)
            last = middle - 1;
        else
            last = middle + 1;
    }

}
while(name != "END");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文