Программа контактов C ++ - PullRequest
0 голосов
/ 22 апреля 2020

У меня есть вопрос о моей программе, которую я создаю в C ++ Visual Stud ios. Сначала я хочу получить всю информацию об учетных записях, введенную пользователем, а затем отобразить ее обратно пользователю, чтобы убедиться, что она была введена правильно. Затем поместите эту информацию в файл 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:
``` ***

1 Ответ

1 голос
/ 23 апреля 2020

Вам нужно изменить строку прямо под cin >> EmailAdd; на cin.ignore();, затем поставить getline(cin, HomeAddress);, которая будет принимать весь адрес и сохранять все это в переменной HomeAddress, как я хочу. Theres также еще некоторые изменения, которые я внедрил, чтобы сделать его более плавным.

...