我的函数不从其他函数接管变量值

imre_talpa

我有一个程序可以在文件中创建电话簿。我的某些功能无法正常工作或无法正常工作。

用户名

#pragma once
#include <string>
#include <vector>
using namespace std;
class User
{private:
   string firstname, lastname, country, city, street;
   string phone;
public:
    string prefix;
  void ReadAllUsers(User[], int&);
  void SaveUser(User, int&);
  void SaveToFile(const User[], int);
  void AddName(User[], int&);
  void ListAllUsers(const User[], int&);
  void Prefix(User, int);
  void ChangePhone(User[], int&);
  void Help();
  void DeleteUser(User[], int&);
  bool Search(string x) 
{
return (phone.find(x) != string::npos);
}
};

用户.cpp

#include "User.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#pragma warning(disable:4996)
using namespace std;

const string PHONEBOOK_FILENAME = "phonebook.txt";

void User::Help()
{cout<<"\nWELCOME TO THE APPLICATION!\n";
 cout<<"Press 0 to display on the screen all records that are saved in the file(phonebook.txt)\n";
 cout<<"Press 1 to add 1 or more new record(s) in file(phonebook.txt)\n";
 cout<<"Press 2 to delete permanently a record from file(phonebook.txt)\n";
 cout<<"Press 3 to sort users from file(phonebook.txt) by name and display them on the screen\n";
 cout<<"Press 4 to edit a user phone number and save it after in file(phonebook.txt)\n";
 cout<<"Press 5 for help\n";
 cout<<"Press 6 to exit the application\n";
}

void User::ReadAllUsers(User people[], int &num_people)

{
    ifstream f;

    f.open(PHONEBOOK_FILENAME.c_str());

    if (f.fail())
{
  cout << "Unable to open file " << endl;
  return ;
}
    int i = 0;

    while (!f.eof() && i < 100)

    {   getline(f, people[i].firstname);
        getline(f, people[i].lastname);
        getline(f, people[i].phone);
        getline(f, people[i].country);
        getline(f, people[i].city);
        getline(f, people[i].street);
        i++;
    }
num_people = i;

f.close();

}

//Add country prefix to the phone number
void User::Prefix(User person, int num_people)
{string filecountry;
ifstream f;
    f.open("prefix.txt");
    {
    while (getline(f, filecountry))
  {
      if (person.country == filecountry )
      {
         f.ignore();//next line
         f >> person.prefix;
        }
    }
f.close();
  }
}
void User::SaveUser(User person, int &num_people)
{
ofstream f(PHONEBOOK_FILENAME.c_str(), ios::app ) ;
    if (f.fail())
    cout << "Unable to open file " << endl;
        else
        f << person.firstname << " " <<  person.lastname << " " << person.country <<  " " << person.city << " " << person.street << " " << person.prefix << "-" << person.phone << endl;
cout << "\nThe user was added\n";
}
//Save data after a modification or after a user delete
void User::SaveToFile(const User people[], int num_people)

{ofstream f;
f.open(PHONEBOOK_FILENAME.c_str());

 for(int i = 0; i < num_people; i++)

    {

        f << people[i].firstname << " " << people[i].lastname << " " <<  people[i].country << " " << people[i].city << " " << people[i].street << " " << people[i].prefix << " " << people[i].phone << endl;
}
}
// Read user data from the keyboard, add a new contact to the array
void User::AddName(User people[],int &num_people)
{User person; 
    cout <<"Enter the user's first name: ";
    cin >> person.firstname;

    cout <<"Enter the user's last name: ";
    cin >> person.lastname;

    cout <<"Enter the user's country: ";
    cin >> person.country;

    cout <<"Enter the user's city: ";
    cin >> person.city;

    cout <<"Enter the user's street: ";
    cin >> person.street;

    cout <<"Enter the user's phone number: ";
    cin >> person.phone;

    Prefix(person, num_people);

    cout <<"The prefix is " << person.prefix;

        for(int i = 0; i < num_people; i++)

    {

        if( i + 1  == num_people)

            people[num_people] = person;

    }
SaveUser(person, num_people);

   num_people++;

}
// Ask the for person's name to change, find the person in the array and
// change it to the new phone number.  Then save the new data to file by
// calling SaveToFile.
void User::ChangePhone(User people[], int &num_people)
{
User person;
int count;

cout <<"Enter name to change: ";
cin >> person.firstname;

for(count = 0; count < num_people; count++)

    {

        if(people[count].Search(person.firstname))

        {   cout <<endl<< people[count].firstname<<endl;

cout <<"Current number"<<people[count].phone;
cout << "\nNew number: ";

cin >> people[count].phone;

SaveToFile(people,num_people);
cout <<"\n\nNew number Saved.";
return;
 }
}
if(count = num_people)

        cout <<"\nName not found.\n";

}

