c++ - 将数组作为参数传递给函数

标签 c++

<分区>

在下面的代码中,我想通过数组输入一些记录,最后想比较字符串的记录以生成输出。没有获得比较功能的功能。请帮忙....................

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

int compare(string record[20], int numb1)
{
    system("pause");
    int i;
    string str, substring;
    cout<<"enter the string"<<endl;
    getline(cin, str);
    for (i=0;i<numb1;i++)
    {
        if(str == record[i])
        {
               substring = str.substr(0,2); 
        }
        break;
    }
    if (substring == 'del')
    {
             cout<<"you have a delux type"<<endl;
    }
    else if ('bas' == substring)
    {
         cout<<"you have a basics type"<<endl;
    }
    else
    {
        cout<<"invalid id"<<endl;
    }
    system("pause");
    return 0;
}

int enter_record(int n)
{
    int i;
    string rec[20];
    char choice_1;
    for(i=0;i<n1;i++)
    {
    cout<<"enter value for "<<i+1<<" record"<<endl;
    cin>> rec[i];
    }
    cout <<"\t\t\t FINAL DESIGNED PACKAGES ARE :- "<<endl;
    cout <<"\t\t\t ----------------------------"<<endl;
    for(i=0;i<n;i++)
    {
              cout<<i+1<<". "<<rec[i]<<endl;
    }         
    cout<< "\t\t\tdo you want to compare an string (y/n)"<<endl;
    if ('y' == choice_1)
    {
       compare(rec, n);
    }
    system("pause");
    return 0;
}

int main()
{
    int numb;
    cout<<"\t\t\t WELCOME TO TOUR PACKAGE CHECKING"<<endl;
    cout<<"\t\t\t --------------------------------"<<endl;
    cout<<"enter number of records you want"<<endl;
    cin>>numb;
    enter_record(numb);
    return 0;
}

最佳答案

在 C++ 中,单个刻度用于字符。 del 不是合法字符。所以你需要 "del",而不是 'del'。但实际上,您应该使用比 C 风格数组更智能的东西。

关于c++ - 将数组作为参数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14301873/

相关文章:

c++ - 我怎样才能得到更好的分析?

c++ - 对于带括号的循环中的 return 语句,错误可能会到达非 void 函数的末尾,但在没有括号的循环中则不会

c++ - 我应该在哪里实现这个 "private"辅助函数?

c++ - operator<< 打印一个底层类型为 short 的枚举值是不明确的

c++ - 如何正确链接?

c++ - opencv:检查两个 iplimages 在 c/c++ 中是否相等?

c++ - OpenGL 中的抗锯齿

c++ - 模拟窗口的移动并让它对碰撞使用react

c++ - 具有快速查找功能的对象列表的数据结构

c++ - 如何在现代 OpenGL 中管理模型、 View 和投影矩阵