查看: 857|回复: 11
|
请问c++ 怎样 string value 要怎样return?
[复制链接]
|
|
class test
{
void setname(??? t )
{
name = t
}
??? getname()
{
return ??
}
};//end class
void main ()
{
string name;
test test1;
cout<<"enter name";
cin>>name;
test.getname();
}
谢谢 |
|
|
|
|
|
|
|
发表于 29-6-2006 11:07 PM
|
显示全部楼层
因为那个变数是private的,可以直接呼叫出来,所以return void就可以了
#include <iostream>
#include <string>
using namespace std;
class mystring {
private:
string m_string;
public:
void setText(string p_string) {
m_string = p_string;
}
void getText() {
cout << m_string << endl;
}
};
int main() {
string sample("Hello World!");
mystring testing;
testing.setText(sample);
testing.getText();
return 0;
} |
|
|
|
|
|
|
|
楼主 |
发表于 1-7-2006 01:30 AM
|
显示全部楼层
oo.原来是这样。谢谢。。
想问问下一个问题。。
我是做一个借书的system..全部data是store进file里面。
我要记录那一笨书的ID每一个月的收入。。。
我应该怎样做?
ADD
......................................
int ID,month;
float rentfee[month];
cout<<"1. Enter book ID: ";
cin>>ID;
cout<<"2. Enter Tenant month(1-12) : ";
cin>>month;
cout<<"2. Enter Tenant Fee : ";
cin>>rentfee[month];
..................................................
display
cout<<ID;cout<<"\t\t\t";
for(int i=0; i<=12; i++)
if(ID=ID)
{
for(int i=0; i<=12; i++);{
cout<<rentfee[month];
}//end inner for
if(aptno!=aptno)
for(int i=0; i<=12; i++);{
cout<<aptno;
cout<<rentfee[month];
}//end for
}//end if
?? 我。。我。。。很模糊。。。可以请教我吗?谢谢。。 |
|
|
|
|
|
|
|
发表于 2-7-2006 01:13 PM
|
显示全部楼层
这算是基本框架,基本上我没有做bounds checking,也没有validate user input,txt file用append方式储存book object,当你下次执行这个程式时,之前的record还会存在
#include <fstream>
#include <iostream>
using namespace std;
class book {
protected:
float fee;
float total;
int bookid;
int month;
public:
void setData() {
cout << "1. Enter Book ID:";
cin >> bookid;
cout << "2. Enter Tenant Month (1-12):";
cin >> month;
cout << "3. Enter Tenant Fee Per Month:";
cin >> fee;
total = fee * month;
}
void getData() {
cout << "1. Book ID: " << bookid << endl;
cout << "2. Tenant Month: " << month << endl;
cout << "3. Tenant Fee Per Month: " << fee << endl;
cout << "4. Total Tenant Fee: " << total << "\n\n";
}
};
int main() {
char input;
book myrecord;
fstream myfile;
myfile.open("bookfile.txt", ios::binary | ios::app | ios::out | ios::in);
cout << "Book Rental System\n\nEnter book record:" << endl;
do {
myrecord.setData();
myfile.write (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
cout << "\nEnter another book record (y/n)?";
cin >> input;
}
while (input == 'y');
myfile.seekg(0);
myfile.read (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
cout << "\n\nBook Rental Info" << endl;
while ( !myfile.eof() ) {
myrecord.getData();
myfile.read (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
}
cout << endl;
return 0;
}
|
|
|
|
|
|
|
|
楼主 |
发表于 2-7-2006 02:48 PM
|
显示全部楼层
谢谢你。。你真的很神。。。
如果我output 是以下 example ...
display interface like
(book) (income)
ID Jan Feb Mar Apr May .......
--------------------------------------
123 12.3 234.1 34 0 0
244 34.2 244 0 0 0
..
...
...
...
..
我应该怎样写??
现我的display 只能。。
ID Jan Feb Mar Apr May .......
--------------------------------------
123 12.3
243 345
123 34.4
我应该怎样set ?
谢谢你。。。 |
|
|
|
|
|
|
|
发表于 2-7-2006 09:10 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 2-7-2006 11:54 PM
|
显示全部楼层
formatting要搞好,不然会走位
- #include <fstream>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class book {
- protected:
- float fee[12];
- int bookid;
- public:
- void setData() {
- cout << "Enter Book ID:";
- cin >> bookid;
- for (int i=0; i<12; i++) {
- cout << "Enter Tenant Fee for Month " << i+1 << ":";
- cin >> fee[i];
- }
- }
- void getData() {
- cout << "\n" << bookid;
- for (int j=0; j<12; j++) {
- cout << setiosflags(ios::fixed)
- << setiosflags(ios::showpoint)
- << setprecision(2)
- << setw(6)
- << fee[j];
- }
- }
- };
- int main() {
- char input;
- book myrecord;
- fstream myfile;
- myfile.open("bookfile.txt", ios::binary | ios::app | ios::out | ios::in);
- cout << "Book Rental System\n\nEnter book record:" << endl;
- do {
- myrecord.setData();
- myfile.write (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
-
- cout << "\nEnter another book record (y/n)?";
- cin >> input;
- }
- while (input == 'y');
- myfile.seekg(0);
- myfile.read (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
- cout << "\n\nBook Rental Info" << "\n\n";
- cout << "ID "
- << setw(6) << "1" << setw(6) << "2" << setw(6) << "3"
- << setw(6) << "4" << setw(6) << "5" << setw(6) << "6"
- << setw(6) << "7" << setw(6) << "8" << setw(6) << "9"
- << setw(6) << "10" << setw(6) << "11" << setw(6) << "12" << endl;
- while ( !myfile.eof() ) {
- myrecord.getData();
- myfile.read (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
- }
- cout << endl;
- return 0;
- }
复制代码
|
|
|
|
|
|
|
|
发表于 3-7-2006 12:27 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 3-7-2006 08:44 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 3-7-2006 09:42 PM
|
显示全部楼层
谢谢。。
但这方法是一次过insert完全部12months =_="
我希望是insert自己所要的月份.
谢谢你的帮忙。
........................................
他说你弄错,可能他要的是.......
void setID(int d)
{
id=d;
}
int getID()
{
return id;
}
cout<<getID();
这类东西。。
[ 本帖最后由 bye_bye 于 3-7-2006 09:43 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 4-7-2006 10:46 PM
|
显示全部楼层
试试这个
- #include <fstream>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class book {
- protected:
- int bookid;
- int month;
- int i;
- float fee[13];
- char choice;
- public:
- book() {
- for (i=1; i<13; i++) {
- fee[i] = 0;
- }
- }
- void getUserInput() {
- cout << "Enter Book ID:";
- cin >> bookid;
- do {
- cout << "Enter a month:";
- cin >> month;
- cout << "Enter Tenant Fee for Month " << month << ":";
- cin >> fee[month];
- cout << "\nAdd more records for this book (y/n)?";
- cin >> choice;
- } while (choice == 'y');
- }
- void displayRecord() {
- cout << "\n" << bookid;
- for (i=1; i<13; i++) {
- cout << setiosflags(ios::fixed)
- << setiosflags(ios::showpoint)
- << setprecision(2)
- << setw(6)
- << fee[i];
- }
- }
- };
- int main() {
- char input;
- book myrecord;
- fstream myfile;
- myfile.open("bookfile.txt", ios::binary | ios::app | ios::out | ios::in);
- cout << "Book Rental System\n\nEnter book record:" << endl;
- do {
- myrecord.getUserInput();
- myfile.write (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
-
- cout << "\nEnter another book record (y/n)?";
- cin >> input;
- }
- while (input == 'y');
- myfile.seekg(0);
- myfile.read (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
- cout << "\n\nBook Rental Info" << "\n\n";
- cout << "ID "
- << setw(6) << "1" << setw(6) << "2" << setw(6) << "3"
- << setw(6) << "4" << setw(6) << "5" << setw(6) << "6"
- << setw(6) << "7" << setw(6) << "8" << setw(6) << "9"
- << setw(6) << "10" << setw(6) << "11" << setw(6) << "12" << endl;
- while ( !myfile.eof() ) {
- myrecord.displayRecord();
- myfile.read (reinterpret_cast<char*>(&myrecord), sizeof(myrecord));
- }
- cout << endl;
- return 0;
- }
复制代码
|
|
|
|
|
|
|
|
楼主 |
发表于 5-7-2006 09:55 PM
|
显示全部楼层
这方法我试过了
但它display 是
insert ID =1
mont = 2
fee = 123
insert ID =2
mont = 1
fee = 456
insert ID =3
mont = 3
fee = 567
ID Jan Feb Mar
1 0.00 123.0 0
2 456.00 0 0
1 0.00 0.00 567
因为会重复两个一样的ID..
我所以我放弃这方法>_<
我尝试compare
if(Id,Book.ID)==0) 方法
但。。。。。
之后是
ID Jan Feb Mar
1 0.00 0 567
2 456.00 0 0
fee 123 没有了T_T |
|
|
|
|
|
|
| |
本周最热论坛帖子
|