Hello everyone so this is the piece of code i'm wokingon..

the program ask to input an integer. If the integer is even the program will divide it by two and if the integer is odd the number will be multiplied by three and then after it is multiplied it will be added by one. if the input value is less than one , an error message will be displayed...
at the end I tried to print out how many of these operations were performed but this seems to do not want to work :(.
-several functions were added.

any help will be appreciated :). at the end I will be posting the final edit so someone else could benefit from this code ...

#include <iostream>
#include<cmath>
#include <cmath>

using namespace std;

void errorCheck(int number);
void counting(int number,int count);
void whileLoop(int number,bool finished);
void askNumber(int number);


int main()
{
  
int number;
int count;
bool finished;
  //variables



//asking the twice


	errorCheck(number);
	whileLoop(number,finished);
	counting(number,count);
	askNumber(number);
  
system("pause");
return 0;
}


void errorCheck(int number)
{

int number=0;
while (!(cin >> number))
{
cout<<"input error";
cin.clear();
cin.ignore(1000,'\n');
}


}
void counting(int number,int count)
{
count=0;
number=0;
cin.get(number);
while(number !='1')
{

count++;
cin.get(number);

}

}
void whileLoop(int number,bool finished)
{
number=0;
while (number%2)!==0
{
  cin>>number;
  (number/2);


}

	number=0;
  while(number%2)!=0
  {
  cin>>number;
  ((number*3)+1);
  
  }

  if(number==1)
  {
    finished=true;

}




}
void askNumber(double number)
{

  cout<<"input a number"<<endl;
  cin>>number;

}

Recommended Answers

All 7 Replies

It's called the "Collatz Conjecture".

Things I spot:

1.

void errorCheck(int number)
{
    int number=0; [b] // Redefenition of "number"[/b]

2. The function takes in a number, but doesn't return it, so you'll lose the number immediately after return from errorCheck(). Read this (scroll down to scopes)

3. The function counting() also asks for input from the user (but incorrectly). You don't want that.

4. The function whileLoop() also asks for input from the user, but now using the unbuffered >> operator. This is not what you want.

There are quite a few others things, but the main thing is: You got the complete structure of the program wrong. What I would do is something like:

number = userinput()
while number != 1
{
    if number % 2 == 0 // even
        number /= 2
    else // uneven
        number = (number * 3) + 1 
}

And you're done.

//so I got this code, made several changes but apparently the "number" variable is not passing the number to the loop... it does not save it in memory and the whole while loop got screwed :/

#include <iostream>
#include<cmath>
#include <cmath>

using namespace std;

void errorCheck(int number);
void counting(int number,int count);
void askNumber(int number,int & a,int & b);
void calcEven (int number,int & a);
void calcOdd (int number,int & b);
//function prototypes
int main()
{
  
int number=0;
int count=0;
int a;
int b;

  //variable declarations



	askNumber(number,a,b);
	calcEven (number,a);
	calcOdd (number,b);
	errorCheck(number);
	counting(number,count);
	
  //function calls

system("pause");
return 0;
}
//pause and wait for an action by the user

void errorCheck(int number)
{


while (!(number>=1))
{
cout<<"input error";
cin.clear();
cin.ignore(1000,'\n');
}


}
//function definition errorcheck
void counting(int number,int count)
{

while(number !='1')
{

count++;
cin.ignore(100,'\n');

}

}
//function definition counting
void askNumber(int number,int & a,int & b)
{
number=0;
  cout<<"input a number";
  cin>>number;


while (number != 1)
{  
	if (number % 2 == 0)
		// even
		a;  
	else 
		// odd
		b; 
}
}
//function definition ask number
void calcEven (int number,int & a)
{a=(number/2);
}

//function definition calcEven
void calcOdd (int number,int & b)
{b=((number*3)+1);}

//function definition calcOdd

3 loops all using number. You should know by now we need details, not vague descriptions.

And if you are passing values back from functions, pass them back. Don't use void functions. That's why they designed int functions.

That's some hard to follow code.

So pretty much what you is this:

cin >> number;
if(number & 1) number /= 2;
else           number  = (number * 3)+1;
if(number < 1) cout << "error message";

But in a loop that at the end prints the index value?

You may need to start over, and try it in a concise format.

yea sorry about that :) I just gave up lol I've been trying to do this for 2 days and I so could not get it right. :( oh well im getting an F in this assignment ... not a big deal thanks to everyone that tried to help :)

oh well im getting an F in this assignment ... not a big deal thanks to everyone that tried to help :)

Why do you refuse to read the useful replies in this thread?
MosaicFuneral and myself gave you the alogorithm you need. It takes only about 10 lines of code. Seriously: read it, program it, and come back in 30 minutes thanking us all because it'll work ;)

Why do you refuse to read the useful replies in this thread?
MosaicFuneral and myself gave you the alogorithm you need. It takes only about 10 lines of code. Seriously: read it, program it, and come back in 30 minutes thanking us all because it'll work ;)

haha you were right... its almost 4am here but I did it XD oh god I hate being in intro to programming :( lol it can be frustrating when something does not work :P ok gotta go to sleep (5hrs) i have class at 11 am / thank you so much ;)

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.