David W's Profile
Reputation: 255
Architect
- Group:
- Authors
- Active Posts:
- 1,665 (0.98 per day)
- Joined:
- 20-September 08
- Profile Views:
- 16,652
- Last Active:
Private- Currently:
- Offline
Previous Fields
- Country:
- CA
- OS Preference:
- Windows
- Favorite Browser:
- FireFox
- Favorite Processor:
- Intel
- Favorite Gaming Platform:
- Who Cares
- Your Car:
- Chevrolet
- Dream Kudos:
- 1225
Latest Visitors
-
eig132062 
18 May 2013 - 19:51 -
Zereo 
12 May 2013 - 04:04 -
Java Student 
08 May 2013 - 16:02 -
darek9576 
04 May 2013 - 16:03 -
cplusplusnoob 
02 May 2013 - 01:22 -
jaipurchanchal 
02 May 2013 - 00:11 -
jayburn00 
28 Apr 2013 - 23:42 -
raghav.nagana... 
14 Feb 2013 - 01:40 -
baumd91 
10 Feb 2013 - 09:11 -
Aphex19 
10 Jan 2013 - 05:11
Posts I've Made
-
In Topic: Error in code
Posted 21 May 2013
shash4, on 20 May 2013 - 02:54 AM, said:I have to create the basic reservation system of temple passes. The details are
-3 timings per day(morning,afternoon,evening)
-1000 passes per timing
Can you guys please check the code and tell me how to fix it?
Basically in the case 1 ie. reservation loop, the value of n remains 1000 even in future iterations of the main while loop. I need this value of n to vary depending on how many passes are already booked in the same date and timing.
#include<iostream.h> #include<conio.h> #include<string.h> #include<fstream.h> class passdetails { public: char sex[20],firstname[20],lastname[20],timing[10]; int day,month,year,age; static int cost; void getpassdetails(); void showpassdetails(); }; int passdetails::cost=0;// declaration of static var void passdetails::getpassdetails() { fflush(stdin); cout<<"Fill in the following pass details as displayed on the screen:"; cout<<"\nFirst Name:"; cin>>firstname; cout<<"Last Name:"; cin>>lastname; fflush(stdin); cout<<"Sex(male/female) in small letters only:"; cin>>sex; cout<<"Age:"; cin>>age; if(age>=6) cost+=1500; else cost+=500; cout<<"Date:"; cout<<"\nDay:"<<day; cout<<"Month:"<<month; cout<<"Year:"<<year; cout<<"Time of validity of pass(morning 9am-11am /afternoon 1pm-3pm /evening 5pm-7pm):"<<timing; } void passdetails::showpassdetails() { cout<<"\n TEMPLE PASS "; cout<<"\nFirst Name:"<<firstname; cout<<"\nLast Name:"<<lastname; cout<<"\nSex:"<<sex; cout<<"\nAge:"<<age; cout<<"\nDate:"<<day<<"/"<<month<<"/"<<year; cout<<"\nTiming:"<<timing; if(timing=="morning") cout<<" (9am-11am)"; if(timing=="afternoon") cout<<" (1pm-3pm)"; else cout<<" (5pm-7pm)"; } class reserve { public: int n1,day2,month2,year2; char timing2[10]; void resdetails() { cout<<"\nEnter the date of pass needed:"; cin>>day2>>month2>>year2; cout<<"Enter the timing:"; cin>>timing2; cout<<"Enter the no of passes needed:"; cin>>n1; } }; int main() { int counter=0; char ch='y'; int choice; while(ch=='y') { cout<<"\nTo reserve(1), to cancel (2), to enquire (3), to exit(4):"; cin>>choice; switch(choice) { case 1: { passdetails p1; int n=1000; reserve r1; r1.resdetails(); ifstream f1("res.dat",ios::in); f1.seekg(ios::beg); if(counter!=0) { while(!f1.eof()) { f1.read((char *)&p1,sizeof(p1)); if((r1.day2==p1.day)&&(r1.month2==p1.month)&&(r1.year2==p1.year)) { if(strcmp(r1.timing2,p1.timing)==0) { n=n-1; } } } } if(n>=r1.n1) cout<<"\nThe no of available tickets for that day and time are:"<<n; cout<<"\nThis no of passes are available to be booked congrats:\n"; f1.close(); fstream f2; f2.open("res.dat",ios::in|ios::out|ios::app); while(r1.n1!=0) { counter++; p1.day=r1.day2; p1.month=r1.month2; p1.year=r1.year2; strcpy(p1.timing,r1.timing2); p1.getpassdetails(); f2.write((char *)&p1,sizeof(p1)); r1.n1--; } f2.close(); cout<<"\nDo u want to do something else:(y/n)"; cin>>ch; break; } case 2: { break; } case 3: { break; } } } getch(); return 0; }
Besides the other items already mentioned ...
best not to use conio.h ...
and you have a design redundancy ... (you do NOT need a reserve class) ... see below:
class PassDetails { public: char firstname[MAX_NAME_LEN+1], lastname[MAX_NAME_LEN+1], sex, dummyChar ; int age, timing, year, month, day; int cost, numPasses; PassDetails() : cost(500), numPasses(0) {} // initial pass cost to 500, etc... void takeIn(); void takeInReservationData(); void show() const; } ; // MORE validation needs to be done ... void PassDetails::takeIn() { cout << "\nFill in the following pass details as displayed on the screen.\n"; strcpy( firstname, takeInStr( "First Name : ", MAX_NAME_LEN+1 ) ); strcpy( lastname, takeInStr( "Last Name : ", MAX_NAME_LEN+1 ) ); for( ; ; ) { sex = tolower( takeInChar( "Sex (m/f) : " )); if( sex == 'm' || sex == 'f' ) break; // else .... cout << "\nPlease note: \n" << "\"In the beginning God created male and female " << "in His own image and likeness ...\"\n"; } age = takeInInt( "Age : ", 1, 1000 ); if( age >= 6 ) cost += 1000; } void PassDetails::takeInReservationData() { for( ;; ) { cout << "\nEnter the pass date (y m d ) : "; if( cin >> year >> month >> day && year > 2012 && cin.get() == '\n' && month > 0 && month < 13 && day > 0 && day < 32 ) { cout << "Enter the time (1->morning or " << "2->afternoon or 3->evening) : "; if( cin >> timing && cin.get() == '\n' && ( timing == 1 || timing == 2 || timing == 3 ) ) { cout<<"Enter the no of passes needed: "; if( cin >> numPasses && cin.get() == '\n' && numPasses > 0 ) { break; } else cout << "\nNumber of passes requested must be " << "greater than 0 ...\n"; } else cout << "\nOnly 1 or 2 or 3 valid here ...\n"; } else { cout << "\nValid dates are greater than today ... \n"; cin.clear(); while( cin.get() != '\n' ); // flush cin stream ... } } } -
In Topic: difference between getline and gets()?
Posted 16 May 2013
jimblumberg, on 16 May 2013 - 07:55 AM, said:Quote
AND NOTE: you can't use C strings with getline ...
Sure you can, as long as you use the proper version of getline(). There are two versions of getline() one for use with std::string and one for use with C-strings.
The thing to remember with C-strings in to never use a function where you don't limit the number of characters you can retrieve into the string. This includes the scanf() family and the C++ extraction operator>>. These methods have ways to limit the number of characters you accept into the string to avoid buffer overruns, the width modifier and the setw() manipulator.
Quote
There are two (obvious) ways of solving this problem.
1) Limit the number of characters the user can input
2) Move the input data away from the stack (which std::string does)
Really there is only one safe solution. Which you list is reason 1. Just using dynamic memory doesn't actually solve the buffer overrun problem, it just moves the problem from the stack to the heap. Buffer overflow in either the stack or heap is a problem that must be repaired.
Jim
Thanks for all the clarifications, Jim ... I forgot about the C string versions of getline - I never use them, is probably why ;(
But the OP needs to hear the truth ... Thanks for bringing it forward
-
In Topic: Temple Pass Reservation reserving question
Posted 16 May 2013
-
In Topic: difference between getline and gets()?
Posted 16 May 2013
Praveen Kumar 28061994, on 14 May 2013 - 01:40 PM, said:it is mentioned in some sites that using gets() is not preferred
but i felt that is as useful as getline
can you kindly tell me the differences and the advantages that one has on the other
If you want to understand how getline works with a C++ string ...
you can think of it like this:
it reads the whole line and discards any newline char(s) at the end
into a dynamically allocated C++ string of just the right size to hold the line (at run time)
which string memory is freed up when the string goes out of scope
but ...
gets uses a C string buffer that has memory of pre-fixed size, and that size was specified at compile time.
So ... if the line you are reading has more bytes in it than the C string pre-fixed size buffer can hold ...
it will OVERFLOW the buffer as already mentioned above.
About the only thing the same between
gets (In C)
and
getline( in C++ )
is that they both will 'eat' the newline char(s) at the end of the line (if there).
If you need to use C strings, and you wish to use a function in C like getline (getline is in C++ AND NOTE: you can't use C strings with getline ... getline is a function that returns a string of just the right (dynamic memory) size to hold a line being read in)
you can code your own function to do that in C style code ...
or use a function like my readLine freely available in the DIC snippets
or at this link:
readLine.h -
In Topic: incompatible arguement type with int and int*
Posted 16 May 2013
breezett93, on 16 May 2013 - 12:26 AM, said:Alright, I managed to get rid of the error. One final thing is that I'd like my output to be somewhat organized and setup in rows and columns.
Something like this:
data data data data
data data data data
etc, so that there are 4 rows and 5 columns.
I do know I need #include<iomanip> and the "fixed" position. but it still does not look correct.
I think you meant to say:
I do know I need #include<iomanip> and the "setw( field_width )"
My Information
- Member Title:
- DIC supporter
- Age:
- 65 years old
- Birthday:
- June 29, 1947
- Gender:
-
- Location:
- Toronto Ontario Canada
- Interests:
- The events that lead up to the soon return of Yeshua (Jesus) ... and being ready
- Full Name:
- David Wayne Zavitz
- Programming Languages:
-
Basic
6502 Assembly
Comal
C++
HLA (High Level Assembly)
Pascal
C
Python
Contact Information
- E-mail:
- Click here to e-mail me
- Website URL:
-
http://sites.google.com/site/andeveryeyeshallseehim/
|
|


Find Topics
Find Posts
View Reputation Given




|
Comments
Invincible-99
29 Jun 2012 - 06:10That link doesn't work.
http://i48.tinypic.com/34qqrg3.jpg
Invincible-99
29 Jun 2012 - 06:06[IMG]http://i48.tinypic.com/34qqrg3.jpg[/IMG]
Anarion
24 Jun 2012 - 13:44v0rtex
07 Jun 2012 - 03:04caleb123
22 Jun 2010 - 19:37You and I can give each other tips and answer question to make us both better at designing websites.
skyhawk133
08 Jun 2010 - 09:43xxxpriya
20 Feb 2010 - 21:25