2017-11-18 265 views
-2

this is the code source of my programC++,而(P!= NULL)不工作

我在與while循環的問題//而(P!= NULL) 它不工作每當執行reachs循環程序停止工作!儘管P已經初始化,並且p在循環之後不被使用!

void Ajout_Debut() { 
Q=(Liste*) malloc(sizeof(Liste)); 
Liste *Q=new Liste(); 
cout<<"donnez cod_prod"<<endl; 
cin>>cp; 

cout<<"donnez designation"<<endl; 
cin>>ds; 
cout<<"donnez unite de mesure"<<endl; 
cin>>u; 
cout<<"donnez prix unitaire d'achat hors taxe'"<<endl; 
cin>>pu; 
cout<<"donnez la quantite achetee"<<endl; 
cin>>qt; 
cout<<"donnez tva (9 ou bien 21)"<<endl; 
cin>>tv; 
Q->cod_prd=cp; 
Q->dsg=ds; 
Q->tva=tv; 
Q->um=u; 
Q->pua=pu; 
Q->qte=qt; 
if(Tete!= NULL) { 
    Q->svt=Tete; 
    Tete=Q; 
} 
else { 
    Tete=Q; 
    Tete->svt==NULL; 
    free(Q); 
}} 



void Afficher() { 
P=Tete; 
int a=1; 
if(P==NULL) 
    cout<<"La liste est vide !"<<endl; 
while(P!=NULL) { 
    cout<<P->cod_prd<<endl; 
    cout<<P->dsg<<endl; 
    cout<<P->um<<endl; 
    cout<<P->pua<<endl; 
    cout<<P->qte<<endl; 
    cout<<P->tva<<endl; 
    P=P->svt; 
    } 
} 
int main() { 

Tete=NULL; 
int c; 
string cp; 
do { 

cout<<"\001                   \001"<<endl; 
cout<<"\001 01-Ajouter un produit au debut           \001"<<endl; 
cout<<"\001 02-Suprimer un produit             \001"<<endl; 
cout<<"\001 03-Afficher la liste des produit achetes         \001"<<endl; 
cout<<"\001 04-Afficher le nombre total des produits achetes       \001"<<endl; 
cout<<"\001 05-afficher le montants total HT           \001"<<endl; 
cout<<"\001 06-afficher le montants total TVA           \001"<<endl; 
cout<<"\001 07-afficher le montants total TTC (Net à Payé)       \001"<<endl; 
cout<<"\001 08-demander au client le paiement et calculer la difference    \001"<<endl; 
cout<<"\001 09-afficher le prix max et le prix min         \001"<<endl; 
cout<<"\001 10-Vider la liste               \001"<<endl; 
cout<<"\001 00-quitter                \001"<<endl; 
cout<<"\001                   \001"<<endl; 

cout<<"         Votre Choix:"; 
cin>>c; 
if(c<0 || c>10) { 
    cout<<"Votre choix n'existe pas ! "<<endl; 
} 
if (c>0 && c<11) { 
switch(c) { 
    case 1 : 
     Ajout_Debut();break; 
    case 2 : 
     Supprimer();break; 
    case 3 : 
     Afficher();break; 
    case 4 : 
     CalAffi (4);break; 
    case 5 : 
     CalAffi (5);break; 
    case 6 : 
     CalAffi (6);break; 
    case 7 : 
     CalAffi (7);break; 
    case 8 : 
     Payment();break; 
    case 9 : 
     MaxMin();break; 
    case 10: 
     Vider();break; 
}}}while(c!=0); if (c==0) system("PAUSE");} 

AND here is a photo of what happens when i call the afficher function 由我使用的功能ajout_debut afficher beforecaliing的方式,使已經存在的列表

+2

歡迎來到stackoverflow.com。請花些時間閱讀[幫助頁面](http://stackoverflow.com/help),尤其是名爲[「我可以問些什麼話題?」](http://stackoverflow.com/help/討論話題)和[「我應該避免問什麼類型的問題?」](http://stackoverflow.com/help/dont-ask)。還請[參觀](http://stackoverflow.com/tour)和[閱讀如何提出好問題](http://stackoverflow.com/help/how-to-ask)。最後,請學習如何創建[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+2

請提供您的代碼樣本。 – jeteon

+1

該代碼中有很多P!= NULL的地方。 –

回答

0

這裏的元素是非常大問題:

Tete=Q; 
Tete->svt==NULL; 
free(Q); 

首先你使Tete指向與Q指向的內存相同的內存。這意味着TeteQ指向相同的內存

那麼幾行後,你釋放該QTete指向的內存。這意味着在此之後的所有解除引用Tete是無效的,並將導致undefined behavior

此外,使用Tete->svt==NULL;您不會將NULL分配給「下一個」指針svt。你比較它到NULL然後丟棄結果。

+1

其實,它比這更糟糕。 Ajout_Debut()中的第一個語句設置了一個名爲'Q'的變量(推測是全局變量)。第二個聲明一個名爲'Q'的變量,它不同於它,並使用'new'表達式初始化它。 「自由(Q)」本身因此具有未定義的行爲。 – Peter

+0

thnxx真的幫助只剩下一個問題,爲什麼當我只使用Q =(Liste *)malloc(sizeof(Liste));而不是Liste * Q =新的Liste(); ajout_debut函數的作用與afficher相同,我的意思是它停止了程序 – Mafaza