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;
    
}

Dani AI

Generated

Quick diagnosis and a minimal fix.

Two faults explain the “goes crazy” behavior: the input loop never reads a new value (the inData >> num; inside the loop is commented out and the first extraction happens only once), and the helper even() is declared to return int but neither returns a value nor is its result used. As and noted, non-void functions must return a value; as suggested, updating sumE can also be done via pass-by-reference — but the simplest, clearest approach is to do the summation in the loop that actually reads the file.

Prefer the extraction-in-condition pattern and accumulate directly in main:

int sumE = 0;
int num;
while (inData >> num) {
    cout << num << '\n';
    if (num % 2 == 0) sumE += num;
}
cout << "The sum of the even numbers is: " << sumE << '\n';

If a helper is preferred, use one of two safe patterns: return the new sum, or make the helper void and pass sum by reference. Example alternatives:

int addIfEven(int sum, int num) { if (num % 2 == 0) sum += num; return sum; }

void addIfEvenRef(int num, int &sum) { if (num % 2 == 0) sum += num; }

Troubleshooting checklist (notes missing from earlier replies): confirm the file opened successfully (if (!inData) { cerr << "file open failed\n"; }), remove the unused temp variable, avoid while(!inData.eof()) (it is an anti-pattern), and only print the final sum after the loop. These changes resolve the infinite-loop/undefined-behavior symptoms described by and make the program deterministic and easy to test.

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.