954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Displaying the Sum of Even and Odd numbers

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;
    
}
Jboy05
Junior Poster
124 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

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

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

so void even instead of int?

Jboy05
Junior Poster
124 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

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

pradeepaks
Newbie Poster
2 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 
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 ofreturn.... 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

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

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

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

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.

pradeepaks
Newbie Poster
2 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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

hammerhead
Posting Whiz in Training
257 posts since May 2006
Reputation Points: 46
Solved Threads: 24
 

how do I that?

Jboy05
Junior Poster
124 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

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

Traicey
Posting Whiz in Training
283 posts since Mar 2008
Reputation Points: 26
Solved Threads: 19
 

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You