Question

PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program tha...

PLEASE WRITE CODE FOR C++ !

Time Validation, Leap Year and Money Growth

PartA: Validate Day and Time

Write a program that reads a values for hour and minute from the input test file timeData.txt . The data read for valid hour and valid minute entries. Assume you are working with a 24 hour time format. Your program should use functions called validateHour and validateMinutes. Below is a sample of the format to use for your output file  timeValidation.txt :

   Line 1 13:45 hours and minutes are valid

   Line 2 25:30 hour is invalid minutes are valid

   Line 3 11:33 hour is valid and minutes are valid

   Line 4 26:61 hour and minutes are both invalid

Use the file timeData.txt as input data to you program

Upload your C++ file as timeValidation.cpp

Upload your output file as timeValidation.txt

PartB: Leap Years

Write a program which will calculate the number of leap years between any two years from 1900 to 2099. Program should prompt for starting year and ending year. The program should validate that the starting date and ending dates are within the above range. The program should also validate that the ending year is greater than the beginning year. The program should have Boolean function that tests if a given year is a leap year. Use a for loop to call the leap year test function.

The leap year algorithm:

if (year is not divisible by 4) then (it is not a leap year)
else
if (year is not divisible by 100) then (it is a leap year)
else
if (year is not divisible by 400) then (it is not a leap year)
else (it is a leap year)

(see http://en.wikipedia.org/wiki/Leap_year (Links to an external site.)Links to an external site. )

Your program should access the input file dataForLeapTests.txt to test your program. This file contains pair of years separated by a space. The first year is the beginning year. The second year is the ending year.

Example of output format your program should create:

2000-2016 there are 5 leap years in this range

2015-2020 there are 2 leap years in this range

1859-1910 (invalid range error for calculation)

Submit the following files to Canvas: leapYear.cpp and leapYearRun.jpg

PartC: How much to save

You have several dates you plan to go on a trips. Calculate how many days it will take you to raise your goal for money for a given trip if you start with an initial deposit amount of 2 cents. Each day you will double the amount of money to add to your trip fund. Day one would be 2 cents, day two add 4 cents, day three add 8 cents and so on. Note 2 cents = .02 dollars.

Calculate the amount of days it will take to earn your target trip money for each trip using the input text file moneyNeededForMyTrips.txt

Save your results in the file daysNeededPerTrip.txt

Use the file tripsToTake.txt as input to your program. Output the trip name, amount and number of days it will take.

Example input data:

Hawaii 4000

Brazil 7000

Take your input data from the file tripSavingPlans.txt

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part a)

here is your program:

#include<iostream>
#include<fstream>
using namespace std;
bool validateHour(int h) //function which will check validity of hour
{
if(h<0 || h>23)
{
return false;
}
return true;
}
bool validateMinutes(int m) //function which will check validity of minutes
{
if(m<0 || m>59)
return false;
return true;
}

int main()
{
ifstream f;
f.open("timeData.txt"); //here the file will be opened
  
ofstream of;
of.open("timeValidation.txt"); //in this file we will write data
string s;
int i=1;
while(f)
{
f>>s;
int h=(s[0]-'0')*10+(s[1]-'0'); //convert character to integer
int m=(s[3]-'0')*10+(s[4]-'0');
bool a=validateHour(h);
bool b=validateMinutes(m);
if(!f)
break;
of<<"Line "<<i<<" "<<h<<":"<<m<<" ";

if(a)
{
if(b)
{
of<<"hour is valid and minutes are valid\n";
}
else
{
of<<"hour is valid and minutes are valid\n";
}
}
else{
if(b)
of<<"hour is invalid minutes are valid\n";
else
of<<"hour and minutes are both invalid\n";
}
i++;

}
}

here is the ss of both input and output file:

X timeValidation - Notepad timeData Notepad File Edit Format View Help File Edit Format View Help 13:45 Line 1 13:45 hour is

part B)

here is the program for this part

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

