Help!

I can't get a value to pass

Page 1 of 1

3 Replies - 859 Views - Last Post: 16 October 2007 - 10:35 AM Rate Topic: -----

#1 DewK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-September 07

Help!

Posted 15 October 2007 - 07:23 PM

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void read(double &no);



int main()
{
  double count, no, temp = no;


read (no);

  while (temp != 1)
  {
	// if no is even
	if (temp % 2 == 0)
	{
	  temp /= 2;
	}
	// if no is odd
	else
	{
	  temp = temp * 3 + 1;
	}

	++count;
  }
  cout << "Cycle length of " << no << " is " << count << endl;

  return 0;
}

void read (double &no)
{

 cout << "Enter number: ";
 cin >> no;

}



I am trying to get this program to read the input from a different function in the in the program(i.e. read) and pass it back to the main function to determine the output and then output the final answer. I keep getting errors. I am not sure why. Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Help!

#2 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Help!

Posted 15 October 2007 - 07:41 PM

It would be helpful if you posted the actual errors that you are getting as well.

However, just from a quick look at this, you should probably know that the % operator doesn't like to play with the double data type and will cause an error if you try to use one as an argument. Are you trying to have the function read() get an integer? If so, declare no as an int, and have the function read() accept an int as an argument. If not, the fmod(double dividend, double divisor) function in cmath may be something you should try. It returns the modulus of two doubles or two floats (but you'll have to pass 2 as 2.0 instead).

And there doesn't seem to be a point to using temp in your if control statements. You can just use no as the variable in your statements. If you want to use temp for some reason, you'll have to do the temp=no statement after you've read in the variable using your function.

You should also intialize your counter variable to 0 if you're going to be incrementing it in your loop, otherwise you will be incrementing it from some unknown (uninitialized) value.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#3 XenoSophist  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 02-October 07

Re: Help!

Posted 16 October 2007 - 09:02 AM

One thing to consider is that your while loop is has more code than the read() function. I would suggest including the user prompt within the main function and calling a function to do the calculations.

The variable names used are not descriptive of what they are used for. To many people "no" is the English opposite of yes and not an abbreviation of number.

As suggested, integer calculation is much simpler than using doubles. If you must use doubles, use setprecision, etc. to limit decimal places because some divisions yield longn result.

Also, what would happen if the user typed in 0 instead of a non-zero number?

We would need to know exactly what errors you are getting to give a more complete answer of what is happening.

Jjhaag: I thought that if primitive types were not initialized, the compiler would assign default initial values. For double, this would be 0.0.
Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Help!

Posted 16 October 2007 - 10:35 AM

View PostXenoSophist, on 16 Oct, 2007 - 10:02 AM, said:

Jjhaag: I thought that if primitive types were not initialized, the compiler would assign default initial values. For double, this would be 0.0.

Many compilers try to do this, but it's not necessarily true.

On my machine, using gcc 3.4.2, the value of temp is 1.86931e-307, which might seem like it's small enough to be basically zero. That might work for addition and subtraction, but will not provide the expected result for a zero argument to multiplication, division, or the relational operators.

And if I change the variables to ints, it winds up being initialized to 4247014. So it's not something you can depend on.

I'm actually not sure what the standard has to say on this one. Anyone?

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1