0

私はプログラミングに不慣れで、A1 を超える何かを押すとクラッシュするという聞いたことのないエラーが表示され、「プログラムでアクセス違反 (セグメンテーション違反) が発生しました」というエラーが表示されます。ive almsot はプログラムを完了しました。これは私を止めさせます

#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 要素しかありません。

そうでない場合は、同様のオーバーフロー、null アクセスなどを検索する必要があります。デバッガー、gdb、またはビジュアル スタジオで実行します。

于 2013-08-23T05:03:29.080 に答える