C++ 地址簿
C++ Address Book
我试图做到这一点,如果 userselection = 1,则系统会询问用户问题以创建他们的地址簿联系人。它将所有联系信息保存到一个结构中,然后保存到一个 .txt 文件中。我对 C++ 很陌生。这就是我到目前为止所拥有的......我一直在'.'之前收到[Error] expected primary-expression token. <---- 我该如何解决这个问题
另外,任何人都可以提供有关如何将结构保存到文件的指导吗?
谢谢
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
struct person{
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main(){
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == '1');
person newPerson;
cout << "What is your Name?" << endl;
cin >> person.Name;
cout << "What is your Address?" << endl;
cin >> person.Address;
cout << "What is your Phone Number?" << endl;
cin >> person.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> person.Email;
}
对于您描述的错误,您需要访问 class 实例中的成员,而不是 class 定义中的成员..
newPerson.Name
而不是
person.Name
你的错误只与语法有关。以后请阅读编译器的错误消息。
#include <iostream>
#include <string> // added
using namespace std;
struct person {
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main() {
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == 1) { // userselection is int so why compare it to char
person newPerson;
cout << "What is your Name?" << endl;
cin >> newPerson.Name; // assign to object's member not a static member
cout << "What is your Address?" << endl;
cin >> newPerson.Address;
cout << "What is your Phone Number?" << endl;
cin >> newPerson.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> newPerson.Email;
}
}
我试图做到这一点,如果 userselection = 1,则系统会询问用户问题以创建他们的地址簿联系人。它将所有联系信息保存到一个结构中,然后保存到一个 .txt 文件中。我对 C++ 很陌生。这就是我到目前为止所拥有的......我一直在'.'之前收到[Error] expected primary-expression token. <---- 我该如何解决这个问题 另外,任何人都可以提供有关如何将结构保存到文件的指导吗? 谢谢
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
struct person{
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main(){
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == '1');
person newPerson;
cout << "What is your Name?" << endl;
cin >> person.Name;
cout << "What is your Address?" << endl;
cin >> person.Address;
cout << "What is your Phone Number?" << endl;
cin >> person.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> person.Email;
}
对于您描述的错误,您需要访问 class 实例中的成员,而不是 class 定义中的成员..
newPerson.Name
而不是
person.Name
你的错误只与语法有关。以后请阅读编译器的错误消息。
#include <iostream>
#include <string> // added
using namespace std;
struct person {
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main() {
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == 1) { // userselection is int so why compare it to char
person newPerson;
cout << "What is your Name?" << endl;
cin >> newPerson.Name; // assign to object's member not a static member
cout << "What is your Address?" << endl;
cin >> newPerson.Address;
cout << "What is your Phone Number?" << endl;
cin >> newPerson.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> newPerson.Email;
}
}