I need help Fixing this I traced through the code, and it compiles not error but when I run the program its just goes crazy I need help finding a the soultion to this problem please
Thank you very much

#include <iostream>                                                                                      
#include <fstream>    
#include <cstdlib>  
  
using namespace std;    
int even(int sumE, int num, int temp);
int main ()    


{    
    int num, sumE=0, temp=0;    
     ifstream inData;      
     inData.open("f:/p4.txt");    
     inData >> num;
     while (!inData.eof() )
     {
           cout << num << " " << endl;
           //inData >> num;
           //even(sumE, num, temp);
     
     }
     cout << "The sum of the even numbers is: " << sumE;
     inData.close();
         
           
      system("pause");       
       return 0;    
} 

int even(int sumE, int num, int temp)
{
    
    if (num%2==0)
    sumE= temp +num;
    
}

Recommended Answers

All 10 Replies

As far as I know when you use value-returning function u definately need to return something (i.e) if u use a datatype instead of void to declare ur functiong then at the end of ur function (in ur case) u need to have something like return sumE;

Just try to fix that and they will take it from there

so void even instead of int?

In C++, function should have return statement if the function return type not void.

so void even instead of int?

No it does not matter as long as u know the boundaries of value-returng function coz there are case where u cant use....mostly a returning statement return one value

A value-returning function return its value via the use of return.... A value-returning function returns a value therefore it is used (called) in either an expression or an output statement, or as a parameter in a funciton call

Ok now coming back to ur problem.... I dont really understand what ur program has to do.... does it have to accept the odd number from the user or what

I said so in context of C++. But particular this progarm written abve, the function call "even(sumE, num, temp);" is commentted out. Also i dont understand the use of variable temp here. The function even(--) is adding only the even numbers here.

sumE, num and temp are local to main. Pass these variables by reference in order for the changes to show.

how do I that?

When u pass a variable by reference as Hammerhead pointed U gona have something like

int event ( int sumE, int &num, int &temp)

but you usually do that when u have used a non-returning function that is a void function

There is no much difference between non-value returning function(void function) and value returning function

So when do u use pass by reference...U use pass by reference when the value passed is changed in the function and the new value must be reflected in the calling program

What it means to pass by reference..... it means that a value inside the parameters receives a the address(memory location) of its corresponding parameters (i.e) u refer the new value to the value inside ur function parameters

What it means to pass by Value..... it means that a value parameter receives a copy of its corresponding actual parameter

I need help Fixing this I traced through the code, and it compiles not error but when I run the program its just goes crazy I need help finding a the soultion to this problem please
Thank you very much

#include <iostream>                                                                                      
#include <fstream>    
#include <cstdlib>  
  
using namespace std;    
int even(int sumE, int num, int temp);
int main ()    


{    
    int num, sumE=0, temp=0;    
     ifstream inData;      
     inData.open("f:/p4.txt");    
     inData >> num;
     while (!inData.eof() )
     {
           cout << num << " " << endl;
           //inData >> num;
           //even(sumE, num, temp);
     
     }
     cout << "The sum of the even numbers is: " << sumE;
     inData.close();
         
           
      system("pause");       
       return 0;    
} 

int even(int sumE, int num, int temp)
{
    
    if (num%2==0)
    sumE= temp +num;
    
}

You need to give a better description of what this program is supposed to do and what the problem is. "Goes crazy" isn't very descriptive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.