Visual C++ not identifying gets_s
Visual C++ not identifying gets_s
所以这是一个简单的练习程序。
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "stdio.h"
using namespace std;
class book
{
int bookno;
char bookt[20];
float price;
float totalcost(int n)
{
float tot;
tot = n * price;
return tot;
}
public:
void input()
{
cout << "nEnter book number: ";
cin >> bookno;
cout << "nEnter book title: ";
gets_s(bookt); //Does not identify this.
cout << "nEnter book price: ";
cin >> price;
}
void purchase()
{
int n;
float total;
cout << "nEnter the number of books to be purchase: ";
cin >> n;
total = totalcost(n);
cout << "nTotal amount is: " << total;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
book B1;
B1.input();
B1.purchase();
_getch();
return 0;
}
编译器(Visual C++2010)无法识别gets_s
。从这个意义上说,它只是跳过输出中的输入字段,如下所示:
OUTPUT
Enter book number: 5
Enter book title:
Enter book price: 5
Enter the number of books to be purchased: 5
Total amount is: 25
它只是没有给我时间输入书名,同时运行书名和书价。帮助
问题是,当您这样做时,不会从输入流中提取新行字符:
cin >> bookno;
最快的方法是在cin.get()之后(在input()方法中)插入一个额外的cin.get
cout << "nEnter book number: ";
cin >> bookno;
cin.get();
cout << "nEnter book title: ";
gets_s(bookt); // Now it will identify this.
cout << "nEnter book price: ";
cin >> price;
另一个建议是尽量避免混合使用C和C++函数。
相关文章:
- Turbo C++ cin() 不能与 gets() 一起使用
- 一起使用 GETS 和 CIN
- 在C/C++中检查数字是否为整数(不带scanf/gets/etc)
- gets() 二维数组
- gets() 不允许先执行 cout
- 如何解决"identifier 'gets'/'clrscr' is undefined"问题?
- gets()未在作用域中声明
- QJsonValue gets empty
- Identifying amd gpus
- CPP中如何使用get gets()
- gets() 和 getch() 有什么区别?
- gets() 在第一次循环迭代中不起作用,但在后续迭代中工作
- 为什么gets()需要更多的字符
- gets() 不接受输入
- 函数中的 gets() 在第二次使用后被跳过
- Visual C++ not identifying gets_s
- Gets()导致内存损坏
- 为什么第一个gets()在这段代码中不起作用?
- dev 为什么在 dev c++ 中使用 "cin function" 后我无法使用"gets function"?
- 它的gets()被认为是C函数或c++函数