How to read the input data into functions? - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# How to read the input data into functions?

Posts: 1Member
I'm having a lot of troubles trying to read the input data into the program. I'm a beginner of C++ and this is my first time to learn to use functions. I wrote out the code but I only got zero. Can someone please help me out?

I have use the following input data:
104
3773
13
121
77
30751

to determine the integers if they are:
1)a multiple of 7, 11 or 13
2)sum of the digits odd or even
3)the square root value(if positive)
4)is it a prime number

and here is my code:

#include
#include
#include
#include
using namespace std;

#define in_file "data.txt"
#define out_file "result.txt"

void multiple();
void sum();
double square(double);
void prime();

int num;
float value;

void main()
{
ifstream ins;
ofstream outs;

ins.open(in_file);
outs.open(out_file);

int num;
ins>>num;

multiple();
sum();
double square();
prime();

ins.close();
outs.close();
getch();
}
void multiple()
{
if (num%7==0 || num%11==0 || num%13==0)
cout<<num<<" is a multiple of 7, 11 or 13."<<endl;
}
void sum()
{
if (num%2==0)
cout<<num<<" is a even number."<<endl;
else
cout<<num<<" is a odd number."<<endl;
}
double square(double value)
{
double num=sqrt(value);
cout<<"The square root value is: "<<value<<endl;
return value;
}
void prime()
{
for (int i=2; i<=num; i++)
{
for (int j=2; j<i; j++)
{
if (i%j==0)
cout<<num<<" is a prime number."<<endl;
else
cout<<num<<" is not a prime number."<<endl;
}
}
}

• Posts: 54Member
So in regards to your question:

The reason you are only getting zero is an issue of scope. You redefine num in your main function which hides the global num and thus the global num isn't the variable that is written to when the data is read in.

Also some other issues:

[code]
//...
multiple();
sum();
double square(); // This line is ignored because it is interpreted as
// a function prototype
prime();
//...
[/code]

Instead you should have something like the following:
[code]
double value = square( num );
[/code]
But judging on the structure of your code you could probably more easily change the square function to be something like the following:
[code]
void square( );
//...
void square( )
{
double value = sqrt( num );
cout<<"The square root value is: "<<value<<endl;
}
[/code]

Your prime function finds all primes less than that of the number you are looking for so that doesn't seem to operate as specified in that the function should determine whether or not the number is a prime or not.

And lastly you should add a loop around your read and function calls:
[code]
ins>>num;
while( !ins.fail() )
{
multiple();
sum();
square();
prime();

ins>>num;
}
[/code]
• Posts: 754Member
It should be 'int main', see http://richelbilderbeek.nl/CppMain.htm for references.
• Posts: 754Member
This post has been deleted.
• Posts: 754Member
This post has been deleted.