C++编程问题,求大神帮忙
#include<iostream>#include<cstring>classStock{private:charcompany[30];intshares;doubl...
#include<iostream>
#include<cstring>
class Stock
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot(){ total_val = shares*share_val; }
public:
void acquire(const char*co, int n, double pr);
void buy(int num, double price);
void sell(int mun, double price);
void update(double price);
void show();
};
void Stock::acquire(const char*co, int n, double pr)
{
std::strncpy(company, co, 29);
company[29] = '\0';
if (n < 0)
{
std::cerr << "Number of share can't be negative."
<< company << " share set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
void Stock::buy(int num, double price)
{
if (num < 0)
{
std::cerr << "Number of shares purchased can't be negative"
<< "Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()
{
using std::cout;
using std::endl;
cout << "Company:" << company
<< " shares: " << shares << endl
<< " Share Price:&" << share_val
<< "Total Worth:$" << total_val << endl;
}
int main()
{
using std::cout;
using std::ios_base;
Stock stock1;
stock1.acquire("NanoSmart", 20, 12.50);
cout.setf(ios_base::showpoint);
stock1.show();
stock1.buy(15, 18.25);
stock1.show();
stock1.sell(400, 20.00);
stock1.show();
return 0;
}
这是一个简单的C++程序,我定义了一个类,其他地方都没有问题,就是在编译的时候说strncpy()函数在VS2013中被认为是不安全的,于是我在程序开头加了一行#define _CRT_SECURE_NO_WARNINGS,但还是报错,该怎么办? 展开
#include<cstring>
class Stock
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot(){ total_val = shares*share_val; }
public:
void acquire(const char*co, int n, double pr);
void buy(int num, double price);
void sell(int mun, double price);
void update(double price);
void show();
};
void Stock::acquire(const char*co, int n, double pr)
{
std::strncpy(company, co, 29);
company[29] = '\0';
if (n < 0)
{
std::cerr << "Number of share can't be negative."
<< company << " share set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
void Stock::buy(int num, double price)
{
if (num < 0)
{
std::cerr << "Number of shares purchased can't be negative"
<< "Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()
{
using std::cout;
using std::endl;
cout << "Company:" << company
<< " shares: " << shares << endl
<< " Share Price:&" << share_val
<< "Total Worth:$" << total_val << endl;
}
int main()
{
using std::cout;
using std::ios_base;
Stock stock1;
stock1.acquire("NanoSmart", 20, 12.50);
cout.setf(ios_base::showpoint);
stock1.show();
stock1.buy(15, 18.25);
stock1.show();
stock1.sell(400, 20.00);
stock1.show();
return 0;
}
这是一个简单的C++程序,我定义了一个类,其他地方都没有问题,就是在编译的时候说strncpy()函数在VS2013中被认为是不安全的,于是我在程序开头加了一行#define _CRT_SECURE_NO_WARNINGS,但还是报错,该怎么办? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询