C++ 联系人文件程序
C++ Contact File Program
我对我在 C++ Visual Studios 中创建的程序有疑问。首先我要做的是获取用户输入的所有关于帐户的信息,然后显示给用户以确保输入正确。然后将该信息放入文件 AccountInformation.txt。到目前为止,我已经完成了所有工作,它从做得很好 cin >> Street
然后它将把接下来的两个 cin 组合在一起,所以我不知道为什么要这样做。这是程序现在运行时的代码和示例输出。
// CreateWriteDisplay.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <fstream>
#include <iostream>
#include <string>
#include <ostream>
using std::cout; //using namespace std is not a good practice **
using std::cin; //it's best to use std:: scope or using only what you need
using std::string; //not the whole namespace, C++17 allows for comma
using std::endl; //separated usings
using namespace std;
// Declares all the variables I need.
int AccountAge;
string LastName;
string FirstName;
string Ocupation;
string UserName;
string EmailAdd;
string HomeAddress;
string TeleNum;
string HomeDirectory;
string RoamingProfile;
string Street;
string City;
string State;
string Zipcode;
string YnRoaming;
//macro definitions for max variable length
#define MAX_NAME_LENGTH 50
#define MAX_ADDRESS_LENGTH 100
#define MAX_ABOUT_LENGTH 200
int main()
{
char name[MAX_NAME_LENGTH], address[MAX_ADDRESS_LENGTH],
about[MAX_ABOUT_LENGTH];
//Gets The Accounts Personal Information: Ocupation, Full Name, Age, Email, Home Address, Telephone Number, UserName, HomeDirectory, RoamingProfile,
cout << "Please enter your Ocupation: " << "\n";
getline(cin, Ocupation);
cout << "Enter Your First Name and Last name: " << "\n";
cin >> FirstName >> LastName;
cout << "Enter Your Age: " << "\n";
cin >> AccountAge;
cout << "Please enter your Email: " << "\n";
cin >> EmailAdd;
cout << "Please Enter Your Home Address( ex: 123(enter) Street( Enter), City Name(Enter), State(Enter), Zipcode(Enter)): " << "\n";
cin >> HomeAddress;
cout << "Enter Street Name" << "\n";
cout << "( ex: StreetName st " << "\n";
cin >> Street;
cout << "Enter City" << "\n" ;
cin >> City;
cout << "Enter State" << "\n";
cin >> State;
cout << "Enter Zipcode" << "\n";
cin >> Zipcode;
cout << "Please Enter Your Best Telephone Number(EX:508-675-4567): " << "\n";
cin >> TeleNum;
cout << "Please Enter Your UserName: " << "\n";
cin >> UserName;
cout << "Please Enter Your Account's HomeDirectory(EX:\HOMEDIRECTORY\): " << "\n";
cin >> HomeDirectory;
cout << "Do you have a Roaming Profile?(Y/N)" << "\n";
cin >> YnRoaming;
if (YnRoaming == "Y") {
cout << "Please Enter Your Roaming Profile Name(IF APPLYS): " << "\n";
cin >> RoamingProfile;
}
else {
string RoamingProfile = "N / A";
}
//getline(cin, HomeAddress);
// Displays All the information entered by the user To verify it was entered correctly.
cout << "Full Name: " << LastName << "," << FirstName << ", Length: " <<
FirstName.length() + LastName.length() << "\n" << "Age: " << AccountAge << "\n"
<< "Ocupation: " << Ocupation << "\n" << "UserName: " << UserName
<< "\n" << "Email Address: " << EmailAdd << "\n"
<< FirstName << "'s Home Address" << HomeAddress << Street << City << State << Zipcode
<< "\n" << " Primary Telephone Number: " << TeleNum << "\n" << UserName << "'s Home Directory: " << HomeDirectory
<< "\n" << UserName << "'s RoamingProfile: " << RoamingProfile << "\n";
// Create File and Write to it then Close it.
ofstream MyFile("AccountInformation.txt");
MyFile << "Full Name: " << LastName << "," << FirstName << ", Length: " << FirstName.length() + LastName.length() << "\n" << "Age: " << AccountAge << "\n"
<< "Occupation: " << Ocupation << ", Length: " << Ocupation.length() << "\n" << "UserName: " << UserName << "\n" << "Email Address: " << EmailAdd << "\n" << FirstName << "'s Home Address"
<< HomeAddress << "\n" << " Primary Telephone Number: " << TeleNum << "\n" << UserName << "'s Home Directory: " << HomeDirectory << "\n"
<< UserName << "'s RoamingProfile: " << RoamingProfile << "\n";
MyFile.close();
return 0;
}
Please enter your Ocupation:
Citizens For Citizens
Enter Your First Name and Last name:
Mark Monhan
Enter Your Age:
24
Please enter your Email:
mmonhan23@gmail.com
Please Enter Your Home Address( ex: 123(enter) Street( Enter), City Name(Enter), State(Enter), Zipcode(Enter)):
524
Enter Street Name
( ex: StreetName st
Street st
Enter City
Enter State
Boston MA
Enter Zipcode
Please Enter Your Best Telephone Number(EX:508-675-4567):
02726 603-854-7845
Please Enter Your UserName:
Please Enter Your Account's HomeDirectory(EX:\HOMEDIRECTORY\):
mgede
Do you have a Roaming Profile?(Y/N)
N
Full Name: Monhan,Mark, Length: 10
Age: 24
Ocupation: Citizens For Citizens
UserName: 603-854-7845
Email Address: mmonhan23@gmail.com
Mark's Home Address524StreetstBostonMA
Primary Telephone Number: 02726
603-854-7845's Home Directory: mgede
603-854-7845's RoamingProfile:
``` ***
您需要将 cin >> EmailAdd;
正下方的行更改为 cin.ignore();
然后输入 getline(cin, HomeAddress);
将获取整个地址并将其全部存储在变量 HomeAddress 中,就像我想要的那样.我还实施了一些其他更改,以使其 运行 更流畅。
我对我在 C++ Visual Studios 中创建的程序有疑问。首先我要做的是获取用户输入的所有关于帐户的信息,然后显示给用户以确保输入正确。然后将该信息放入文件 AccountInformation.txt。到目前为止,我已经完成了所有工作,它从做得很好 cin >> Street
然后它将把接下来的两个 cin 组合在一起,所以我不知道为什么要这样做。这是程序现在运行时的代码和示例输出。
// CreateWriteDisplay.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <fstream>
#include <iostream>
#include <string>
#include <ostream>
using std::cout; //using namespace std is not a good practice **
using std::cin; //it's best to use std:: scope or using only what you need
using std::string; //not the whole namespace, C++17 allows for comma
using std::endl; //separated usings
using namespace std;
// Declares all the variables I need.
int AccountAge;
string LastName;
string FirstName;
string Ocupation;
string UserName;
string EmailAdd;
string HomeAddress;
string TeleNum;
string HomeDirectory;
string RoamingProfile;
string Street;
string City;
string State;
string Zipcode;
string YnRoaming;
//macro definitions for max variable length
#define MAX_NAME_LENGTH 50
#define MAX_ADDRESS_LENGTH 100
#define MAX_ABOUT_LENGTH 200
int main()
{
char name[MAX_NAME_LENGTH], address[MAX_ADDRESS_LENGTH],
about[MAX_ABOUT_LENGTH];
//Gets The Accounts Personal Information: Ocupation, Full Name, Age, Email, Home Address, Telephone Number, UserName, HomeDirectory, RoamingProfile,
cout << "Please enter your Ocupation: " << "\n";
getline(cin, Ocupation);
cout << "Enter Your First Name and Last name: " << "\n";
cin >> FirstName >> LastName;
cout << "Enter Your Age: " << "\n";
cin >> AccountAge;
cout << "Please enter your Email: " << "\n";
cin >> EmailAdd;
cout << "Please Enter Your Home Address( ex: 123(enter) Street( Enter), City Name(Enter), State(Enter), Zipcode(Enter)): " << "\n";
cin >> HomeAddress;
cout << "Enter Street Name" << "\n";
cout << "( ex: StreetName st " << "\n";
cin >> Street;
cout << "Enter City" << "\n" ;
cin >> City;
cout << "Enter State" << "\n";
cin >> State;
cout << "Enter Zipcode" << "\n";
cin >> Zipcode;
cout << "Please Enter Your Best Telephone Number(EX:508-675-4567): " << "\n";
cin >> TeleNum;
cout << "Please Enter Your UserName: " << "\n";
cin >> UserName;
cout << "Please Enter Your Account's HomeDirectory(EX:\HOMEDIRECTORY\): " << "\n";
cin >> HomeDirectory;
cout << "Do you have a Roaming Profile?(Y/N)" << "\n";
cin >> YnRoaming;
if (YnRoaming == "Y") {
cout << "Please Enter Your Roaming Profile Name(IF APPLYS): " << "\n";
cin >> RoamingProfile;
}
else {
string RoamingProfile = "N / A";
}
//getline(cin, HomeAddress);
// Displays All the information entered by the user To verify it was entered correctly.
cout << "Full Name: " << LastName << "," << FirstName << ", Length: " <<
FirstName.length() + LastName.length() << "\n" << "Age: " << AccountAge << "\n"
<< "Ocupation: " << Ocupation << "\n" << "UserName: " << UserName
<< "\n" << "Email Address: " << EmailAdd << "\n"
<< FirstName << "'s Home Address" << HomeAddress << Street << City << State << Zipcode
<< "\n" << " Primary Telephone Number: " << TeleNum << "\n" << UserName << "'s Home Directory: " << HomeDirectory
<< "\n" << UserName << "'s RoamingProfile: " << RoamingProfile << "\n";
// Create File and Write to it then Close it.
ofstream MyFile("AccountInformation.txt");
MyFile << "Full Name: " << LastName << "," << FirstName << ", Length: " << FirstName.length() + LastName.length() << "\n" << "Age: " << AccountAge << "\n"
<< "Occupation: " << Ocupation << ", Length: " << Ocupation.length() << "\n" << "UserName: " << UserName << "\n" << "Email Address: " << EmailAdd << "\n" << FirstName << "'s Home Address"
<< HomeAddress << "\n" << " Primary Telephone Number: " << TeleNum << "\n" << UserName << "'s Home Directory: " << HomeDirectory << "\n"
<< UserName << "'s RoamingProfile: " << RoamingProfile << "\n";
MyFile.close();
return 0;
}
Please enter your Ocupation:
Citizens For Citizens
Enter Your First Name and Last name:
Mark Monhan
Enter Your Age:
24
Please enter your Email:
mmonhan23@gmail.com
Please Enter Your Home Address( ex: 123(enter) Street( Enter), City Name(Enter), State(Enter), Zipcode(Enter)):
524
Enter Street Name
( ex: StreetName st
Street st
Enter City
Enter State
Boston MA
Enter Zipcode
Please Enter Your Best Telephone Number(EX:508-675-4567):
02726 603-854-7845
Please Enter Your UserName:
Please Enter Your Account's HomeDirectory(EX:\HOMEDIRECTORY\):
mgede
Do you have a Roaming Profile?(Y/N)
N
Full Name: Monhan,Mark, Length: 10
Age: 24
Ocupation: Citizens For Citizens
UserName: 603-854-7845
Email Address: mmonhan23@gmail.com
Mark's Home Address524StreetstBostonMA
Primary Telephone Number: 02726
603-854-7845's Home Directory: mgede
603-854-7845's RoamingProfile:
``` ***
您需要将 cin >> EmailAdd;
正下方的行更改为 cin.ignore();
然后输入 getline(cin, HomeAddress);
将获取整个地址并将其全部存储在变量 HomeAddress 中,就像我想要的那样.我还实施了一些其他更改,以使其 运行 更流畅。