Project question :D

Started by
4 comments, last by zell901 16 years, 2 months ago
well, I'm doing another project and this time I can't quite figure out every part of it by myself :(. I have everything going perfect, but it's more of a "I just want to know how to do this for future reference question" The project is to make a police ticket program that calculates the speed/money and stuff. It works and everything but this one line of code I want to know how to change if I ever have to someday...where asks you to put [1/0], how do I change that to make it work with [Y/N] AND [y/n]?? In case you forget to capitalize it, so you don't have to re-do the entire thing? ***(my full source code is down below the first source thing if you need that)*** I tried for awhile after I finished the project but I couldn't figure it out for the life of me.
[Source]
void SchoolZone()
{
	
	std::cout << "\n\nWas the vehical speeding in a schoolzone?" << std::endl;
	std::cout << "[1/0]: ";  // 1 = yes, 0 = no
	std::cin >>  yesNoSchool;

	if (yesNoSchool == 1) // if car was speeding in school zone, option 1
	{
		std::cout << "\n\nThe vehical was speeding in a school zone.";
		TicketYes(); //calls TicketYes function (diffrent fines)
	}
	else //if not speeding in school zone, option 2
	{
		std::cout << "\n\nThe vehical was not speeding in a school zone.";
		TicketNo(); //calls ticketNo function (diffrent fines)
	}
}
[/Source]
[/source][/source][/source][/source] --you could grade my project if you have some time to burn, since I'm teaching myself and don't have a teacher to do it for me :)--
[Source]
#include <iostream> 
#include <conio.h> 

//prototypes
void VehicalSpeed(); 
void SpeedLimit(); 
void SchoolZone(); 
void TicketYes();      //yes schoolzone ticket function
void TicketNo();       //no schoolzone ticket function

//Global vars
int speed;             //vehical speed
int speedLimit;        //road speed limit
int yesNoSchool;       // yes/no school zone
int rSpeedCharge = 30; // regular speeding charge
int x; // miles over/under the speed limit

//main function
int main()
{
	SpeedLimit();   // calls SpeedLimit function
	VehicalSpeed(); // calls VehicalSpeed funtion

	getch(); //stops console from flashing by.
	return 0; // I still gotta look this up sometime...
}

//Function info
void SpeedLimit() 
{
	std::cout << "**********SPEEDING TICKET**********" << std::endl << std::endl;
	std::cout << "\nWhat is the speed limit of the area: "; //prompts for speed limit
	std::cin >> speedLimit;
}

void VehicalSpeed()
{
	std::cout << "How fast was the vehical going: "; //prompts for vehical speed
	std::cin >> speed;
	
	
	if (speed > speedLimit) //if the speed is greater than limit, option 1
	{
		x = speed - speedLimit; //assigns x to vehical speed - speedlimit
		
		std::cout << "\n\nOver the speed limit by " << x << " miles" << std::endl;
		SchoolZone(); //calls the SchoolZone function
	}
	else //if speed was less than limit, option 2 starts
	{
		x = speedLimit - speed; // assigns x to speed limit - vehical speed

		std::cout << "\n\nUnder the speed limit by " << x << " miles" << std::endl;
	}
}

void SchoolZone()
{
	
	std::cout << "\n\nWas the vehical speeding in a schoolzone?" << std::endl;
	std::cout << "[1/0]: ";
	std::cin >>  yesNoSchool;

	if (yesNoSchool == 1) // if car was speeding in school zone, option 1
	{
		std::cout << "\n\nThe vehical was speeding in a school zone.";
		TicketYes(); //calls TicketYes function (diffrent fines)
	}
	else //if not speeding in school zone, option 2
	{
		std::cout << "\n\nThe vehical was not speeding in a school zone.";
		TicketNo(); //calls ticketNo function (diffrent fines)
	}
}

void TicketYes()
{
	std::cout << "\n\nRegular speed ticket fine: " << rSpeedCharge << std::endl;
	std::cout << "Extra charge per MPH: " << 6.00 << std::endl;
	std::cout << "\nFinal Ticket Charge: " << (rSpeedCharge + (x * 6.00)) 
		<< std::endl; // ^^figures out and adds up the numbers
}

void TicketNo()
{
	std::cout << "\n\nRegular speed ticket fine: " << rSpeedCharge << std::endl;
	std::cout << "Extra charge per MPH: " << 3.00 << std::endl;
	std::cout << "\nFinal Ticket Charge: " << (rSpeedCharge + (x * 3.00))
		<< std::endl; // ^^figures out and adds up the numbers
}
[/Source]
[/source][/source][/source][/source]
Advertisement
Do something like:

char ch;std::cin >> ch;if (ch == '1' || ch == 'Y' || ch == 'y')...


The || is OR. If any of those conditions are true, the if statement will execute. Does that help at all?
Yes it did, thanks a bunch.

I was getting the || part wrong, instead of...
char = '1' || char = 'Y' || char = 'y'

I was doing...
char = 1 || 'Y' || 'y', didnt know i had to put the variable in there every time.

:\ oopsie, thanks though.
you can also use the toupper and tolower keywords. I forget the exact syntax but you can find them online.
Quote:Original post by Chrono1081
you can also use the toupper and tolower keywords. I forget the exact syntax but you can find them online.


Those are not keywords, but functions. You can use them if you #include <cctype>.

Quote:Original post by zell901
--you could grade my project if you have some time to burn, since I'm teaching myself and don't have a teacher to do it for me :)--


Your project looks pretty good for a beginner, but there are some issues. First of all, you should not be using all these global variables. For such a small project as this, you will not have much problems because of it, but in a larger project it will be terrible to do any debugging when you have a lot of functions that modify global variables and could potentially thread at each others feet.

Instead, you should make your functions take parameters and do returns. This:
void TicketYes(){	std::cout << "\n\nRegular speed ticket fine: " << rSpeedCharge << std::endl;	std::cout << "Extra charge per MPH: " << 6.00 << std::endl;	std::cout << "\nFinal Ticket Charge: " << (rSpeedCharge + (x * 6.00)) 		<< std::endl; // ^^figures out and adds up the numbers}


Should be this:

int TicketYes(int rSpeedCharge, int x) // x should have a more useful name{    int ticket = rSpeedCharge + x * 6;    std::cout << // ...    return ticket; // Return the cost to the caller so he knows}


Or something to that effect, and most of your functions that use global variables could be written better by doing the same change.

Otherwise it looks pretty good :)

[Edited by - qebab on February 10, 2008 5:12:46 AM]
Are there any free eBooks about managing your code? I notice in comparison to most people my source is really cluttered :\

This topic is closed to new replies.

Advertisement