如何在 C++ 代码中使用 GET 函数

how to use gets function in c++ code?

本文关键字:GET 函数 代码 C++      更新时间:2023-10-16

在此代码的主函数中,在进入字符串程序后开关大小写的大小写2终止! 代码有什么问题?

/*this code is a implementation of bubble sort algorithm*/
#include <iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<dos.h>
using namespace std;
int counter;
template <class T>//template created
class program//class which holds all the sorting functions
{
    public:
        T *v,x;
        int j,k,l,siz,flag;
        time_t t1,t2;
        char c;
    public:
        void sortlist()//fn to sort characters and numbers
        {
            cout<<endl<<"------->>INTERMEDIATE STEPS<<-------";
            for(k=1;k<=siz-1;k++)//sorting using a bubble sort
            {   flag=0;
                cout<<endl<<"PASS : "<<k<<endl;
                j=0;
                while(j<=siz-1-k)
                {
                    if(v[j]>v[j+1])//comparing two consecutive elements
                    {
                        x=v[j+1];
                        v[j+1]=v[j];
                        v[j]=x;
                        flag++;
                    }
                    for(l=0;l<siz;l++)
                    {
                        cout<<v[l]<<"  ";
                    }
                    cout<<endl;
                    j++;
                }
                cout<<"COMPARISONS:"<<(siz-k)<<endl;
                if(flag==0)
                {
                    cout<<endl<<"----->NO need to carry out more passes"<<endl<<"List is already sorted"<<endl;
                    break;
                }
            }
            }

        void stringsort()//fn to sort the strings
        {
            T a[90][20],b[1][20];
            cout<<"enter the size of list:";
            cin>>siz;
            cout<<"enter the list:";
            cin.ignore();
            for(j=0;j<siz;j++)
            {
                gets(a[j]);
            }
            cout<<endl<<"------->>INTERMEDIATE STEPS<<-------";
            for(k=1;k<=siz-1;k++)//sorting using bubble sort
            {
                flag=0;
                cout<<endl<<"PASS : "<<k<<endl;
                j=0;
                while(j<siz-k)
                {
                    x=strcmp(a[j],a[j+1]);//comparing two consecutive string
                    if(x>0)
                    {
                        strcpy(b[1],a[j+1]);
                        strcpy(a[j+1],a[j]);
                        strcpy(a[j],b[1]);
                        flag++;
                    }
                     for(l=0;l<siz;l++)
                {
                    cout<<a[l]<<"       ";
                }
                cout<<endl;
                    j++;
                }
                cout<<endl<<"COMPARISON:"<<(siz-k)<<endl;
                if(flag==0)
                {
                    cout<<endl<<"No need to carry out more passes"<<endl<<"List is already Sorted"<<endl;
                    break;
                }
            }
            cout<<"SORTED LIST:"<<endl;
            for(j=0;j<siz;j++)
            {
                cout<<endl<<a[j]<<endl;
            }
        }
};
int main()//main fn
{
    int x;
    char c;
    do
    {
        program <char> p1;
        program <int> p2;
        cout<<endl<<"To sort a list of NUMBERS enter -> 1"<<endl<<endl<<"To sort string of CHARACTERS enter -> 2"<<endl<<endl<<"To sort a list OF STRINGS and DOUBLE_STRINGS enter -> 3";
        cout<<endl<<endl<<"Enter either 1 OR 2 OR 3:";
        cin>>x;
        switch(x)
        {
       case 1://to sort list of numbers
            {
                cout<<endl<<"enter the size of list: ";
                cin>>p2.siz;
                cout<<"enter the list: "<<endl;
                p2.v=new int[p2.siz];
                for(p2.l=0;p2.l<=p2.siz-1;p2.l++)
                {
                    cin>>p2.v[p2.l];
                }
                    p2.sortlist();//sort and search in numbers
                cout<<endl<<"SORTED LIST:"<<endl;//sorted list after the bubble sort
                for(x=0;x<=(p2.siz)-1;x++)
                {
                    cout<<p2.v[x]<<endl;
                }
            }
            break;
        case 2://to sort string of character
            {
                    cout<<"enter the string of characters:";
                    cin.ignore()
                    gets(p1.v);
                    p1.siz=strlen(p1.v);
                    p1.sortlist();//sort in characters
                    cout<<endl<<"SORTED STRING:"<<p1.v;
            }
            break;
                case 3://to sort list of strings
                {
                        p1.stringsort();//sort list of string
                }
                break;
            default:
            cout<<"INVALID_CHOICE"<<endl<<endl;
        }
        cout<<endl<<"do u want to enter another list?y/n";
        cin>>c;
    }
    while(c=='y');
        return 0;
}

gets要求您传递一个指向足够存储空间的指针,以保存读取的字符串。程序传递未初始化的指针。

实际上,您不允许对未初始化的值执行任何操作,因此从理论上讲,程序甚至在进入gets函数之前就可能崩溃。

由于用户可以将任意数量的数据传递给gets并且您的程序将负责存储它,因此不推荐使用该函数。自 2011 年以来,它甚至不再存在于C++标准库中,std::gets,尽管::gets可能永远在 POSIX 中可用。简短的回答是,"不要。

您可以考虑std::stringstd::getline