0

我是编程新手,我遇到了一个我从未听说过的错误,如果我按下 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
4

1 回答 1

1
code == Candy_selection[16][0]

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

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

于 2013-08-23T05:03:29.080 回答