2015-10-06 46 views
-3
#include<iostream> 
#include<conio.h> 

using namespace std; 
//const int n=20; 

class cofee 
{  
    //public: 
    int i; 
    //int n=20; 
    int price[20]; 
    //int totalprice[n]; 

    public: 
    void getprice() 
    {  
     if(i=1) 
     { 
     price[i]=10; 
     cout<<"price is :"<<price[i]<<""\n; 
     } 
     else if(i=2) 
     { 
     price[i]=20; 
     cout<<"price is :"<<price[i]; 
     } 
     else if(i=2) 
     { 
     price[i]=30; 
     cout<<"price is :"<<price[i]; 
     } 
     else 
     { 
     cout<<"\n"; 
     } 
    } 

    void total() 
    { 
     int sum=0; 
     int n=3; 
     for(i=1;i<=n;i++) 
     { 
     sum = sum+price[i]; 
     } 
     cout<<"total is:"<<sum; 
    } 
}; 

int main() 
{ 
    cofee c; 
    int i; 

    do 
    { 
     cout<<"enter your choice"; 
     cout<<"\n1.plain cofee"; 
     cout<<"\n2.cold cofee"; 
     cout<<"\n3.farali cofee"; 
     cout<<"\n4.total"; 
     cout<<"\n5.quit"; 
     cout<<"\n"; 
     cin>>i; 

     switch(i) 
     { 
     case 1 : c.getprice(); break; 
     case 2 : c.getprice(); break; 
     case 3 : c.getprice(); break; 
     case 4 : c.total(); break; 
     case 5 : break; 
     default : cout<<"\nwrong cchoice"; 
     } 
    }while(i!=5); 

    getch(); 
    return 0; 
} 

我已經嘗試了幾乎所有我可以但仍然沒有給出正確的輸出。既沒有顯示任何錯誤或警告。與C++中的ledder containg陣列混淆

in if else ledder price is not stored value for i = 2 & 3.需要幫助來解決它。

+1

您的代碼中有非常基本的錯誤。通過一些基礎教程或課本上的課本例子,你會更好。 –

+0

你的代碼中有很多小問題(就像@RSahu已經指出的那樣)。你應該從小處着手,並試着理解代碼中每行或每個小節的含義。然後在一個較小的程序中嘗試使用該行/小節,以確保它實際上按照您的想法行事(您可能會感到驚訝)。這裏有一個提示可能會有幫助:你的'main()'函數中的變量'i'與'cofee'類中的成員'i'不同,即使它們有名字。你是否認爲在main中爲'i'賦值同樣會給''cofee'類中的'i'賦值? – crayzeewulf

+0

提示#2:閱讀[C++中'=='和'='的區別](http://programmers.stackexchange.com/questions/162256/in-c-and-c-what-methods-can - 防止偶然使用的最賦值,在哪裏)。 – crayzeewulf

回答

0

當你在一個if中使用(=)時,你沒有比較這些值,你實際上給了他這個值,所以第一個「if」將總是成立。如果他們都是「如果」而不是「其他」,他們都會是真的(用「==」來代替)!另外,對於你的變量,如果你不指定它們是公開的,它們默認是私有的。