Some help on a program

Started by
41 comments, last by Blckknight118 13 years, 6 months ago
Wow, so I thought I finally had it and started to celebrate a little but the program outputs some right numbers and some wrong. An example would be entering in 5 days and 10 crud and 10 comes out but 15 days and 10 crud is 10 crud. Is there something wrong with my formula?
Source:
[source lang ="cpp"]#include<iostream>using namespace std;int greencrud(int crud_days, int amount_crud){	    int crud_fib = 0;	int numberof_crud = amount_crud;	int i;	int t;	for( i = crud_days / 5; i <= crud_days; i++)	{		t = crud_fib + numberof_crud;		crud_fib = numberof_crud;		numberof_crud = t;	}  return numberof_crud;}int main()    {		int numberofcrud2 = 0;        cout << "Welcome to Crud Estimator\n";        cout << "Please enter the number of days the crud has been grown \n";        int crud_days = 0;        cin >> crud_days;        cout << "Please enter the original amount of crud ";        int amount_crud;        cin >> amount_crud;        cout << "\nThe number of crud you have in your household is " << greencrud(crud_days, amount_crud);        return 0;}


[Edited by - Blckknight118 on October 19, 2010 3:45:47 PM]
Advertisement
Quote:
Is there something wrong with my formula?

Yes:
int greencrud(int crud_days, int amount_crud){	// I'd probably name these variables "result" and "previous"	// Those these names aren't unreasonable.	int crud_fib = 0;	int numberof_crud = amount_crud; // You sure about this initial value?	// Other variables moved closer to use	// We start i = crud_days, and loop while i <= crud_days	// How many loops will we run?	for(int i = crud_days; i <= crud_days; i++)	{		// Always try to use descriptive variable names		// Loop counters are an exception		// Here, the variable is a temporary holder while we do some assignment		// So "temp", while not overly descriptive, is a better name than "t"		int temp = crud_fib + numberof_crud;		crud_fib = numberof_crud;		numberof_crud = temp;	}  	return numberof_crud;}
I finally got it!
#include<iostream>using namespace std;int greencrud(int crud_days, int amount_crud);int greencrud(int crud_days, int amount_crud){	    int crud_fib = 0;	int numberof_crud = amount_crud;	int x = crud_days / 5;		for( int i = 0; i < x; i++)	{		int temp = crud_fib + numberof_crud;		crud_fib = numberof_crud;		numberof_crud = temp;	}  return numberof_crud;}int main()    {		char ans;		do{		        cout << "Welcome to Crud Estimator \n";        cout << "Please enter the number of days the crud has been growing ";        int crud_days = 0;        cin >> crud_days;        cout << "Please enter the original amount of crud ";        int amount_crud;        cin >> amount_crud;        cout << "\nThe number of crud you have in your household is " << greencrud(crud_days, amount_crud) << '\n' << '\n';				cout << "Would you like to enter a different number?";		cout << "\n(Y/N)";		cin >> ans;		}		while(ans == 'y' || ans == 'Y');		cout << "\nGoodbye!" << endl;        return 0;		}

This topic is closed to new replies.

Advertisement