bool leap_year(int y) //this function will check for leap year
{
if(y%4!=0)
return false;
else if(y%100!=0)
return true;
else if(y%400!=0)
return false;
else
return true;
}
int main()
{
ifstream f;
f.open("dataForLeapTests.txt"); //here the file will be opened
  
ofstream of;
of.open("leapYearRun.txt"); //in this file we will write data
int y1,y2;
while(f)
{
f>>y1>>y2;
if(!f)
break;
of<<y1<<"-"<<y2;
if(y1<1900 || y2>2099 || y1>y2)
of<<" (invalid range error for calculation)\n";
else
{
int i=0;
for(int y=y1;y<=y2;y++)
{
if(leap_year(y))
i++;
}
of<<" there are "<<i<<" leap years in this range\n";
}
}
  
}

here is the ss of the input file and output file

dataForLeapTests - Notepad leapYearRun - Notepad X File Edit Format View Help File Edit Format View Help 2000-2016 there are

Part c)

here is the required program

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

int daysNeeded(int money) //this will calculate days
{
money=money*100; //convert dollar into cents
int d=1;
int it=2;
while(it<money)
{
d++;
it*=2;

}
return d;
}
int main()
{
ifstream f;
f.open("moneyNeededForMyTrips.txt"); //read from this file
ofstream of;
of.open("daysNeededPerTrip.txt"); //put output in this file
string s;
int money;

while(f)
{
f>>s>>money;
if(!f)
break;
of<<s<<" "<<daysNeeded(money)<<"\n";
}
}

here is the ss of the input and output file

moneyNeeded ForMyTri ps - Notepad X daysNeededPerTrip Notepad File Edit Format View Help Hawaii 19 Brazil 20 File Edit Format

I hope this will help you so please give positive ratings :))

Add a comment
Know the answer?
Add Answer to:
PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program tha...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Extend this date validation program, so that it checks for valid leap years (in which the...

    Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...

  • Has to be written in C++ Write a program that asks the user to enter the...

    Has to be written in C++ Write a program that asks the user to enter the month (1-12) and the year (0-2019). Validate the input and print an error and stop the program if an invalid month or year was entered. The program should then display the number of days in that month. Use the following criteria to identify leap years: Determine whether the year is divisible by 100. If it is, then it is a leap year if and...

  • FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary...

    FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary year or bissextile year) is a calendar year containing one additional day (or, in the case of lunisolar calendars, a month) added to keep the calendar year synchronized with the astronomical or seasonal year." In the Gregorian calendar, which is in use today, to determine leap year: A year is a leap year if it is divisible by 400 Or it is divisible by...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

  • leap year C++

    the instructions are: A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are:1) The year must be divisible by 42) If the year is a century year...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • c++ please :) First, ask the user to enter a year (4-digit) and what week day...

    c++ please :) First, ask the user to enter a year (4-digit) and what week day does this year starts with (String) For example, the year 2018 starts with the week day "Friday". In this case, the calendar should start from January 1 which is Friday, 2018 Your program should produce a formatted calendar for the specified year and there should be a blank line after each month. Moreover, the month and the year should be centered over each month....

  •    Lab/HW 3   Write a program that reads in the following data, all entered on one...

       Lab/HW 3   Write a program that reads in the following data, all entered on one line with at least one space separating them: a) an integer representing a month b) an integer representing the day of the month c) an integer representing a year 1. Check that the month is between 1 and 12. If it’s not print an error message. 2. Day cannot be 0 or less nor can it be more than 31 for all months. Print...

  • 3. Using no date-specific Java libraries, write a program that computes the number of days between...

    3. Using no date-specific Java libraries, write a program that computes the number of days between any two dates (inclusive). You must take into account leap years. The rule for computing a leap year is as follows: If the year is divisible by 4 it is a Leap Year ... Unless the year is divisible by 100, then it is _not_ a Leap Year ... Unless the year is divisible by 400, then it _is_ a Leap Year. During a...

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT