noob needs helpses! (if/else and while loops)

if/else loop returns user to initial while loop

Page 1 of 1

3 Replies - 1723 Views - Last Post: 04 May 2008 - 06:25 PM Rate Topic: -----

#1 miensta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-May 08

noob needs helpses! (if/else and while loops)

Post icon  Posted 03 May 2008 - 04:31 PM

ok so im trying to make an if/else loop that redirects you to the initial while loop; i.e. do multiple runs until you don't want to anymore...also, if the number is negative, i want it to go to a new run as well.




#include <iostream>
#include <string>

using namespace std;

int main()
{
	double x = 0;
	double factorial = 0;
	int count = 0;
	
	cout << "Would you like to do a factorial? (y/n) \n";
	
	string response;

	cin >> response;
	cout << "Enter a positive integer.\n";

		while (response =="y")
	{

		cin >> x;
		if (x != -1)

		{ factorial = (x*(x-1))*2;
			count ++;
		}
	if (x > 0)
	{ cout << "For your integer, " << x << " , the factorial is " << factorial << "\n";
	}
	else if (x < 0)
			{
	cout << "The number you have entered is not a positive integer\n";
			}
	else (count > 0);
	{ cout << "Would you like to do another factorial? (y/n) \n";

		}
}
			
return 0;

}



This post has been edited by miensta: 03 May 2008 - 04:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: noob needs helpses! (if/else and while loops)

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: noob needs helpses! (if/else and while loops)

Posted 03 May 2008 - 06:37 PM

You had a few things going wrong here. And the equation you were using for a factorial was not right. Remember factorials are like 5! which means 5 x 4 x 3 x 2 x 1 = 120.

Here is your program fixed up a bit...

#include <iostream>
#include <string>
using namespace std;

long factorial(long n);

int main()
{
    long x = 0;
    double factorialval = 0;
    int count = 0;
    
	string response;

	// Prompt user to figure out a factorial
    cout << "Would you like to do a factorial? (y/n) \n";
    cin >> response;

	// Check their response
    while (response == "y")
    {
		// Ask them for the number
		cout << "Enter a positive integer.\n";
        cin >> x;

		// If it is positive, figure out the factorial and print result.
        if (x > 0)
        { 
			factorialval = factorial(x);
			cout << "For your integer, " << x << " , the factorial is " << factorialval << "\n"; 
            count++;
        }
		else {
			// Otherwise tell them that they need to enter a positive value.
			cout << "The number you have entered is not a positive integer\n";
        }
    
		// Prompt again if they would like to do a factorial
		cout << "Would you like to do another factorial? (y/n) \n";
		cin >> response;
	}
            
	return 0;

}

// Find factorial using recursion. Remember factorial is 5! = 5 x 4 x 3 x 2 x 1 = 120
long factorial(long n) {
	if (n == 1) { return 1; }
	else {
		return n * factorial(n - 1);
	}
}



I have thrown in a factorial function for you to see how the recursion would work to find the solution. Keep in mind that factorials can grow very fast so don't use very high numbers or you will get some problems.

Read the in code comments to see how the flow of this program works and then you should get the idea of how things work together to solve the task.

Enjoy!

"At DIC we be factorial junkies... and crack head junkies but never mind that part." :snap:
Was This Post Helpful? 0
  • +
  • -

#3 miensta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-May 08

Re: noob needs helpses! (if/else and while loops)

Posted 03 May 2008 - 07:02 PM

:D thanks so much! i was trying to do a riemann sum instead of a factorial...and i get kind of tripped up by all the loops...
Was This Post Helpful? 0
  • +
  • -

#4 ajaymatrix  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 410
  • Joined: 15-May 07

Re: noob needs helpses! (if/else and while loops)

Posted 04 May 2008 - 06:25 PM

hope your problem is solved.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1