如何在 C++ 代码中使用 GET 函数
how to use gets function in c++ code?
在此代码的主函数中,在进入字符串程序后开关大小写的大小写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::string
和std::getline
。
相关文章:
- C++中是否有任何函数等同于python中字典的get函数?
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 使用"push_back"作为带有"get"函数C++的向量
- c++ 在类中声明私有的向量,但有一个 get 函数
- C++ :: 设置值函数在函数内设置,但后跟 Get 函数,设置值消失了
- get 函数返回无效值
- 通过引用将私有类实例传递到另一个类 C++ 的"get"函数中
- 如何允许编译器推断出正确的返回类型以进行模板get函数
- 由值持有的成员的get函数应该具有什么类型
- 无法返回带有 get 函数的对象数组
- 返回get函数中的值
- friend get 函数,返回通过可变参数模板递归计算的类型
- 带有get函数的模板类始终返回参考
- 在 xcode 中使用 get 函数时面临问题
- 回到c++,带指针的void函数,如何生成get函数
- 如何在 C++ 代码中使用 GET 函数
- 继承的'Get'函数不会返回正确的值
- 使用 get 函数访问存储在节点中的数据
- C++:将'this'指针与"get"函数一起使用以替换 cout 语句的可能性
- 如何在C++中操作get函数中的数据