错误访问冲突写入位置0x00229C20.尝试在控制台中输入字符串时
error access violation writing location 0x00229C20. when trying to input a string in console
我试图在c++代码中输入字符串,当我运行时,我总是得到以下错误:在assignment-1.exe中的0x0F5023F5 (msvcp140 .dll)抛出异常:0xC0000005:访问违规写入位置0x00229C20。我会张贴我的代码下面,如果有人能帮助我,那将是伟大的。请注意,我已经知道,这是一个问题,我试图访问的内存位置上,你没有访问权限,我只是不知道如何解决它。
HEADER FILE:
#ifndef item_H
#define item_h
class item
{
private:
//attributes
int itemID;
char itemName[20];
float itemcost;
float itemprice;
//utility function
float calcPrice();
public:
//constructor
item(int = 000, char[] = "itemUnknown", float = 0,float = 0);
//destructor
~item();
//set functions
void setAll(int, char[], float, float);
void setID(int);
void setName(char[]);
void setCost(float);
//get function
int getID();
float getcost();
float getprice();
void getname();
//print function
void print();
};
#endif
CPP:
#include "Dariush.h"
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
//constructor will set attributes
item::item(int ID, char n[] , float c,float p)
{
setID(ID);
setName(n);
setCost(c);
setAll(ID, n, c, p);
}
//destructor will print destroing two objects
item::~item()
{
cout << "destroing two objects : " << " " << itemName << " "
<< " & " << itemName << endl;
}
//set functions :
void item::setID(int ID)
{
cout << "please enter the item's ID : " << endl;
cin >> ID;
}
void item::setName(char n[])
{
cout << "please enter the item's name" << endl;
cin.ignore();
cin.getline(n, 20);
}
void item::setCost(float c)
{
cout << "please enter the item's cost : " << endl;
cin >> c;
}
void item::setAll(int ID, char n[], float c, float p)
{
itemID = (ID > 0 && ID < 999) ? ID : 0;
strcpy_s(itemName, n);
itemcost = (c > 0) ? c : 0;
calcPrice();
}
//get functions :
int item::getID()
{
return itemID;
}
float item::getcost()
{
return itemcost;
}
float item::getprice()
{
return itemprice;
}
void item::getname()
{
cout << itemName << endl;
}
//print function :
void item::print()
{
cout << "ID : " << itemID << endl
<< "Name : " << itemName << endl
<< "cost : " << itemcost << endl
<< "price : " << itemprice << endl;
}
// utility function for price callculation :
float item::calcPrice()
{
if (itemcost < 1000)
{
itemprice = itemcost + (itemcost*0.1);
}
else
itemprice = itemcost + (itemcost*0.2);
return itemprice;
}
MAIN.CPP:
#include "Dariush.h"
#include <iostream>
#include<string>
using namespace std ;
void main()
{
item i1;
item i2;
i1.print();
i2.print();
}
感谢您的协助
让我们仔细看看这三个函数声明:
item(int = 000, char[] = "itemUnknown", float = 0,float = 0);
void setAll(int, char[], float, float);
void setName(char[]);
这里的问题是,您声明的字符"array"参数根本不是真正的数组。它们是指针。当声明参数时,例如char n[]
实际上被编译器翻译为char *n
。
构造函数声明使指针指向常量字符串字面值""
。常量字符串字面值的重要之处在于它们确实是常量。试图修改字符串字面值会导致未定义行为。并且改变这个文字是您试图在setName
函数中使用cin.getline(n, 20)
调用所做的。不仅如此,您还告诉cin.getline
函数读取超出字符串字面量的内容。
简单的解决方案是将setName
读入成员变量itemName
。
这段代码有很多问题,但是导致访问冲突的一个是:
void item::setName(char n[])
{
cout << "please enter the item's name" << endl;
cin.ignore();
cin.getline(n, 20); //here
}
你应该用cin.getline(itemName, 20);
代替。
同样,为了防止将来出现这样的错误,将参数声明为char const n[]
而不是char n[]
——当您使用带有非const指针的字符串字面量作为参数时,好的编译器应该显示警告。
相关文章:
- 如何防止父进程在孩子死后丢失控制台输入?
- 如何使用字符串获取用户控制台输入
- 如何读取控制台输入并将其存储到向量字符串中
- 如何检测控制台输入不是C++中的数字
- 关闭控制台输入C
- 验证 C++ 中的控制台输入
- 如何将文件输入到C++并比较控制台输入
- 如何获取类似 QIODevice 的控制台输入信号 (stdin)
- C++中的GTK:基于控制台输入更改标签
- 如何在C++中调用具有参数和用户控制台输入的函数
- 用C++读取一个很长的控制台输入
- Seg 错误初始化列表向量与控制台输入
- C++控制台应用呈现其他 CMD 窗口不接受控制台输入
- 异步控制台输入冻结了以前的输入 C
- 控制台输入,直到.然后输入
- C++阻止5个字符后的控制台输入
- 在代码中定义字符串与控制台输入之间的区别
- 如何将控制台输入写入等待ReadConsoleInput的子应用程序
- 在代码块中,您在哪里键入控制台输入
- 将文件中的数字与控制台输入进行比较