void User::DeleteUser(User people[], int &num_people)
{string phone;
int count = 0;
ifstream f;
f.open("phonebook.txt");
cout << "Input the phone of user that you want to delete ";
cin >> phone;

for(count = 0; count < num_people; count++)

    {

        if(people[count].Search(phone))

        {   cout <<endl<< people[count].phone<<endl;
people[count].firstname = people[count].lastname = people[count].phone = people[count].country = people[count].city = people[count].street = " ";
        }
SaveToFile(people,num_people);
cout <<"\n\nUser deleted.";
return;}
f.close();
}

函数 Prefix()(这是为电话号码自动添加国家前缀,这是从文件中读取的)正在工作,但 person.prefix 的值不是由 SaveUser() 获取的,因此该值未写入文件.SaveToFile() 函数将所有用户保存在文件的单行中。并且函数 ChangePhone() 和 DeleteUser 不起作用。

沙克逊

问题是,void User::Prefix(User person, int num_people)需要person通过,所以它使人的副本,改变副本,那么副本消失函数结束时。原作从未改变。

相反,您想要:

void User::Prefix(User & person, int num_people)

为了有一个参考指定的人,这将是你让函数内部的变化而受到影响。

另外,我建议您将 saveuser 更改为:

void User::SaveUser(User const & person, int &num_people) const

只是为了避免制作 User 对象的额外副本,但我认为您目前拥有它的方式并没有错。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从其他函数中获取变量值 Python

来自分类Dev

从函数获取变量值

来自分类Dev

Twilio函数-变量值

来自分类Dev

从函数获取变量值

来自分类Dev

保留变量值的Python递归函数

来自分类Dev

函数不改变变量值

来自分类Dev

获取在函数内部创建的变量值

来自分类Dev

变量值未跨函数更新

来自分类Dev

变量值未在函数内部传递

来自分类Dev

Swift变量值和函数

来自分类Dev

通过函数设置窗口变量值

来自分类Dev

在函数外访问变量值

来自分类Dev

在函数名中使用变量值

来自分类Dev

如何将两个变量值计算为不同的函数,并在php中将输出显示为其他函数?

来自分类Dev

根据R中的其他变量分配变量值

来自分类Dev

将变量值设置为基于其他变量

来自分类Dev

PHP编辑变量值可编辑其他变量值

来自分类Dev

我的变量值更改了,如何相应地更新我的函数?

来自分类Dev

我无法在函数中获取配置变量值作为参数

来自分类Dev

为什么我的包含函数无法获取全局变量值?

来自分类Dev

服务器主机可以看到我的后端代码的函数变量值吗?

来自分类Dev

为什么我不能在 cookie.get 函数之外使用 cookie 变量值?

来自分类Dev

在JavaScript中将函数变量值传递给新变量

来自分类Dev

将$ scope变量值传递给工厂变量/函数

来自分类Dev

如何在jquery中的函数外获取变量值

来自分类Dev

使用当前变量值动态创建Javascript函数

来自分类Dev

变量值在函数调用后更改,不使用

来自分类Dev

在函数内部更改全局变量值

来自分类Dev

javascript范围:在函数后保留全局变量值