![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Apr 2008
Posts: 4
| BUT now i need to make a user input based program instead of test program like what i've done. Its like, the user can input the receiver and sender name and informations. I thought this would be the easiest part but damn.. i'm trying everything i know trying to link the >>cin to the constructors above but none are working. Everything giving me error. Again and again.. errors.. errors.. errors.. ![]() Please help me. Any idea how to do it ? An example on how to create a user input for atleast the sender's name in this program would be very very much appreciated. The rest i can do it myself. Thanks. Code: #include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Package
{
public:
// constructor initiliazes data members
Package( const string &, const string &, const string &,
const string &, int, const string &, const string &, const string &,
const string &, int, double, double );
void setSenderName( const string & );
string getSenderName() const;
void setSenderAddress( const string & );
string getSenderAddress() const;
void setSenderCity( const string & );
string getSenderCity() const;
void setSenderState( const string & );
string getSenderState() const;
void setSenderZIP( int );
int getSenderZIP() const;
void setRecipientName( const string & );
string getRecipientName() const;
void setRecipientAddress( const string & );
string getRecipientAddress() const;
void setRecipientCity( const string & );
string getRecipientCity() const;
void setRecipientState( const string & );
string getRecipientState() const;
void setRecipientZIP( int );
int getRecipientZIP() const;
void setWeight( double );
double getWeight() const;
void setCostPerOunce( double );
double getCostPerOunce() const;
virtual double calculateCost() const;
private:
// data members to store sender and recipient's address information
string senderName;
string senderAddress;
string senderCity;
string senderState;
int senderZIP;
string recipientName;
string recipientAddress;
string recipientCity;
string recipientState;
int recipientZIP;
double weight; // weight of the package
double costPerOunce; // cost per ounce to ship the package
};
//-----------------------------------------------------------------------------------------------------------//
class OvernightPackage : public Package
{
public:
OvernightPackage( const string &, const string &, const string &, const string &, int,
const string &, const string &, const string &, const string &, int, double, double, double );
void setOvernightFeePerOunce( double );
double getOvernightFeePerOunce() const;
virtual double calculateCost() const;
private:
double overnightFeePerOunce; // fee per ounce for overnight delivery
}; // end class OvernightPackage
//-----------------------------------------------------------------------------------------------------------//
// OvernightPackage constructor
OvernightPackage::OvernightPackage( const string &sName, const string &sAddress, const string &sCity,
const string &sState, int sZIP, const string &rName, const string &rAddress, const string &rCity,
const string &rState, int rZIP, double w, double cost, double fee ) : Package( sName, sAddress, sCity,
sState, sZIP, rName, rAddress, rCity, rState, rZIP, w, cost )
.
..
...
//-----------------------------------------------------------------------------------------------------------//
class TwoDayPackage : public Package
{
public:
TwoDayPackage( const string &, const string &, const string &, const string &, int,
const string &, const string &, const string &, const string &, int, double, double, double );
void setFlatFee( double );
double getFlatFee() const;
virtual double calculateCost() const;
private:
double flatFee;
};
//-----------------------------------------------------------------------------------------------------------//
// TwoDayPackage constructor
TwoDayPackage::TwoDayPackage( const string &sName, const string &sAddress,
const string &sCity, const string &sState, int sZIP, const string &rName, const string &rAddress,
const string &rCity, const string &rState, int rZIP, double w, double cost, double fee )
: Package( sName, sAddress, sCity, sState, sZIP, rName, rAddress, rCity, rState, rZIP, w, cost )
.
..
...
//-----------------------------------------------------------------------------------------------------------//
// Package constructor initiliazes data members
Package::Package( const string &sName, const string &sAddress, const string &sCity, const string &sState,
int sZIP, const string &rName, const string &rAddress, const string &rCity, const string &rState, int rZIP,
double w, double cost ) : senderName( sName ), senderAddress( sAddress ), senderCity( sCity ),
senderState( sState ), senderZIP( sZIP ), recipientName( rName ), recipientAddress( rAddress ),
recipientCity( rCity ), recipientState( rState ), recipientZIP( rZIP )
.
..
...
//-----------------------------------------------------------------------------------------------------------//
int main()
{
//This is the test program
OvernightPackage NightDeliver("Micheal", "Dr Rajkumar Road", "Rajajinagar Stage II",
"Bangalore", 12345, "Mathan", "Manjunath Road", "Berhampur", "Orissa", 54321, 16.00, 1.00, .25 );
TwoDayPackage DayDeliver("Kamal", "Amravati Road", "Nagpur", "Maharashtra", 33333,
"Rajan", "Vijayawada Road", "Ansari Nagar", "New Delhi", 44444, 10.5, .65, 2.0 );
cout << fixed << setprecision(2);
cout << "Customers for overnight delivery\n";
cout << "\n";
cout << "The sender :\n" << NightDeliver.getSenderName()<< "\n";
cout << NightDeliver.getSenderAddress() << "\n";
cout << NightDeliver.getSenderCity() << " " << NightDeliver.getSenderState() << " "
<< NightDeliver.getSenderZIP() << "\n";
cout << "\n";
cout << "The recipient :\n" << NightDeliver.getRecipientName()<< "\n";
cout << NightDeliver.getRecipientAddress() << "\n";
cout << NightDeliver.getRecipientCity() << " " << NightDeliver.getRecipientState() << " "
<< NightDeliver.getRecipientZIP() << "\n";
cout << "The cost is $ " << NightDeliver.calculateCost() << "\n";
cout << "\n\n";
cout << "Customers for 2 days delivery\n";
cout << "\n";
cout << "The sender :\n" << DayDeliver.getSenderName()<< "\n";
cout << DayDeliver.getSenderAddress() << "\n";
cout << DayDeliver.getSenderCity() << " " << DayDeliver.getSenderState() << " "
<< DayDeliver.getSenderZIP() << "\n";
cout << "\n";
cout << "The recipient :\n" << DayDeliver.getRecipientName()<< "\n";
cout << DayDeliver.getRecipientAddress() << "\n";
cout << DayDeliver.getRecipientCity() << " " << DayDeliver.getRecipientState() << " "
<< DayDeliver.getRecipientZIP() << "\n";
cout << "The cost is $ " << DayDeliver.calculateCost() << "\n";
cout << "\n\n";
system ("pause");
return 0;
}
Last edited by chandreu; 04-25-2008 at 02:28 PM. Reason: Re-edited(removed some part of the code) again to protect from code theft by lazy arses. |
| chandreu is offline | |
| | #2 |
| Registered User Join Date: Jan 2008
Posts: 575
| Er... I didn't see a 'std::cin' anywhere. What are you trying? And also, can you edit your post and break the long lines? Your making my laptop cry. Soma |
| phantomotap is offline | |
| | #3 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| I'm thinking that your package class could do with having the "where to send" and "where from" split out into a separate "address" class. You would then have two instances of that, sender and recipient in the package class, rather than repeating a bunch of member variables with sender and recipient prefixes. A further consequence of this would be that you could write a funciton to display an address, making your overall code a lot simpler. You may even find that you can deal with address data completely independently from the package class, and just pass in a sender and receiver address into the constructor, taking the number of arguments to the constructor down to something manageable. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #4 |
| Afraid of widths Join Date: Apr 2008 Location: Chicago
Posts: 887
| You have created perfectly named member functions. There's no need to adorn them with superfluous comments. I'd lose the comments at the end of every function, too. Code: // return sender's ZIP code
int Package::getSenderZIP() const
{
return senderZIP;
} // end function getSenderZIP
|
| medievalelks is offline | |
| | #5 | |||
| Registered User Join Date: Apr 2008
Posts: 4
| Thanks for the replies. I edited my code as requested. Quote:
You wont find the 'std::cin' anywhere coz i'm using the namespace std therefore i don't need to declare the "std::cin" seperately. What i'm trying to ask here: Now you can see the constructor for each function TwoDayPackage and overNightPackage it goes on like OvernightPackage (const string &sName, const string &sAddress, const string &sCity, const string &sState, int sZIP.......) So, when i build my test program in int main() below, i simply create an object under that function and enter values that follow the same arrangement i made above (name, address, city, state, zip code.....) Now, what im trying to ask is, how to make user input the values into the function instead of declaring it in the program itself. For example, cout>> "please enter sender name : "; cin>>sName; <----- sName refering to sender name in the OverNightPackage Ofcoz it doesn't work simply this way as i'll definately will get "sName undeclared" (i'm just trying to give you a good picture about what i want my program to do). But there must be some way to achieve this thru other method. And all i'm asking for is this unknown method, how to do it ? Quote:
Quote:
I have 3 days remaining to complete this assignment.. plus another subject assignment.. gotta model a warrior character in 3ds max.. 4 days to deadline and i'm still figuring out what texture would fit on his butt... ;( | |||
| chandreu is offline | |
| | #6 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| You can create extra variables to read the values into. Code: string sName; cin >> sName; OvernightPackage(sName, ...);
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #7 |
| Registered User Join Date: Apr 2008
Posts: 4
| |
| chandreu is offline | |
| | #8 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: string sName; cin >> sName; -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #9 |
| Registered User Join Date: Apr 2008
Posts: 4
| Hey guys... its finally working the way i want it. Great thanks to Mats and CornedBee. When i read CornedBee, i didn't get what he meant TILL only after reading Mats short reply, it "clickED" at the back of my head !!! Tried it and works like wonders !!!! Thanks CornedBee !!!! Thanks Mats !!! This is the full user input code i did based on CornedBee's simple example. Code:
void ChoiceOvernight()
{
string sName, sAddress, sCity, sState;
int sZIP;
string rName, rAddress, rCity, rState;
int rZIP;
double w, cost, fee;
ofstream outfile("OvernightData.txt",ios::ate);
cout<<"\t\t\t------------------------------------"<<endl;
cout<<"\t\t\t Over Night Package Delivery "<<endl;
cout<<"\t\t\t------------------------------------"<<endl<<endl;
cout <<"Please enter sender name, address, city, state, zip, : "<<endl;
cin >> sName >> sAddress >> sCity >> sState >> sZIP;
cout <<"Please enter receiver name, address, city, state, zip : "<<endl;
cin >> rName >> rAddress >> rCity >> rState >> rZIP;
cout <<"Please enter the weight of the package : "<<endl;
cin >> w;
OvernightPackage NightDeliver(sName, sAddress, sCity, sState, sZIP, rName,
rAddress, rCity, rState, rZIP, w, cost, fee );
cout << fixed << setprecision(2);
cout << "Information status for overnight delivery\n";
cout << "\n";
cout << "The sender :\n" << NightDeliver.getSenderName()<< "\n";
cout << NightDeliver.getSenderAddress() << "\n";
cout << NightDeliver.getSenderCity() << " " << NightDeliver.getSenderState() <<
" " << NightDeliver.getSenderZIP() << "\n";
cout << "\n";
cout << "The recipient :\n" << NightDeliver.getRecipientName()<< "\n";
cout << NightDeliver.getRecipientAddress() << "\n";
cout << NightDeliver.getRecipientCity() << " " << NightDeliver.getRecipientState() <<
" " << NightDeliver.getRecipientZIP() << "\n";
cout << "The cost is $ " << NightDeliver.calculateCost() << "\n";
outfile<<" Customers Information Status for Two Day Delivery"<<"\n"<<sName<<"\n"<<sAddress<<"\n"<<sCity<<" "<<sState<<" "<<
sZIP<<"\n\n"<<rName<<"\n"<<rAddress<<"\n"<<rCity<<" "<<rState<<" "<<
rZIP<<"\n\n"<<"Package Weight: "<<w<<"KG"<<"\n"<<"Total Charges: "<<
"RM"<<cost<<"\n\n\n";
outfile.close();
system("PAUSE");
system("cls");
}
//-----------------------------------------------------------------------------------------------------------//
void ChoiceTwoDay()
{
string sName, sAddress, sCity, sState;
int sZIP;
string rName, rAddress, rCity, rState;
int rZIP;
double w, cost, fee;
ofstream outfile("TwoDayData.txt",ios::ate);
cout<<"\t\t\t------------------------------------"<<endl;
cout<<"\t\t\t Two Day Package Delivery "<<endl;
cout<<"\t\t\t------------------------------------"<<endl<<endl;
cout <<"Please enter sender name, address, city, state, zip, : "<<endl;
cin >> sName >> sAddress >> sCity >> sState >> sZIP;
cout <<"Please enter receiver name, address, city, state, zip : "<<endl;
cin >> rName >> rAddress >> rCity >> rState >> rZIP;
cout <<"Please enter the weight of the package : "<<endl;
cin >> w;
TwoDayPackage DayDeliver(sName, sAddress, sCity, sState, sZIP, rName,
rAddress, rCity, rState, rZIP, w, cost, fee );
cout << fixed << setprecision(2);
cout << "\nInformation status for two days delivery\n";
cout << "\n";
cout << "The sender :\n" << DayDeliver.getSenderName()<< "\n";
cout << DayDeliver.getSenderAddress() << "\n";
cout << DayDeliver.getSenderCity() << " " << DayDeliver.getSenderState() <<
" " << DayDeliver.getSenderZIP() << "\n";
cout << "\n";
cout << "The recipent :\n" << DayDeliver.getRecipientName()<< "\n";
cout << DayDeliver.getRecipientAddress() << "\n";
cout << DayDeliver.getRecipientCity() << " " << DayDeliver.getRecipientState() << " " << DayDeliver.getRecipientZIP() << "\n";
cout << "The cost is $ " << DayDeliver.calculateCost() << "\n";
cout << "\n";
outfile<<" Customers Information Status for Two Day Delivery"<<"\n"<<sName<<"\n"<<sAddress<<"\n"<<sCity<<" "<<sState<<" "<<
sZIP<<"\n\n"<<rName<<"\n"<<rAddress<<"\n"<<rCity<<" "<<rState<<" "<<
rZIP<<"\n\n"<<"Package Weight: "<<w<<"KG"<<"\n"<<"Total Charges: "<<
"RM"<<cost<<"\n\n\n";
outfile.close();
system("PAUSE");
system("cls");
}
int main()
{
int target=0;
double amount;
char opt='x';
while(opt=='x'|| opt=='X')
{
cout<<" 1.--> [ Over Night Delivery ] ";
cout<<" 2.--> [ Two Day Delivery ] ";
cout<<" 3.--> [ EXIT ] ";
cout<<endl;
cout<<"\t\t\tEnter Selection from (1 - 3) ==>";
cin>> target;
cout<<endl;
if (target==1)
{
system("cls");
ChoiceOvernight();
}
else if (target==2)
{
system("cls");
ChoiceTwoDay();
}
else if (target==3)
{
exit(0);
}
}
}
Thanks to you guys, now i can go back to my 3Ds Max and start editing my warrior's butt... Last edited by chandreu; 04-25-2008 at 03:01 PM. |
| chandreu is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| can someone help me with these errors please code included | geekrockergal | C Programming | 7 | 02-10-2009 02:20 PM |
| Client-server system with input from separate program | robot-ic | Networking/Device Communication | 3 | 01-16-2009 03:30 PM |
| SSH Hacker Activity!! AAHHH!! | Kleid-0 | A Brief History of Cprogramming.com | 15 | 03-06-2005 03:53 PM |