有時代碼運行到最后沒有任何錯誤,而其他時候它在中間停止并給我這個錯誤執行緒 1:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)這是它的圖片(https://i.stack.imgur.com /uZDX1.png )。錯誤出現在我名為 Functions 的頭檔案中,而這張圖片中使用的編譯器是 Mac 設備上的 Xcode。我還在 Windows 設備上嘗試了另一個編譯器“Visual Studio”,代碼永遠不會運行到最后,它總是在運行中間停止,并在同一行代碼中給我一個錯誤,這是錯誤 Visual 的圖片Studio在 Visual Studio 中給了我錯誤。
#include <iostream>
using namespace std;
//products' data
struct products{
int ID;
string Name;
double Price;
int Quantity;
};
//receipt
struct receipt{
string name;
double price;
receipt* link;
};
struct linkedListFunctions{
//inserts node at the end of the list
void insert(receipt** head_name_ref, string new_name, double new_price)
{
receipt* new_name_node = new receipt();
receipt *last = *head_name_ref;
new_name_node->name = new_name;
new_name_node->price = new_price;
new_name_node->link = NULL;
if (*head_name_ref == NULL)
{
*head_name_ref = new_name_node;
return;
}
while (last->link != NULL)//The error is right here
{
last = last->link;
}
last->link = new_name_node;
return;
}
//prints list
void printReceipt(receipt* n){
while(n!=NULL){
cout<<n->name<<": ";
cout<<n->price<<'\t'<<" ";
cout<<endl;
n=n->link;
}
}
//removes first node in the list
receipt* removeFirstreceipt(struct receipt* head)
{
if (head == NULL)
return NULL;
receipt* temp = head;
head = head->link;
delete temp;
return head;
}
};
前兩個代碼框在名為 Functions.h 的頭檔案中 錯誤在第 15 行的第二個代碼框中,它旁邊有一個注釋
#include "Functions.h"
int main(){
struct products details[5];
details[0] = {0, "Apple Juice", 12, 240};
details[1] = {1,"Bread", 10, 100};
details[2] = {2, "Chocolate", 5, 500};
details[3] = {3, "Dates", 50, 150};
details[4] = {4, "Eggs", 30, 360};
linkedListFunctions list;
//declaring first node in receipt linked list
receipt* head = NULL;
head = new receipt;
//prints all products IDs and Names
for (int i=0; i<5; i ) {
cout<<details[i].ID<<": ";
cout<<details[i].Name<<" ";
cout<<details[i].Price<<"LE"<<endl;
}
char buyAgain;
while ((buyAgain='y' && buyAgain!='n')){
//choosing a product
cout<<"Enter the product's ID to choose it: ";
int chooseProduct;
cin>>chooseProduct;
cout<<"ID: "<<details[chooseProduct].ID<<endl
<<"Name: "<<details[chooseProduct].Name<<endl
<<"Price: "<<details[chooseProduct].Price<<endl
<<"Quantity: "<<details[chooseProduct].Quantity<<endl<<"********"<<endl;
//choosing the quantity
cout<<"How much "<<details[chooseProduct].Name<<" do you want? ";
int chooseQuantity;
cin>>chooseQuantity;
list.insert(&head, details[chooseProduct].Name, details[chooseProduct].Price*chooseQuantity);//
details[chooseProduct].Quantity=details[chooseProduct].Quantity-chooseQuantity;
cout<<details[chooseProduct].Name<<" Left: "<<details[chooseProduct].Quantity<<endl<<"********"<<endl;
cout<<"Would you like to order something else? y=yes n=no";
cin>> buyAgain;
switch(buyAgain) {
case 'y':
break;
case 'n':
//prints receipt
cout<<"***Receipt***"<<endl;
list.printReceipt(head);
}
}
}
最后一個代碼框是主要功能
uj5u.com熱心網友回復:
這肯定是錯的
char buyAgain;
while ((buyAgain = 'y' && buyAgain != 'n'))
您正在嘗試測驗未初始化的變數,但實際上正在分配給它
2>C:\work\ConsoleApplication1\ConsoleApplication1.cpp(106):警告C4706:條件運算式中的賦值
2>C:\work\ConsoleApplication1\ConsoleApplication1.cpp(106):警告 C4701:使用了可能未初始化的區域變數“buyAgain”
更重要的是你沒有初始化收據上的欄位,你應該有一個建構式,就像這樣
struct receipt {
string name;
double price;
receipt* link;
receipt() { price = 0; link = nullptr; }
};
uj5u.com熱心網友回復:
對于初學者來說,這些行:
receipt* head = NULL;
head = new receipt;
沒有多大意義。運算子 new 創建了一個型別的未初始化物件receipt
(例如,資料成員link
可以具有不確定的值),這是在函式中使用指標時未定義行為的原因。你需要的只是寫:
receipt* head = NULL;
此 while 回圈中的條件:
while ((buyAgain='y' && buyAgain!='n')){
沒有意義。buyAgain
在此子運算式中始終設定為 'y':
buyAgain='y'
看來你的意思是:
while ( buyAgain == 'y' ){
或者:
while ( buyAgain != 'n' ){
但是在while回圈之前你必須初始化變數buyAgain
char buyAgain = 'y';
由于結構收據是一個聚合,因此而不是函式插入中的這些陳述句:
receipt* new_name_node = new receipt();
new_name_node->name = new_name;
new_name_node->price = new_price;
new_name_node->link = NULL;
你可以寫:
receipt* new_name_node = new receipt
{
new_name, new_price, nullptr
};
注意結構中宣告的處理串列的函式struct linkedListFunctions
至少應該是靜態成員函式。
函式的引數printReceipt
應該有限定符 const:
void printReceipt( const receipt* n);
和空白的輸出:
cout<<n->price<<'\t'<<" ";
^^^^^^^^^^^^
實際上由于下一行而沒有效果
cout<<endl;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/470302.html