需要帮助了解如何向用户询问他们想要删除的内容,并将其删除到阵列中
Need help figuring out how to ask a user for what they want to remove and removing it in an array
我有一个程序用于管理一个名为"client_DB"的记录数据库。数组"client_DB"由客户的手机通话记录组成。每个客户呼叫记录包含八个字段,如下所示:1)十位数的手机号码(字符串,无破折号),2)呼叫中使用的中继站数量(整数),3)以分钟为单位的呼叫长度(整数);4)呼叫的净成本(双倍);5)税率(双倍),6)呼叫税(双倍),7)调用的总成本(双)和8)名为"discount_aval"的字符串字段,值为"yes"或"no"。阵列client_DB的容量(SIZE)为20条记录。
它从一个名为"client_data.txt"的输入文件中读取,该文件由以下值组成:
9546321555 0 0 yes
5612971340 5 50 no
3051234567 8 25 no
7542346622 24 17 no
3054432762 15 30 yes
9544321011 50 100 yes
8776219988 87 82 yes
9042224556 4 5 yes
7877176590 11 1 no
5617278899 20 45 no
9546321555 4 3 yes
5612971340 79 86 no
3051234567 8 25 no
7542346622 24 118 no
3054432762 115 25 yes
9544321011 43 10 yes
8776219988 265 22 yes
9042224556 2 5 yes
7877176590 89 67 no
5617278899 40 56 no
我几乎已经完成了这项工作,但我需要关于如何编写几行代码以及它们的去向的帮助。我需要实现一个菜单,但我需要帮助如何询问用户要删除哪个电话号码,并为REMOVE功能删除该号码,以及询问要搜索哪个电话号码并返回其在search功能中的位置。我还需要帮助设置打印到输出文件"weekly_client_call_info.txt"的精度。我知道我需要实现这个:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
但是我该把它放在哪里呢?此外,我很想得到关于如何将列标题添加到输出文件中的提示。有一个叫做Process的函数可以进行计算,我如何设置这些计算的精度?我所理解的一切,谢谢!到目前为止,我的代码如下:(如果我在如何正确提问方面犯了任何错误,我很抱歉!)
#include <iostream>
#include <string>
#include <fstream>
//************************************************************************
//Name: Kevin Dunphy Due Date: 022113
//Instructor: Dr. Bullard Total Points: 100 pts
//Assignment2: client_call.cpp UsIDFAU: kdunphy
//:
using namespace std;
const int CAPACITY = 20;
class client_db
{
public:
string cellnum;
int numofrelay;
int call_length;
double net_cost;
double tax_rate;
double call_tax;
double total_cost;
string discount_aval;
};
bool IsFull(int); //returns true if the array is full; otherwise false.
bool IsEmpty(int count);// returns ture if the array is empty; otherwise false.
void Add(client_db A[], int & count, client_db & db);
void Remove(client_db A[], int *count, string name);// removes an item from the array if it is there
void Print_DB(client_db A[], int count);//prints to output file
void Call_stats(client_db A[], int count);// prints all the items in the array
int Search_DB(client_db A[], int count, string name); //if the name is in the array, its location is returned
// //otherwise return -1;
//
bool IsFull(int count)
////Description: Determines if the array is full
{
return (count == CAPACITY);
}
bool IsEmpty(int count)
////Description: Determines if the array is empty
{
return (count == 0);
}
void Process (client_db A[], int count)
{
for(int i=0; i<count; i++)
{
if (A[i].numofrelay >=1 && A[i].numofrelay<=5)
{
A[i].tax_rate=0.01;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);
}
else if (A[i].numofrelay >=6 && A[i].numofrelay<=11)
{
A[i].tax_rate=0.03;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);
}
else if (A[i].numofrelay>=12 && A[i].numofrelay<=20)
{
A[i].tax_rate=0.05;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].numofrelay);
}
else if (A[i].numofrelay >=21 && A[i].numofrelay<=50)
{
A[i].tax_rate =0.08;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);
}
else if (A[i].numofrelay >50)
{
A[i].tax_rate =0.12;
A[i].net_cost = ((A[i].numofrelay / 50.0)*0.40*A[i].call_length);
}
A[i].call_tax = ((A[i].tax_rate)/(100))*(A[i].net_cost);
A[i].total_cost = A[i].net_cost + A[i].call_tax;
}
}
void Print_DB(client_db A[], int count)
//Description: Prints the items stored in A to the standard i/o device
{
string filename;
cout<<"Enter output filename: "; //geting filename
cin>>filename;
ofstream output; //declaring an output file stream
output.open(filename.c_str()); // c_str() converts a C++ string into a
// c-style string (char array) &
//open binds an ofstream to a file
for(int i=0; i<count; i++)
{
output<<A[i].cellnum<<"t"
<<A[i].numofrelay<<"t"
<<A[i].call_length<<"t"
<<A[i].net_cost<<"t"
<<A[i].tax_rate<<"t"
<<A[i].call_tax<<"t"
<<A[i].total_cost<<"t"
<<A[i].discount_aval<<endl;
}
output.close();
}
int Search(client_db A[], int count, string cellnum)
////Description: Locates cellnumbers in A's fields
{
for(int i=0; i<count; i++)
{
if (cellnum == A[i].cellnum)
{
return i;
}
}
return -1;
}
void Add(client_db A[], int &count)
////Description: Adds key to the array
{
if (!IsFull(count))
{
cout<<"Enter a cellphone number, number of relay stations and the call lenght and if a discount is available: ";
cin>>A[count].cellnum>>A[count].numofrelay>>A[count].call_length>>A[count].discount_aval;
count++;
}
else
{
cout<<"The list is fulln";
}
}
void Add(client_db A[], int &count, client_db &db)
////Description: Adds key to the array
{
if (!IsFull(count))
{
A[count] = db;
count++;
}
else
{
cout<<"The list is FULL! n";
}
}
void Remove(client_db A[], int *count, string cellnum )
////Description: Removes the number from the array is it is there
{
cout<<"Remove function is called and removed the cellphone number 9546321555 t" <<endl;
int loc = Search(A,*count,cellnum);
if (IsEmpty(*count))
{
cout<<"A is EMPTY!n";
return;
}
else if (loc == -1)
{
cout<<"key not in An";
}
else
{
for(int j=loc; j<(*count)-1; j++)
{
A[j] = A[j+1];
}
(*count)--;
}
}
void Call_stats(client_db A[],int count) // prints to screen
{
for(int i=0; i<count; i++)
{
cout<<A[i].cellnum<<"t"
<<A[i].numofrelay<<"t"
<<A[i].call_length<<"t"
<<A[i].discount_aval<<endl;
}
}
void Menu ()
{
cout<<"The values of the filename you entered have been recognized"<<endl;
cout<<"Please enter the letter of your application of choice"<<endl;
cout<<" "<<endl;
cout<<"************ WELCOME TO THE MAIN MENU ************"<<endl;
cout<<" Add an item...........................A"<<endl;
cout<<" Remove an item........................R"<<endl;
cout<<" Search for an item....................S"<<endl;
cout<<" Print current data....................P"<<endl;
cout<<" Print to output file..................O"<<endl;
cout<<"****************************************************"<<endl;
}
int main()
{
char answer;
char answer2;
client_db CLIENT[CAPACITY]; //declaring database
int count = 0; //initializing count
string filename;
cout<<"Hello!, this program holds clients call data records."<<endl;
cout<<"Enter input filename: "; //geting filename
cin>>filename;
ifstream input; //declaring an input file stream
input.open(filename.c_str()); // c_str() converts a C++ string into
while(count<CAPACITY && !input.eof()) //reading until the end of the file (eof=end-of-file)
{
input>>CLIENT[count].cellnum
>>CLIENT[count].numofrelay
>>CLIENT[count].call_length
>>CLIENT[count].discount_aval;
count++;
}
do
{
Menu();
cout<<"Please enter a command letter: "<<endl;
cin>>answer;
client_db db;
switch (answer)
{
case 'A' :
cout<<"Enter a cellphone number, number of relay stations and the call lenght and if a discount is available: "<<endl;
cin>>db.cellnum>>db.numofrelay>>db.call_length>>db.discount_aval;
Add(CLIENT, count, db);
break;
case 'R' : //Remove function goes here
break;
case 'S' : // SEARCH function goes here
break;
case 'P' : Call_stats(CLIENT,count);
break;
case 'O' :
Process(CLIENT,count); //how do i set the precision for this?
Print_DB(CLIENT,count);
break;
}
cout<<"Would you like to make another command?(y/n): "<<endl;
cin>>answer2;
} while (answer2 == 'Y' || answer2 == 'y');
cout<<"Goodbye"<<endl;
return 0;
}
在您的输出函数中
void Call_stats(client_db A[],int count) // prints to screen
{
cout.setf(ios::fixed, ios::floatfield);
cout << setprecision(3); // precision of 3
}
至于search-and-remove方法,您需要从用户那里获得int形式的数字例如cin>>数如果用户int==list int使用适当的参数调用remove函数,则循环浏览数字列表
相关文章:
- 从阵列C++中删除重复项
- 为什么删除分配的阵列会导致内存错误?
- 重新分配2D阵列并删除旧阵列
- 删除阵列时出现分段错误
- 阵列的临时存储如何用于C++中的删除恢复?
- 将C 中的每个指针删除作为阵列的指针安全吗?
- 唯一/共享 ptr 用于在数组超出范围后自动删除阵列
- C 在指针阵列中删除第一个元素,以效应后来的元素
- 在阵列C 中删除和转移剩余的元素
- 阵列旋转并删除
- 删除指向排序的字符串阵列的指针会在运行后会有一个错误
- C 对象阵列删除一个
- 复制动态阵列并使用过载的操作员删除原件
- C 删除并在2D动态阵列中删除和紧凑的行
- 在JNI呼叫中使用后,应删除/释放JarrayObject(字符串阵列)
- 删除阵列与指针
- 删除char阵列指针触发了一个神秘的断点
- 删除阵列错误
- 3D阵列删除C++的性能较慢
- 从1D阵列中删除多个引用