|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Ummmmm... yeah...... C++ help... as usual...
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||
|
Programmer
|
this is the assignment i have...
I'm not getting this at all... Well, i kinda get it, but only in parts. I'm not sure how to structure it all together for it to work correctly... I have some code with a few functions in it thinking thats the best way to do it... But its not going so well... Quote:
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <cctype>
#include <cstdlib>
using namespace std;
int readTime();
double calculateCharge(int timeIn, int timeOut);
int main()
{
int minSinceMidnight = 0;
double totalCharge = 0;
int timeArrive = 0;
int timeExit = 0;
cout << "Enter the time the car entered the parking lot." << endl;
timeArrive = readTime();
cout << "Enter the time the car exited the parking lot." << endl;
timeExit = readTime();
if (timeArrive >= 1800)
totalCharge = 5;
else
totalCharge = calculateCharge(timeArrive, timeExit);
if (totalCharge == -1)
cout << "Invalid times!" << endl;
else
cout << "Total charge = " << setprecision(2) << totalCharge << endl;
return(0);
}
int readTime()
{
int totalMin = 0; //total minutes since midnight
int iHour = 0; //value user enters for hour of the day
bool hourOK = false; //boolen to use for looping until the user gets input correct
int iMin = 0; //value user enters for minute of the hour
bool minOK = false; //boolen to use for looping until the user gets input correct
char cAM; //char used to store either A or P for am/pm
bool amOK = false; //boolen to use for looping until the user gets input correct
totalMin = 0; //initialize the total minutes since midnight
//the user will input the hour of the day and we will loop until they get it correct
while (!hourOK)
{
cout << "Enter hour of day: ";
cin >> iHour;
if ((iHour < 0) || (iHour > 12))
cout << "Hour must be between 0 and 12...please try again." << endl;
else
hourOK = true;
}
//the user will input the minutes of the hour and we will loop until they get it correct
while (!minOK)
{
cout << "Enter minute of hour: ";
cin >> iMin;
if ((iMin < 0) || (iMin > 59))
cout << "Minute must be between 0 and 59...please try again." << endl;
else
minOK = true;
}
//the user will input eith A or P (for am/pm) and we will loop until they get it correct
while (!amOK)
{
cout << "Enter A or P (for AM or PM): ";
cin >> cAM;
if ((toupper(cAM) != 'P') && (toupper(cAM) != 'A'))
cout << "You must enter an A or a P...please try again." << endl;
else
amOK = true;
}
if (toupper(cAM) == 'P')
totalMin = ((iHour + 12) * 60) + (iMin);
else
totalMin = (iHour * 60) + (iMin);
cout << "Total minutes since midnight = " << totalMin << endl;
return totalMin;
}
double calculateCharge(int timeIn, int timeOut)
{
int invalidInput = -1;
int totalMinutes = 0;
int totalHours = 0;
int hourDiff = 0;
double totalCharge = 0;
if (timeOut <= timeIn)
return invalidInput;
totalMinutes = timeOut - timeIn;
totalHours = totalMinutes / 60;
if (totalHours <= 4)
{
totalCharge = totalHours * 2.75;
}
else
{
hourDiff = totalHours - 4;
totalCharge = (4 * 2.75) + (hourDiff * 1.75);
}
if (totalCharge < 5.50)
totalCharge = 5.50;
if (totalCharge > 18.00)
totalCharge = 18.00;
return totalCharge;
}
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
||||||||||
|
|
|
|
#2 (permalink) | ||||||||||||||
|
Programmer
|
Reading through this (on my vacation AWAY from my programming job) and I'll check it out. Also bumping in case anyone else wants to chop through this with me. I'll see what I can do.
Below follow edits with comments/opinions ------------------------------------------------- Edit1: Quote:
Edit2: parallel arrays?? you need to get to object oriented programming ASAP. Edit3: you need to prototype all your required functions before you even start programming. Get everything they give you down on your file before your brain even starts cranking on how to actually code it. Edit4: I'm not sure what in the heck you're doing in main()! You need to do error checking for inputs, and you don't ask for the time incoming and the time exiting untill the person inputs "e" or "x". I'm rewriting main. Edit5: ENUMERATORS.
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
Last edited by kdbolt70 : 06-15-07 at 10:37 PM. |
||||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
Programmer
|
Ok, holy tired. I'm sitting here at my cottage doing the same thing I took vacation from
guess I can't deny that I'm a total nerd. This is what I've got. It seems to work, but I'll leave the testing up to you. I don't know which IDE you're using, so I put in the Code:
system("pause");
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <cctype>
#include <cstdlib>
using namespace std;
int readTime(void);
double calculateCharge(int, int);
int findStall(void);
void park(int, int, string);
int find(string);
int getTimeIn(int);
void unpark(int);
int attChoice(void);
int entry(void);
//Globals
int carTimeIn[10];
string carLicense[10];
int main()
{
double avgCharge =0, totalCharge =0, charge =0;
int totalCars=0, totalTime = 0;
int carsLeft = 0;
int avgTime = 0;
int timeArrive = 0;
int timeExit = 0;
int openStall, exitedStall;
string license;
//initialize arrays
for(int i=0; i<10; i++){
carTimeIn[i]=-1;
carLicense[i] = "";
}
while(true){
int attChoice = entry();
if(attChoice == -1){
// quitting
if(totalCars == 0){
avgCharge = 0;
}
else{avgCharge = totalCharge/(double(totalCars));}
carsLeft = findStall(); //this will return the number of full stalls
if(carsLeft == -1){
carsLeft = 10;
}
if(totalCars == 0){
avgTime = 0;
}
else{avgTime = (totalTime/totalCars);}
cout << "total cars exited: "<< totalCars <<endl;
cout << "total cars remaining: "<< carsLeft <<endl;
cout << "total parking charges: "<< totalCharge<<endl;
cout << "average duration: "<< avgTime <<endl;
cout << "average charge: "<< avgCharge << setprecision(2) << endl;
system("pause");
return (0);
}
else if(attChoice == 1){
openStall = findStall();
if(openStall == -1){
cout << "sorry, lot full";
}
cout << "Enter the license number of the car entering the parking lot" << endl;
cin >> license;
cout << endl;
cout << "Enter the time the car entered the parking lot." << endl;
timeArrive = readTime();
park(openStall, timeArrive, license);
}
else if(attChoice == 0){
cout << "Enter the license number of the car exiting the parking lot" << endl;
cin >> license;
cout << endl;
exitedStall = find(license);
if(exitedStall == -1){
cout << "This vehicle never entered the parking lot" << endl;
system("pause");
return(0);
}
cout << "Enter the time the car exited the parking lot." << endl;
timeExit = readTime();
timeArrive = getTimeIn(exitedStall);
if(timeArrive > timeExit){
cout << "incorrect exit time";
system("pause");
return 0;
}
if (timeArrive >= 1800){
charge = 5;
}
else{
charge = calculateCharge(timeArrive, timeExit);
}
cout << "Vehicle license: " << license << endl;
cout << "Stall Number: " << exitedStall << endl;
cout << "Time of Entry: " << timeArrive << endl;
cout << "Time of Exit: " << timeExit <<endl;
cout << "Charge: " << charge << setprecision(2) <<endl;
totalCars++; //update totals
totalCharge += charge;
totalTime += (timeExit-timeArrive);
}
}
return(0);
}
int entry(void){
char choice;
bool valid = true;
do{
cout << "Please select an action: \"E\" for enter, \"X\" for exit, \"Q\" to quit" << endl;
cin >> choice;
cout << endl;
if(choice=='e' || choice=='E'){
return 1;
}
else if(choice=='x' || choice=='X'){
return 0;
}
else if(choice=='q' || choice=='Q'){
return -1;
}
else{
cout << "invalid selection, please enter again." << endl;
}
}while(valid);
}
int readTime()
{
int totalMin = 0; //total minutes since midnight
int iHour = 0; //value user enters for hour of the day
bool hourOK = false; //boolen to use for looping until the user gets input correct
int iMin = 0; //value user enters for minute of the hour
bool minOK = false; //boolen to use for looping until the user gets input correct
char cAM; //char used to store either A or P for am/pm
bool amOK = false; //boolen to use for looping until the user gets input correct
totalMin = 0; //initialize the total minutes since midnight
//the user will input the hour of the day and we will loop until they get it correct
while (!hourOK)
{
cout << "Enter hour of day: ";
cin >> iHour;
if ((iHour < 0) || (iHour > 12))
cout << "Hour must be between 0 and 12...please try again." << endl;
else
hourOK = true;
}
//the user will input the minutes of the hour and we will loop until they get it correct
while (!minOK)
{
cout << "Enter minute of hour: ";
cin >> iMin;
if ((iMin < 0) || (iMin > 59))
cout << "Minute must be between 0 and 59...please try again." << endl;
else
minOK = true;
}
//the user will input eith A or P (for am/pm) and we will loop until they get it correct
while (!amOK)
{
cout << "Enter A or P (for AM or PM): ";
cin >> cAM;
if ((toupper(cAM) != 'P') && (toupper(cAM) != 'A'))
cout << "You must enter an A or a P...please try again." << endl;
else
amOK = true;
}
if (toupper(cAM) == 'P')
totalMin = ((iHour + 12) * 60) + (iMin);
else
totalMin = (iHour * 60) + (iMin);
cout << "Total minutes since midnight = " << totalMin << endl;
return totalMin;
}
double calculateCharge(int timeIn, int timeOut)
{
int invalidInput = -1;
int totalMinutes = 0;
int totalHours = 0;
int hourDiff = 0;
double totalCharge = 0;
if (timeOut <= timeIn)
return invalidInput;
totalMinutes = timeOut - timeIn;
totalHours = totalMinutes / 60;
if (totalHours <= 4)
{
totalCharge = totalHours * 2.75;
}
else
{
hourDiff = totalHours - 4;
totalCharge = (4 * 2.75) + (hourDiff * 1.75);
}
if (totalCharge < 5.50)
totalCharge = 5.50;
if (totalCharge > 18.00)
totalCharge = 18.00;
return totalCharge;
}
int findStall(void){
for(int i = 0; i<10; i++){
//search through array, find open spot
if(carTimeIn[i] == -1){
return i;
}
}
return -1; //no open spots
}
void park(int stallNumber, int timeIn, string license){
carTimeIn[stallNumber] = timeIn;
carLicense[stallNumber] = license;
}
int find(string license){
for(int i = 0; i<10; i++){
if(carLicense[i] == license){
return i;
}
}
return -1;
}
int getTimeIn(int stall){
return carTimeIn[stall];
}
void unpark(int stall){
carTimeIn[stall] = -1;
carLicense[stall] = "";
}
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
PC Gamer
|
you better rep him LOL
__________________
The Pain Train [AOC COMPETITIVE PVP CLAN]
|
|||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||
|
Programmer
|
O will... every day for like a year... lol... not really... but i would if i could...
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Programmer
|
Ha, I don't do it for rep, I do it because I was in his position once, annoyed as hell at a stupid school project.
And all my opinion edits before, yeah If I was allowed to use/modify those things I suggested it would have gone twice as fast. I mean not being able to use classes/structs/enums/pairs etc... man made that so tedious. Hope it helped!
__________________
Whats this folding I've been hearing about? Crucial Ballistix Club ![]() Member of the OCN Diablo III Club ~M Hail to the Victors M~
|
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||
|
Programmer
|
I have the code running... I'm using Bloodshed. I have no problem using a diff compiler if you recommend a free one... Unless you really thing Visual Studio is really that great... lol...
I'll test it hard and see if i can break it... But so far so good! The gratitude i have for you doing this for me is HUGE! You have no idea how impressed i am that a total stranger help me out so much! On your vacation of all times as well!!! I cannot say thanks enough times... so i hope this smile will do the trick! (jk)
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||
|
|
|
|
#8 (permalink) | |||||||||||
|
Overclocker in Training
|
kind of random, but do you guys know where i can find programs to practice? maybe a site that gives instructions like this, but maybe not as long? i dunno, anything would be cool
__________________
|
|||||||||||
|
|
|
|
|
#9 (permalink) | |||||||||
|
Programmer
|
errr... not really sure where to start... Thats why im taking classes... lol...
I had no idea how to program my way out of a wet paper bag before i started going to school for it... But if you find a good source for it online a person should do alright... The class im taking is all about the online abilities... Assignments and links to help and everything else is all through an online interface called "WebCT" Not sure if this helps you out at all...
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||
|
|
|
|
#10 (permalink) | |||||||||||
|
Overclocker in Training
|
that helps...now i know ill probably have to buy book or something, lol
__________________i took a class 2 years ago, learned alot, but then stopped programming since then. i really don't want to forget all the stuff i learned, it's really helpful to know when you want to go into a career that has something to do with computers
|
|||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|