2015-10-06 49 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”将总是成立。如果他们都是“如果”而不是“其他”,他们都会是真的(用“==”来代替)!另外,对于你的变量,如果你不指定它们是公开的,它们默认是私有的。