vinayak the great
vinayak the great

Reputation: 29

addition and subtraction problems in c++

If after addition, you have to divide by the third number, you need more than 3 variables. Why is that? can anyone please help me with this problem. it will be very grateful.why do we need to after addition, you have to divide by the third number, you need more than 3 variables. Why is that? thank you

#include <stdio.h> 
class res 
{ 
   int a[6],i; 
   public: 
   int result() 
   {
      for(i=0;i<3;i++) 
      { 
         if(a[i]%3==0) 
         {
            "sum=sum+a[i]";
         }
      }
   } 
};  // Added newly

int main() 
{
    res r; 
    int i,a[5];
    cout<<"enter three numbers"; 
    for(i=0;i<3;i++) 
    { 
       cin>>a[i]);
    } 
    r.result(); 
    return 0; 
}

Upvotes: 0

Views: 729

Answers (1)

Mahesh
Mahesh

Reputation: 34625

First you need to understand that the array variable in main is different from the class member a. And in result method, class variable a is not initialized with valid values to do a % operation on it.

if(a[i]%3==0) 
{
    "sum=sum+a[i]"; // And probably here you meant sum=sum+a[i];
                    // string should be enclosed in double quotes.
}

With the above modification made, class res doesn't know what the variable sum is.


Given the number of many other mistakes in your program, I suggest you to read a book from the suggested link.

Upvotes: 2

Related Questions