c++ - “An access violation (Segmentation Fault) raised in your program.”

标签 c++ crash access-violation dev-c++

我是编程新手,我遇到了一个我从未听说过的错误,如果我按下 A1 之后的任何东西,它就会崩溃 我收到错误消息“您的程序中引发了访问冲突(段错误)。” 我几乎完成了这个项目 这阻止了我 为什么我可以输入A1但不能输入A2等等

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;

//function prototype
void fill_array();//fill the array using data from the text file
string Candy_selection[16][4] = {""};//checks array for valid data

int main()
{
string code = "";
string name = "";
double price = 0.0;
double paid = 0.0;
int remaining = 3;
double change = 0.0;
bool item_bought = false;

string convertInt(int number);//converts string to int
fill_array();//fills array


for(int x = 0; x<16; x += 1)
{
    cout << "The codes are "<<Candy_selection[x][0]<<endl;
}

while(code != "E5")
{
    cout << "Enter the candy code: ";//ask for code for candy
    cin >> code;
    int x = 0;
   while(x < 16 && item_bought == false )
    {//checks if code matches up with a valid code
        if(code == Candy_selection[x][0])//if code found
        {
            name = Candy_selection[x][1];//assigns name of candy to name
            price = (double)atof(Candy_selection[x][2].c_str()); //convert to double 
            remaining = (int)atof(Candy_selection[x][3].c_str());//convert to int

            if(remaining != 0)//if there is any of that candy left continue processing
            {
                cout << "You are buying a "<<name<<" The Cost is $"<<price<<endl;//tells how much the candy bar is
                cout << "Please insert money $";
                cin >> paid; 
                if(paid >= price) //makes sure the amount paid is atleast = to the candy bar
                {
                  if(paid > price)
                    {
                        change = paid - price;//calculates change if any
                    } 
                    remaining -= 1; //since candy was bought lowers teh amount remaining by one

                    Candy_selection[x][3] = convertInt(remaining);//converts to string and stores updated value in array

                    cout << fixed <<setprecision(2);
                    cout <<"You have bought a " <<name <<" for $"<<price <<" your change is $"<<change<<endl; 

                }else cout << "you didnt have enough money "<<endl;//not enough paid
            }else cout << "This treat is sold out, please select another"<<endl;//remainder == 0
        }else if(code == Candy_selection[16][0])//if code is not found in array
        {
            cout << "Code not found "<<endl;//code not found
        }
        item_bought = true;//acts as a sentinal value to get out of loop if item is bought
        x+= 1;//moves to next array location

    }
    //reseteting values
    code = "";
    item_bought = false;
    name = "";
    price = 0.0;
    paid = 0.0;
    change = 0.0;
    x = 0;
}
system("pause");
}

void fill_array()//fills array with data from text file
{
    string candy_code = "";
    string candy_name = "";
    string candy_price = "";
    string remaining_candy = "";
    ifstream Candy;     
    Candy.open("Candy.txt");
        if(Candy.is_open() == true)
        {
            while(Candy.eof() == false)
            {

                for(int y = 0;y < 16; y += 1)//puts the data in array then loops on next line then repeats
                {
                    getline(Candy, candy_code, '#');//picking the code name from first field
                    getline(Candy, candy_name, '#');//picking the candy name from second field 
                    getline(Candy, candy_price, '#');//picking the candy price from third field 
                    getline(Candy, remaining_candy, '\n');//picking the candy limit in stalk from 4th field 
                    //puts each entry in the array
                    Candy_selection[y][0] = candy_code;
                    Candy_selection[y][1] = candy_name;
                    Candy_selection[y][2] = candy_price;
                    Candy_selection[y][3] = remaining_candy;

                }
            }
        Candy.close();
        }else cout <<"cannot open data file to sync to array"<<endl;

}
string convertInt(int number)
{
   stringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream
}

文本文件如下

A1#Jelly Beans#1.00#3
A2#Penut butter cup#1.00#3
A3#PB&J#1.00#3
A4#100 Grand bar#1.00#3
B1#Crackers#1.00#3
B2#gum#1.00#3
B3#gummy worm#1.00#3
B4#chocolot bar#1.00#3
C1#Honey bun#1.00#3
C2#Pretzels#1.00#3
C3#Cinnamin bun#1.00#3
C4#gobstocker#1.00#3
D1#pop corn#1.00#3
D2#krabby paddy#1.00#3
D3#chocolot frog#1.00#3
D4#mint#.25#3

最佳答案

code == Candy_selection[16][0]

这可能是它,您正在访问第 17 个元素,但您只有 16 个元素。

如果不是,您需要搜索类似的溢出、空访问等。使用调试器、gdb 或 visual studio 运行它

关于c++ - “An access violation (Segmentation Fault) raised in your program.”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394945/

相关文章:

c++ - 在 C++11 中,如何在捕获到信号时立即离开函数?

c++ - 无法从 WindowsXP 上的子文件夹加载库

c - 打印双向链表时发生访问冲突

iphone - 应用崩溃:内存不足

c++ - 在 visual studio 2010 中实现安装项目后使用 new 时出现 unhandlex 异常

c++ - Lua:避免 pcall 和 Lua 调用堆栈重载

c++ - 在 apache2 上使用 cgi c++ 处理上传的文件

c++ - 为什么 system() 在 fork() 系统调用中使用 100% CPU?

android - 基于 Qt 的 Android 应用程序在一台特定设备上启动时崩溃。 "dlopen failed: libqtforandroid"

ios - 如何符号化苹果评论者发送的崩溃日志?