我正在寫一個c + +代碼。
在這段代碼中,我首先調用函數GetOption(),但它什麼也沒有。
任何人都可以幫助我嗎?
其他函數有其他目的,但我不確定它是否會影響它。因此我將它粘貼!
下面是我的代碼:我打電話功能,但它沒有打印任何字
#include<iostream>
#include<iomanip>
#include<fstream>
#include<boost/regex.hpp>
#include"ContactRec.h"
using namespace std;
using namespace boost;
int GetOption();
int main(){GetOption();}
ContactRec GetContactInfo()
{
ContactRec id;
ofstream myoutfile("directory.txt",ios::app);
regex reg_email("^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$");
regex reg_tel("^\\(?(\\d{3})\\)?-?(\\d{3})-(\\d{4})$");
cout<<"enter your name."<<endl;
cin.ignore(1024,'\n');
getline(cin,id.Name);
cout<<"enter your email."<<endl;
cin>>id.Email;
while (regex_match(id.Email,reg_email)==0)
{
cout<<"data wrong!!!!!!"<<endl;
cin>>id.Email;
}
cout<<"enter your telephone numbers. EX:012-111-1111"<<endl;
cin>>id.Phone;
while(regex_match(id.Phone,reg_tel)==0)
{
cout<<"data wrong!"<<endl;
cin>>id.Phone;
}
cout<<"Please enter the type(0)Family,(1)Friend,(2)Other"<<endl;
int p;
cin>>p;
id.Type=(ContactType)p;
myoutfile<<setiosflags(ios::left)<<setw(30)<<id.Name;
myoutfile<<setiosflags(ios::left)<<setw(30)<<id.Email;
myoutfile<<setiosflags(ios::left)<<setw(30)<<id.Phone;
myoutfile<<setiosflags(ios::left)<<setw(30)<<id.Type<<endl;
myoutfile.close();
return id;
}
void PrintContactInfo(int type)
{
ContactRec person;
ifstream myinfile;
myinfile.open("directory.txt",ios::in);
cout<<setiosflags(ios::left)<<setw(30)<<"name";
cout<<setiosflags(ios::left)<<setw(30)<<"email";
cout<<setiosflags(ios::left)<<setw(30)<<"telephone";
cout<<setiosflags(ios::left)<<setw(30)<<"type"<<endl;
string temp;
if (type==-1)
{
while (myinfile)
{
getline(myinfile,temp);
cout<<temp<<endl;
}
}
else if(type==0)
{
while (myinfile)
{
getline(myinfile,temp);
if (temp[70]=='0')
cout<<temp<<endl;
}
}
else if(type==1)
{
while (myinfile)
{
getline(myinfile,temp);
if (temp[70]=='1')
cout<<temp<<endl;
}
}
myinfile.close();
}
int GetOption()
{
int number=0;
while(number!=5)
{
cout<<"(1)Add a new record"<<endl;
cout<<"(2)Display the all contacts."<<endl;
cout<<"(3)Display the contacts of family. "<<endl;
cout<<"(4)Display the contacts of friends."<<endl;
cout<<"(5)Exit."<<endl;
cin>>number;
if (number==1)
GetContactInfo();
if (number==2)
PrintContactInfo(-1);
if (number==3)
PrintContactInfo(0);
if (number==4)
PrintContactInfo(1);
}
}
下面是ContactRec.h
#include <string>
#include"ContactType.h"
using namespace std;
struct ContactRec
{
string Name;
string Phone;
string Email;
ContactType Type;
};
ostream& operator<<(ostream& out,const ContactRec id)
{
out<<id.Name;
out<<id.Email;
out<<id.Phone;
out<<id.Type;
}
下面是ContactType.h
enum ContactType{Family,Friend,Other};
您應該在'while'循環中更改您的支票,因爲用戶可以輸入-1或6,都是!= 5. – 2013-04-28 18:24:03
當您使用調試器時,哪一行導致問題? – 2013-04-28 18:25:24
請不要粘貼所有的代碼,只有相關的代碼。刪除不相關的內容實際上可以幫助您在發佈之前找到問題。 – interjay 2013-04-28 18:27:00