Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Urgent, need help with simple C++ code (I'm a total beginner!)
Closed Thread
Old 03-20-2005, 05:08 PM   #1 (permalink)
 
Newb Techie

Join Date: Mar 2005

Posts: 2

cleverest

Send a message via AIM to cleverest
Exclamation Urgent, need help with simple C++ code (I'm a total beginner!)

Please someone help me, I'm supposed to come up with a C++ code to give the following dialog (user input capable text is in BOLD) I'm supposed to have one loop for full credit... but I'm not even concerned with that if I'm able to make this work some other way....what I have done so far is below and it's driving me nuts (I told you...I'm a total newbie!) :-D

I only have till midnight today!! Please someone help me out, I'll study it an learn it better then fighting with it all day and failing the challenge

(with the understanding that during
boom times, population increases by 15%. During bad times,
population decreases by 20%)

This is the desired dialog when the program is Run:

Enter a starting population size: 1000
After a boom year, your population has grown to:1150
After a bad year, your population has shrunk to:800
Continue(y/n)? y
Was it a boom or bad year (o/a)? o

Your starting population is now 1150
After a boom year, your population has grown to:1323
After a bad year, your population has shrunk to:920
Continue(y/n)? y
Was it a boom or bad year (o/a)? a

Your population is now 920
After a boom year, your population has grown to:1058
After a bad year, your population has shrunk to:736
Continue(y/n)? n
Was it a boom or bad year (o/a)? o

Your final population is 1058

Code:
#include <iostream>
using namespace std;


int main()
{
	double population = 0;
	double boom_year;
	double bad_year;
	char contin;
	char boomornot;

	cout << "Enter a starting population size: ";
	cin >> population;
	boom_year = (population * 1.15);
	bad_year = (population * 0.8);
	cout << "After a boom year, your population has grown to: " << boom_year << "\n";
	cout << "After a bad year, your population has shrunk to: " << bad_year << "\n";
	cout << "Continue(y/n)? "; // determines which IF statement below to acccess
	cin >> contin;

	if (contin == 'n') // I'm pretty confident with this section, it displays final result and then exits
	{
		cout << "Was it a boom or BAD year (o/a)? ";
		cin >> boomornot;
		if (boomornot == 'o')
	    {
		population = boom_year;
		cout << "Your final population is " << boom_year << "\n";
	    }
			if (boomornot == 'a')
	        {
		population = bad_year;
		cout << "Your final population is " << bad_year << "\n"; 
	        }
	
	}


	if (contin == 'y') // this section is a mess...it works, but can't loop and can't figure it out otherwise...HELP!
	{
		cout << "Was it a BOOM or bad year (o/a)? ";
		cin >> boomornot;
		if (boomornot == 'o')
	    {
		population = boom_year;
		cout << "Your starting population is now " << boom_year << "\n";
	    }
			if (boomornot == 'a')
	        {
		population = bad_year;
		cout << "Your starting population is now " << bad_year << "\n"; 
	        }
	
	}


	// need more code probably....can someone brilliant help me out??
	


	return 0;
}

cleverest is offline  
Old 03-20-2005, 06:31 PM   #2 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

I don't have time to write out the whole program but you need to use a while loop. First, in the beginning, ask for a starting size, display boom year, display bad year, and then ask whether to continue. Now the while loop starts, with the condition while contin == 'y'. In the body of the while loop, you need to ask if it was a boom or bad year, display the appropriate starting population, display boom year, display bad year, and ask whether the user wants to continue.

After the body of the loop is where you put the code to handle a 'n' for continue, since execution will stay in the body until the user enters n. So after the closing bracket for the while loop is where you would put the code that you have for if contin == 'n' (and you don't need an if statement anymore).

Hope this helped.
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 03-20-2005, 09:28 PM   #3 (permalink)
 
Newb Techie

Join Date: Mar 2005

Posts: 2

cleverest

Send a message via AIM to cleverest
Default added additional problem I've found

Actually I believe this will be VERY helpful, I'm going to try to work on what you said....

Since I've typed that sentence above this one, I've managed to write this code, and its VERY CLOSE...any suggestions anyone, to get it to resemble the program dialog in my first post up top?


Code:
#include <iostream>
using namespace std;

int main()
{
double population = 0;
	double boom_year;
	double bad_year;
	char contin;
	char boomornot;

	cout << "Enter a starting population size: ";
	cin >> population;
	boom_year = (population * 1.15);
	bad_year = (population * 0.8);
	cout << "After a boom year, your population has grown to: " << boom_year << "\n";
	cout << "After a bad year, your population has shrunk to: " << bad_year << "\n";
	cout << "Continue(y/n)? "; 
	cin >> contin;

	while (contin == 'y')
	{
    cout << "Was it a BOOM or bad year (o/a)? ";
		cin >> boomornot;
		if (boomornot == 'o')
	    {
		population = boom_year;
		cout << "Your starting population has grown to " << boom_year << "\n";
	    }
			if (boomornot == 'a')
	        {
		population = bad_year;
		cout << "Your starting population has shrunk to " << bad_year << "\n"; 
	        }
	cout << "Continue(y/n)? "; 
	cin >> contin;
	}

	 if (contin == 'n') 
		cout << "Was it a boom or BAD year (o/a)? ";
		cin >> boomornot;
		if (boomornot == 'o')
		{
		population = boom_year;
		cout << "Your final population is " << boom_year << "\n";
		}
	    


}
Also the program dialog that exists with the above program I've written is as follows and it doesn't match the goal exactly which is shown in the first post.....can anyone help? (I'm such a newbie but I'm learning!)

Enter a starting population size: 1000
After a boom year, your population has grown to: 1150
After a bad year, your population has shrunk to: 800
Continue(y/n)? y
Was it a BOOM or bad year (o/a)? o
Your starting population has grown to 1150
Continue(y/n)? y
Was it a BOOM or bad year (o/a)? a
Your starting population has shrunk to 800
Continue(y/n)? n
Was it a boom or BAD year (o/a)? o
Your final population is 1150
Press any key to continue
cleverest is offline  
Old 03-20-2005, 11:29 PM   #4 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

At the beginning of your while loop you need to display the present population (The starting population is now... ), calculate a boom and a bad year and display both the values (After a boom year... after a bad year...).
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On