Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 03-20-2005, 12:55 PM   #1 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Exclamation Total beginner, need help with my code! Urgent timeframe

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;
}

Last edited by cleverest; 03-20-2005 at 12:59 PM. Reason: typos
cleverest is offline   Reply With Quote
Old 03-20-2005, 02:43 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Something like this?
Code:
#include <iostream>
#include <cstring>
#include <cmath> //floor()

using namespace std;

const double BOOM_FACTOR = 1.15;
const double BAD_FACTOR = 0.8;

int main()
{
  double population = 0;
  char userChoice = 'y';
  
  cout << "Enter a starting population size: ";
  cin >> population;
  
  double PredictBoomPopulation;
  double PredictBadPopulation;
  
  //std::floor: 0.67 humans don't exist.
//Let's add 0.5 because of rounding errors during conversion.

  PredictBoomPopulation = floor(population* BOOM_FACTOR);
  PredictBadPopulation = floor(population *BAD_FACTOR);
  cout << "After a boom year, your population will grow to: "
    << PredictBoomPopulation << endl;
  cout << "After a bad year, your population will shrink to: "
    << PredictBadPopulation << endl;
  
  do
  {
    cout<<endl;        
    cout << "Was it a boom or BAD year (o/a)? ";
    cin >> userChoice;
    if (userChoice == 'o')
    {
      population = floor(PredictBoomPopulation);
    }
    if (userChoice == 'a')
    {
      population = floor(PredictBadPopulation);
    }

    cout<<"Population: "<<population<<endl<<endl;
    
    PredictBoomPopulation = floor(population* BOOM_FACTOR+.5);
    PredictBadPopulation = floor(population *BAD_FACTOR+.5);
    cout << "After a boom year, your population will grow to: "
        << PredictBoomPopulation << endl;
    cout << "After a bad year, your population will shrink to: "
        << PredictBadPopulation << endl;

    cout<<"Do you wish to continue program? (yes = y): ";
    cin>>userChoice;
  } while(userChoice == 'y');
  
  cout<<"Program terminated."<<endl;
   return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 03-20-2005, 04:51 PM   #3 (permalink)
cleverest
Registered User
 
Join Date: Mar 2005
Posts: 22
cleverest is on a distinguished road
Wow very nice, I really appreciate you taking the time to answer my inquiry as quickly as you did, if you want to reply again to the following that would rock!

Otherwise, thanks a million anyways!!

Your code will help me understand how to loop the CONTINUE prompt and such ,for sure, but.....it does not match the top dialog exactly (He is probably super picky about that) so I'll have to play around with it to figure out these remaining issues.

for instance:

1. The 'CONTINUE (y/N)' prompt needs to be before the: 'Was it a boom or bad year (o/a)?' question. (even though regardless BOTH questions get outputted to the screen, this confuses little 'ol me)
2. When finally answering N to the continue prompt it should display the: 'Your final population is " ' and then terminate, instead of just terminating the program without the final population shown. (determined by the last 'was it a boom or bad year' prompt.....)
3. The cout text line "Population: ####" should NOT display after answering the Y/N or O/A prompts.

Also we have covered briefly SWITCHES, do you think that would make this code easier as a beginner for me to create??

Thanks again.
cleverest is offline   Reply With Quote
Old 03-20-2005, 05:44 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
For questions 1,2 and 3, you only need to move things up or down most likely. Just fiddle with it a bit.

On the switch issue, that is entirely up to you. Get the code right first, then see if you like a switch statement in this case.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Assignment (Urgent help req.) Boltress Standard C, C++ 0 01-12-2005 07:59 AM
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM


All times are GMT -8. The time now is 04:04 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting