after i tried many times to solf this program i couldnt do it
this is the sorce of my program and it is about sell and rent a care.
my problem is when i run do progrm it works but when i try to see the car i want buy or rent cant . i created text file for rent and sell and it has all information about all cars and also i created anther file for member to save username and password.
2- i want to add a fuction for rent that multuple a days that user choose and the price of car???
plz help me and i will be thankfull to all
i hope you got it ???why it is not display cars ???it show me 0 0 0 for all cars??
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
struct member // This structure is made for the member
{
string member_name; // Each member will have a member's name
string member_pass; // password and
double balance; // balance
}; // This will make things easier for organizing the data
struct item // This structure is made for the items
{
int id; // Each item will have an id
char name[100]; // name and
double price; // price
}; // This will make things easier for organizing the data
//After making the structures then we initialize the needed variables as types of the structures
item personal_cars[20]; // Initialize personal cars variable
item rent_cars[10]; // Initialize rent cars variable
item cart[10]; // Initialize cart variable
member members[20]; // Initialize members variable
//Initializing these global variables to 0 is necesary each time the program is run
int position=0; // Position is a counter that will stop at the user that logs in
int size=0; // Size will determine the position of the item, i.e if size is at 3 then there are 3 items in cart
int counter=0; // Counter will count the total number of members
fstream log_member; // Initializes file for both reading and writing
//These are the user defined functions
void intro();
void start();
void login();
void new_member();
void options();
void view_account();
void view_cart();
void add_to_cart(char);
void check_out();
void drop();
void order();
void logout();
void display_items(char);
void Add_to_balance();
void receipt(double total);
void update();
//*********************************************************************************************************************
//############################## MAIN FUNCTION ##################################################
int main()
{
intro();
start();
options();
system("PAUSE");
return 0;
}
//############################## END MAIN ###################################################
//*********************************************************************************************************************
//The start function Prints out the Welcome screen and gives the user his first choices in the program
//The choices chosen are other functions thenselves
void intro()
{
cout <<endl;
cout << " ***************************************************************************" <<endl;
cout << " * <<<Welcome to SMW Company>>> *\n" <<endl;
cout << " * This program takes your to buy a car that you are looking for *" << endl;
cout << " * and returns the answer to the screen. If an illegal operator is *" << endl;
cout << " * selected, an error message will be displayed. Be careful which *" <<endl;
cout << " * number you select because the program depends on you for input. *" <<endl;
cout << " * The only error check function it has, is for unacceptable opreators. *" <<endl;
cout << " * Acceptable operators are : (1 , 2 , 3 and 4). The character c sets *" <<endl;
cout << " * the value stored to ZERO. ENJOY YOUR PROGRAM !!!!!! *" <<endl;
cout << " ***************************************************************************" <<endl;
cout <<endl;
}
void start()
{
char choice;
cout<<"\n\n1. New Account Sign Up\n2. Login\n"<<endl;
cin >> choice;
if(choice=='1')
new_member();
else if(choice=='2')
login();
else
{
cout<<"Invalid input"<<endl;
start();
}
}
//The new user function as implied in the name adds a new user to the record
void new_member()
{
fstream log_member; //Initializing file
log_member.open ("members.txt"); // opening a file
int i=0; //Loop used to read all existing members into memory
while(log_member>>members[i].member_name && log_member>>members[i].member_pass && log_member>>members[i].balance)
{
i++;
}
//20 is the max number of members, so if i is >= 20 then that means there is no more space, but hopefully not :)
if(i<20)
{
cout<<"Enter new Username (Max 12 Characters) : ";
cin >> members[i].member_name;
cout<<"Enter new Password (Max 10 Characters) : ";
cin >> members[i].member_pass;
members[i].balance=0; //Initializes new user balance to RM 0
position=i;
counter=position;
log_member.close();
}
else
{
cout<<"Maximum limit of members reached"<<endl;
}
}
//Login function logs in an existing member by reading the member name and password,
//and then checking it with existing member names and passwords
void login()
{
fstream log_member; //Initilizing and opening a file
char member_name[12],password[10]; //Initializing arrays to store member name and password
position = -1;
cout<<"Member's name : ";
cin >> member_name;
cout<<"Password : ";
cin >> password;
log_member.open("members.txt");
//This loops reads throuhg all existing member names and pasword sets,
//and if any set mathces the one entered then it allocates to variable position
//and then continues the loop till it reads all
for (int i = 0; log_member>>members[i].member_name && log_member>>members[i].member_pass && log_member>>members[i].balance; i++)
{
if(members[i].member_name == member_name && members[i].member_pass == password)
{
position = i;
}
else
{
position = position;
}
}
if(position>=0) //This checks to see if the member has been found
{
log_member.close();
}
else //This alerts the user that the details he/she entered were not found
{
cout<<"Incorrect username or password!"<<endl; //cout<<"Position: "<< position <<endl;
log_member.close();
start();
}
log_member.close();
}
void options() //Options functions comes after login and it gives the member his next options
{
char option;
cout<<"\n\nWelcome to your account "<<members[position].member_name<<endl;
cout<<"\nChoose an option :"<<endl;
cout<<"\n1. View Account Summary\n2. Order\n3. View Shopping Cart\n4. Logout\n"<<endl;
cin >> option;
switch (option) //Switch is used to select his chosen option
{
case '1':
view_account();
break;
case '2':
order();
break;
case '3':
view_cart();
break;
case '4':
logout();
break;
default:
cout<<"Invalid input"<<endl;
options();
}
}
//View account function gives the member some options to look into his/her account details like balance, Add_to_balance money and loging out
void view_account()
{
char option;
cout<<"\n1. Balance\n2. Add to my balance\n3. Return to Previous Menu\n4. Logout"<<endl;
cin >> option;
switch (option)
{
case '1':
{
cout<<"\nYour Available Balance is RM "<< members[position].balance <<endl;
view_account();
}
break;
case '2':
{
Add_to_balance();
}
break;
case '3':
{
options();
}
break;
case '4':
{
logout();
}
break;
default:
{
view_account();
}
}
}
//Add_to_balance function adds money to the member existing balance
//We assume the member has a valid third party bank account
//So all that is need to be done is to specify the amount and add to
//his/her balance
void Add_to_balance()
{
float Add_to_balancement=0;
cout<<"The amount : ";
cin>>Add_to_balancement;
members[position].balance += Add_to_balancement;
cout<<"The Add_to_balancement has been successful added :)"<<endl;
cout<<members[position].member_name<<" current balance is RM "<< members[position].balance <<endl;
view_account();
}
void order()
{
//Gives member options to choose whic list to view
char option, choice;
cout<<"\nChoose which list you would like to view"<<endl;
cout<<"\n1. Buy car\n2. Rent car\n3. Return to Previous Menu"<<endl;
cin >> option;
//The option chosen is passed to another function used to display the items
display_items(option);
cout<<"1.Add item to the order\n2.Retun to previous menu"<<endl;
cin >> choice;
if (choice == '1')
{
//Option chosen passed on to another function to add to cart
add_to_cart(option);
}
else
{
//Directs to the view the list option
order();
}
//After a member adds a item then he/she is immediately shown his/her cart
view_cart();
}
//view_cart function shows the member his/her cart
void view_cart()
{
char choice;
cout<<"\n"<<members[position].member_name<<" item(s) :\n"<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"ID"<<"\t\t"<<" Item Name"<<"\t\t\t"<<"Price"<<endl;
cout<<"**********************************************************************"<<endl;
for (int i = 0; i < size; i++) //This loop prints out all items in cart
{
cout<<cart[i].id<<"\t\t"<<cart[i].name<<"\t\t"<<cart[i].price<<endl;
}
//More options avaliable since use may want to check out right away
cout<<"\n1.Checkout Item(s)\n2.Drop item(s)\n3.Add item(s)\n4.Return to previous menu"<<endl;
cin >> choice;
switch (choice)
{
case '1':
check_out();
break;
case '2':
drop();
break;
case '3':
order();
break;
case '4':
options();
break;
default:
cout<<"Invalid input"<<endl;
view_cart();
}
}
//Drop function drops last item added, since we are storing added items to cart in arrays
//we have only enough knowledge to drop the last one entered
void drop()
{
//Since variable 'size' it what we use to store the number of elements in the cart array
//all we need to to is rollback one step in order to remove the last item
size -= 1;
cout<<"You last item has been dropped succesfully :)"<<endl;
options();
}
//Display items function displays the items the member wants,
//the function is passed a value which it acts on to show the required item
void display_items(char option)
{
switch (option)
{
case '1':
{
//Initializes file for reading n writing
ifstream personal_cars_file ("personal cars.txt");
for(int i=0; i<20; i++) //loops through the whole file
{
//Stores the first value before a space in the line as 'id'
personal_cars_file>>personal_cars[i].id;
//Stores the next after the space as name until it reaches a tab
personal_cars_file.get(personal_cars[i].name, 100 ,'\t');
//Then stores the next as price
personal_cars_file>>personal_cars[i].price;
//It then prints out what it just read to the screen
cout<<personal_cars[i].id<<"\t"<<personal_cars[i].name<<"\t"<<personal_cars[i].price<<endl;
}
personal_cars_file.close(); //Closes the file
}
break;
//Same as above happens on the rest, exept it does it with different files
case '2':
{
int days;
cout<<"How many day(s) you want to rent the car?"<<endl;
cin>>days;
fstream rent_cars_file ("rent_cars.txt");
for(int i=0; i<10; i++)
{
rent_cars_file>>rent_cars[i].id;
rent_cars_file.get(rent_cars[i].name, 100 ,'\t');
rent_cars_file>>rent_cars[i].price;
cout<<rent_cars[i].id<<"\t"<<rent_cars[i].name<<"\t"<<(rent_cars[i].price)*days<<endl;
}
rent_cars_file.close();
}
break;
case '3':
options();
break;
default:
{
cout<<"\nInvalid Input"<<endl;
order();
}
}
}
void add_to_cart(char option) //This function adds a selected item to the cart
{ //It is passed a value which determines which list the member is refereing to
int id;
//The if statement is a validity checker that check if the cart is full or not
if (size < 10)
{
switch (option)
{
case '1':
{
cout<<"Enter the item ID: ";
cin >> id;
cart[size] = personal_cars[id-1]; //This maps the selected item to the cart
size++;
cout<<"Your item has been added to "<<members[position].member_name<<" cart successfully :)"<<endl;
}
break;
case '2':
{
cout<<"Enter the item ID: ";
cin >> id;
cart[size] = rent_cars[id-1];
size++;
cout<<"Your item has been added to "<<members[position].member_name<<" cart successfully :)"<<endl;
}
break;
default:
cout<<"Invalid input"<<endl;
order();
}
}
//This means the cart is full so it alerts the member
else
{
cout<<"Cart full"<<endl;
order();
}
}
//Checkout is like buying the item(s)
void check_out()
{
char choice;
double total = 0;
for (int i = 0; i < size; i++) //This loop sums the total of the item(s) added to the cart
{
total += cart[i].price;
}
if ( total > members[position].balance) //This statement checks whether the member has enouhgh balance
{
cout<<"\n\nSorry you have insufficient funds to continue the transaction"<<endl;
cout<<"\n\n1.Add_to_balance\n2.Drop last item\n3.Order\n4.Logout"<<endl;
cin >> choice;
switch (choice)
{
case '1':
Add_to_balance();
break;
case '2':
drop();
break;
case '3':
order();
break;
case '4':
logout();
break;
default:
cout<<"Invalid input"<<endl;
check_out();
}
}
else
{
members[position].balance -= total; //It deducts the total sum of the items from his/her balance
cout<<members[position].member_name<<" cart have been checked out successfully :)"<<endl;
cout<<"The remaining balance is : "<< members[position].balance <<endl;
receipt(total);
update();
size=0;
counter=0;
position=0;
for (int i = 0; i < size; i++)
{
cart[i].id=('0');
}
main();
}
}
//Update function writes all the edited information of the member back to the text file
void update()
{
fstream log_member;
log_member.open ("members.txt");
//Loop used to get to the member info
for (int i = 0; i <= position; i++)
{
log_member<< members[i].member_name <<" ";
log_member<< members[i].member_pass <<" ";
log_member<< members[i].balance <<" ";
}
log_member.close();
}
void receipt(double total)
{
cout<<"\nPrinting "<<members[position].member_name<<" receipt.."<<endl;
cout<<"***********************************************************************"<<endl;
cout<<"\t\t\tSMW Company\n";
cout<<"\t\t TEL: +60173784877"<<endl;
cout<<"\t\t IIUM, Gombak"<<endl;
cout<<"----------------------------------------------------------------------"<<endl;
cout<<"ID"<<"\t\t"<<" Car Name"<<"\t\t\t"<<"Price"<<endl;
cout<<"----------------------------------------------------------------------"<<endl;
for (int i = 0; i < size; i++)
{
cout<<cart[i].id<<"\t\t"<<cart[i].name<<"\t\t"<<cart[i].price<<endl;
}
cout<<"***********************************************************************"<<endl;
cout<<"Cart Total\t\t\t\t"<<total<<endl;
cout<<"Tax 2%\t\t\t\t\t"<<(total*2)/100<<endl;
cout<<"Total\t\t\t\t\t"<<(total)+((total*2)/100)<<endl;
cout<<"\t\tTHANK YOU "<<members[position].member_name<<"\n\tIT WAS OUR PLEASURE SERVING YOU\n";
}
//Logout finally updates the member info and then exits the program
void logout()
{
update();
size=0;
counter=0;
position=0;
for (int i = 0; i < size; i++)
{
cart[i].id=('0');
}
cout<<"\nYou have succesfully logged out\n\n\n\n"<<endl;
main();
}