C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to read the input data into functions? Posted by michelle87 on 6 Mar 2011 at 9:57 PM
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<iostream>
#include<fstream>
#include<conio.h>
#include<cmath>
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;
}
}
}


Report
Re: How to read the input data into functions? Posted by nebgast on 7 Mar 2011 at 4:32 PM
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:

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


Instead you should have something like the following:
double value = square( num );

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


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:
  ins>>num;
  while( !ins.fail() )
  {
    multiple();
    sum();
    square();
    prime();

    ins>>num;
  }

Report
Re: How to read the input data into functions? Posted by bilderbikkel on 12 Mar 2011 at 2:40 AM
It should be 'int main', see http://richelbilderbeek.nl/CppMain.htm for references.
Report
This post has been deleted. Posted by bilderbikkel on 12 Mar 2011 at 2:42 AM
This post has been deleted.
Report
This post has been deleted. Posted by bilderbikkel on 12 Mar 2011 at 2:43 AM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.