“程序中出现了访问冲突(Segmentation Fault)。”

时间:2013-08-23 04:55:25

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

我是编程的新手,我得到了一个我从未听说过的错误,如果我按下任何超过A1的东西它会崩溃 我收到错误“程序中出现的访问冲突(分段错误)。” ive almsot完成了该计划 这阻止了我 为什么我可以进入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

1 个答案:

答案 0 :(得分:1)

code == Candy_selection[16][0]

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

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