Jump to content

I'm having a problem with a project for university.

What I'm supposed to be doing is typing in a month (such as April), then depending on what month it is it has a certain amout of hours and if the hours I put in after the month exceed the hours of that month then the program has to cancel, if it is within the hours of the month then the program can keep goiong... I've looked everywhere and nothing is working

 

An example would be:

I type in "April"

Then if I type in 785

the program should cancel, 

if I type in 715 the prrogram should keep going,

I have to be able to do it for every month of the year adnd they have to have the appropriaate hours,

I swear I was never taught this in class.

 

Link to comment
https://linustechtips.com/topic/118189-c-really-stumped/
Share on other sites

Link to post
Share on other sites

Couple of things you'd need to keep track of: 

1) Each month has a fixed number of hours depending on which month. 24*daysinMonth. A good way to keep track of days/month is an array of some sort. 

2) A simple conditional will work. 

 

Pseudo-code:

Print "Enter month: " (user enters month)Check number of days for entered month, store as daysinMonth.Print "Enter number of days: " (user enters days, store as enteredDays) If (enteredDays <= daysinMonth)> Keep goingelse > exit commandendif

Instead of exiting the program if the number is too high though, it would be better to keep prompting the user to enter a valid number, something like:

 

print "Enter number of days: (max 24*daysinMonth)"

 

daysinMonth should contain the number of days for the entered month. 24* that should be the number of hours. 

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
https://linustechtips.com/topic/118189-c-really-stumped/#findComment-1576558
Share on other sites

Link to post
Share on other sites

There are few ways to do it. For simiplicity, you can go with 2 arrays:

std::string months[12] = {"Janurary","February",...};int hoursInMonths[12] = {24*31,24*28,...};

and then whenever you make input you go through all months using a loop, in pseudo-code

While having contents in arrays  compare current month with input month, if same    compare current hours in month, if smaller or same      keep going    else      cancel/exit

This is essentially a loop with nested if statements inside. If you still can't figure it out, post your code, but if it's for a monitored coursework, I wouldn't risk it.

Link to comment
https://linustechtips.com/topic/118189-c-really-stumped/#findComment-1603149
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×