<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'How to read the input data into functions?' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'How to read the input data into functions?' posted on the 'C and C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Tue, 18 Jun 2013 19:47:59 -0700</pubDate>
    <lastBuildDate>Tue, 18 Jun 2013 19:47:59 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>How to read the input data into functions?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/422211/422211/how-to-read-the-input-data-into-functions/</link>
      <description>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?&lt;br /&gt;
&lt;br /&gt;
I have use the following input data:&lt;br /&gt;
104&lt;br /&gt;
3773&lt;br /&gt;
13&lt;br /&gt;
121&lt;br /&gt;
77&lt;br /&gt;
30751&lt;br /&gt;
&lt;br /&gt;
to determine the integers if they are:&lt;br /&gt;
1)a multiple of 7, 11 or 13&lt;br /&gt;
2)sum of the digits odd or even&lt;br /&gt;
3)the square root value(if positive)&lt;br /&gt;
4)is it a prime number&lt;br /&gt;
&lt;br /&gt;
and here is my code:&lt;br /&gt;
&lt;br /&gt;
#include&amp;lt;iostream&amp;gt;&lt;br /&gt;
#include&amp;lt;fstream&amp;gt;&lt;br /&gt;
#include&amp;lt;conio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;cmath&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
#define in_file "data.txt"&lt;br /&gt;
#define out_file "result.txt"&lt;br /&gt;
&lt;br /&gt;
void multiple();&lt;br /&gt;
void sum();&lt;br /&gt;
double square(double);&lt;br /&gt;
void prime();&lt;br /&gt;
&lt;br /&gt;
int num;&lt;br /&gt;
float value;&lt;br /&gt;
&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
	ifstream ins;&lt;br /&gt;
	ofstream outs;&lt;br /&gt;
&lt;br /&gt;
	ins.open(in_file);&lt;br /&gt;
	outs.open(out_file);&lt;br /&gt;
&lt;br /&gt;
	int num;&lt;br /&gt;
	ins&amp;gt;&amp;gt;num;&lt;br /&gt;
&lt;br /&gt;
	multiple();&lt;br /&gt;
	sum();&lt;br /&gt;
	double square();&lt;br /&gt;
	prime();&lt;br /&gt;
&lt;br /&gt;
	ins.close();&lt;br /&gt;
	outs.close();&lt;br /&gt;
	getch();&lt;br /&gt;
}&lt;br /&gt;
	void multiple()&lt;br /&gt;
	{&lt;br /&gt;
		if (num%7==0 || num%11==0 || num%13==0)&lt;br /&gt;
			cout&amp;lt;&amp;lt;num&amp;lt;&amp;lt;" is a multiple of 7, 11 or 13."&amp;lt;&amp;lt;endl;&lt;br /&gt;
	}&lt;br /&gt;
	void sum()&lt;br /&gt;
	{&lt;br /&gt;
		if (num%2==0)&lt;br /&gt;
			cout&amp;lt;&amp;lt;num&amp;lt;&amp;lt;" is a even number."&amp;lt;&amp;lt;endl;&lt;br /&gt;
		else &lt;br /&gt;
			cout&amp;lt;&amp;lt;num&amp;lt;&amp;lt;" is a odd number."&amp;lt;&amp;lt;endl;&lt;br /&gt;
	}&lt;br /&gt;
	double square(double value)&lt;br /&gt;
	{&lt;br /&gt;
		double num=sqrt(value);&lt;br /&gt;
		cout&amp;lt;&amp;lt;"The square root value is: "&amp;lt;&amp;lt;value&amp;lt;&amp;lt;endl;&lt;br /&gt;
		return value;&lt;br /&gt;
	}&lt;br /&gt;
	void prime()&lt;br /&gt;
	{&lt;br /&gt;
		for (int i=2; i&amp;lt;=num; i++)&lt;br /&gt;
		{&lt;br /&gt;
			for (int j=2; j&amp;lt;i; j++)&lt;br /&gt;
			{&lt;br /&gt;
				if (i%j==0)&lt;br /&gt;
					cout&amp;lt;&amp;lt;num&amp;lt;&amp;lt;" is a prime number."&amp;lt;&amp;lt;endl;&lt;br /&gt;
				else &lt;br /&gt;
					cout&amp;lt;&amp;lt;num&amp;lt;&amp;lt;" is not a prime number."&amp;lt;&amp;lt;endl;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/422211/422211/how-to-read-the-input-data-into-functions/</guid>
      <pubDate>Sun, 06 Mar 2011 21:57:39 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to read the input data into functions?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/422211/422243/re-how-to-read-the-input-data-into-functions/#422243</link>
      <description>So in regards to your question:&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Also some other issues:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
//...
multiple();
sum();
double square(); // This line is ignored because it is interpreted as 
                 //   a function prototype
prime();
//...
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Instead you should have something like the following:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
double value = square( num );
&lt;/pre&gt;&lt;br /&gt;
But judging on the structure of your code you could probably more easily change the square function to be something like the following:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
void square( );
//...
void square( )
{
  double value = sqrt( num );
  cout&amp;lt;&amp;lt;"The square root value is: "&amp;lt;&amp;lt;value&amp;lt;&amp;lt;endl;
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
And lastly you should add a loop around your read and function calls:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
  ins&amp;gt;&amp;gt;num;
  while( !ins.fail() )
  {
    multiple();
    sum();
    square();
    prime();

    ins&amp;gt;&amp;gt;num;
  }
&lt;/pre&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/422211/422243/re-how-to-read-the-input-data-into-functions/#422243</guid>
      <pubDate>Mon, 07 Mar 2011 16:32:15 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>Re: How to read the input data into functions?</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/422211/422380/re-how-to-read-the-input-data-into-functions/#422380</link>
      <description>It should be 'int main', see &lt;a href="http://richelbilderbeek.nl/CppMain.htm"&gt;http://richelbilderbeek.nl/CppMain.htm&lt;/a&gt; for references.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/422211/422380/re-how-to-read-the-input-data-into-functions/#422380</guid>
      <pubDate>Sat, 12 Mar 2011 02:40:16 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>This post has been deleted.</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/422211/422381/this-post-has-been-deleted/#422381</link>
      <description>This post has been deleted.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/422211/422381/this-post-has-been-deleted/#422381</guid>
      <pubDate>Sat, 12 Mar 2011 02:42:06 -0700</pubDate>
      <category>C and C++</category>
    </item>
    <item>
      <title>This post has been deleted.</title>
      <link>http://www.programmersheaven.com/mb/CandCPP/422211/422382/this-post-has-been-deleted/#422382</link>
      <description>This post has been deleted.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CandCPP/422211/422382/this-post-has-been-deleted/#422382</guid>
      <pubDate>Sat, 12 Mar 2011 02:43:57 -0700</pubDate>
      <category>C and C++</category>
    </item>
  </channel>
</rss>