数组中的C ++菜单和字符串搜索

时间:2014-11-29 02:09:52

标签: c++ arrays sorting search

当我输入此代码并尝试运行它时,当用户选择选项1,输入一些文本和要在其文本中搜索的字符串时,它不起作用。它输出“输入文本”然后立即“输入字符串进行搜索”,而不给用户输入某些文本的机会。有什么问题?

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>

using namespace std;

string s1, text;

int rand(int*);
int Array[100];
void sortArray(int[], int);
void showArray(const int [], int);

 int main()
 {
     while (1)

    // Menu to prompt user choice
     {
         char choice[1];

             cout << endl;
             cout << endl;
             cout << "--MENU--" << endl;
             cout << "1. Pattern Matching" << endl; // search for string within text
             cout << "2. Sorting Techniques" << endl; // generate and then sort 10 random numbers
             cout << "Enter your choice: " << endl;
             cout << endl;
             cin >> choice;
             cout << endl;

         if (choice[0] == '1') // string search option

        {
            cout << "Enter text:" << endl; // accept text from user
            getline (cin, s1);

            cout << "Enter string to search:" << endl; // accept string to search from user
            getline (cin, text);

            int pos = s1.find(text); // finds position where the string is located within text

            if (pos >= 0)
            {
                cout << "Found '" << text << "'" << " at position " << pos + 1 << "." << endl;
            }
            else
            {
                cout << "Did not find text." << endl;
            }

         }

2 个答案:

答案 0 :(得分:2)

这是因为cin >> choice读取当前输入行的一部分以供用户输入。第一个getline()调用紧跟用户输入的选择后读取输入行的剩余部分。在选择之后,您需要忽略输入行的其余部分。

cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

您还需要将#include <limits>添加到代码的开头,以便提取numerical_limits

答案 1 :(得分:0)

看起来您正在为用户响应定义某种char数组。如果选择既不是1也不是2,我倾向于将其设为非零整数类型。还有一些输出格式的快捷方式可以减少代码行。此外,您还希望包含标准字符串类以接受字符串。也许尝试以下内容:

#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>

using namespace std;

string s1, text;

int rand(int*);
int Array[100];
void sortArray(int[], int);
void showArray(const int [], int);

 int main()
 {
     while (1)

    // Menu to prompt user choice    

 {
     int choice;

         cout << "\n--MENU--\n"l;
         cout << "1. Pattern Matching\n"; // search for string within text
         cout << "2. Sorting Techniques\n"; // generate and then sort 10 random numbers
         cout << "Enter your choice:\n";
         cin >> choice+"\n";

if (choice == 1 && choice > 0 && choice != 0) // string search option

    {
        cout << "Enter text:" << endl; // accept text from user
        getline (cin, s1);

        cout << "Enter string to search:" << endl; // accept string to search from user
        getline (cin, text);

        int pos = s1.find(text); // finds position where the string is located within text

        if (pos >= 0)
        {
            cout << "Found '" << text << "'" << " at position " << pos + 1 << ".\n";
        }
        else
        {
            cout << "Did not find text.\n";
        }

     }}}