搜索数组或显示数组时没有输出结果
我正在创建一个列表,允许我输入数据然后搜索或显示输入的数据。由于某种原因,我无法产生输出。问题可能出在输出函数上。我该如何解决这个问题?这是代码:
#include<iostream>
#include<string>
#include<cstdlib>
#include <stdio.h>
#include <string.h>
using namespace std;
class contact{
private:
string fn;
string ln;
string email;
string number;
public:
// Accessor and Mutator method for contacts variable
void setfname(string f_n);
string getfname();
void setlname(string l_n);
string getlname();
void setemail(string emaila);
string getemail();
void setnumber(string num);
string getnumber();
// takes input entered for contacts
void input();
// display output
void output();
contact();
contact(string f_n, string l_n, string emaila,string num);
};
void menu();
void EnterContact(contact contacts[], int size, int& numberUsed);
void show(contact contacts[], int sizeOfa);
int search_contacts(contact contacts[], string& lastname, int& size);
int main(){
string opt="";
const int MAX=2;
int size;
contact contacts[MAX];
contact c[MAX];
for(int i=0; i<MAX; i++){
EnterContact(contacts, MAX, size);
}
menu();
return 0;
}
//use these variables globally
const int MAX = 2;
contact contacts[MAX];
void EnterContact(contact contacts[], int size, int& numberUsed)
{
char ans;
bool done = false;
int index = 0;
cout << "Enter up to " << size << endl;
while ((!done) && (index < size))
{
contacts[index].input();
cout << "Do you want to add another contact?(y/n followed by Return): ";
cin >> ans;
if ((ans == 'y') && (ans == 'Y'))
index++;
else
done = true;
}
numberUsed = index+1;
}
int search_contacts(contact contacts[], string& lastname, int& size)
{
int index = 0;
bool found = false;
for(int index=0; index<size ; index++){
if (lastname == contacts[index].getlname()){
found = true;
cout<<"found";
break;
}
}
if (!found)
cout<<"no";
return index;
}
void show(contact contacts[], int sizeOfa)
{
for (int index = 0; index < sizeOfa; index++)
contacts[index].output();
}
void menu()
{
cout<<"\nContact Menu"<<endl;
cout << "1. Add a New Contact. " << endl;
cout << "2. Search for a Contact. " << endl;
cout << "3. Delete Contact from list. " << endl;
cout << "4. View All Contacts. " << endl;
cout << "5. Exit the program. " << endl;
cout << "Enter your choice: ";
int opt; int result, a; char ans; string lastname;
cin>>opt;
switch (opt)
{
case 1: cout<<"func to Add a New Contact"<<endl;
cout<<"\nAdd another contact";
break;
case 2:
do
{
cout << "\nSearch contact by lastname: ";
cin >> lastname;
result = search_contacts(contacts, lastname, result);
if (result == -1)
cout << lastname << " Contact not found.\n";
else
contacts[result].output();
cout << lastname << " is stored in array position "<< result << endl;
cout << "Search again? (y/n): ";
cin >> ans;
} while ((ans != 'n') && (ans != 'N'));
menu();
break;
case 3: cout<<"func to Delete Contact from list";
break;
case 4: cout<<"\nAll Contacts"<<endl;
show(contacts, MAX);
cout<<endl;
menu();
break;
case 5: cout<<"\nContact Book is closed"<<endl;
exit(1);
break;
default: cout<<"\nInvalid entry...Closing Contact Book"<<endl;
}
}
contact::contact()
{
fn=""; ln=""; email=""; number="";
}
contact::contact(string f_n, string l_n, string emaila,string num)
{
fn= f_n; ln= l_n; email= emaila;number= num;
}
void contact::input()
{
cout<<"\nFirst Name: ";
cin>>fn;
cout<<"Last Name: ";
cin>>ln;
cout<<"Email: ";
cin>>email;
cout<<"Phone number: ";
cin>>number;
}
void contact::output()
{
cout<<"\nName: "<<fn<<" "<<ln;
cout<<"\nEmail: "<<email;
cout<<"\nPhone number: "<<number;
cout<<endl;
}
void contact::setfname(string f_n)
{
fn= f_n;
}
string contact::getfname();
{
return fn;
}
void contact::setlname(string l_n)
{
ln= l_n;
}
string contact::getlname()
{
return ln;
}
void contact::setemail(string emaila)
{
email= emaila;
}
string contact::getemail()
{
return email;
}
void contact::setnumber(string num)
{
number= num;
}
string contact::getnumber()
{
return number;
}
I'm creating a list that allows me to input data then search or display the inputted data. For some reason I'm not able to produce an output. The problem may be the output function. How can i fix this? Here is the code:
#include<iostream>
#include<string>
#include<cstdlib>
#include <stdio.h>
#include <string.h>
using namespace std;
class contact{
private:
string fn;
string ln;
string email;
string number;
public:
// Accessor and Mutator method for contacts variable
void setfname(string f_n);
string getfname();
void setlname(string l_n);
string getlname();
void setemail(string emaila);
string getemail();
void setnumber(string num);
string getnumber();
// takes input entered for contacts
void input();
// display output
void output();
contact();
contact(string f_n, string l_n, string emaila,string num);
};
void menu();
void EnterContact(contact contacts[], int size, int& numberUsed);
void show(contact contacts[], int sizeOfa);
int search_contacts(contact contacts[], string& lastname, int& size);
int main(){
string opt="";
const int MAX=2;
int size;
contact contacts[MAX];
contact c[MAX];
for(int i=0; i<MAX; i++){
EnterContact(contacts, MAX, size);
}
menu();
return 0;
}
//use these variables globally
const int MAX = 2;
contact contacts[MAX];
void EnterContact(contact contacts[], int size, int& numberUsed)
{
char ans;
bool done = false;
int index = 0;
cout << "Enter up to " << size << endl;
while ((!done) && (index < size))
{
contacts[index].input();
cout << "Do you want to add another contact?(y/n followed by Return): ";
cin >> ans;
if ((ans == 'y') && (ans == 'Y'))
index++;
else
done = true;
}
numberUsed = index+1;
}
int search_contacts(contact contacts[], string& lastname, int& size)
{
int index = 0;
bool found = false;
for(int index=0; index<size ; index++){
if (lastname == contacts[index].getlname()){
found = true;
cout<<"found";
break;
}
}
if (!found)
cout<<"no";
return index;
}
void show(contact contacts[], int sizeOfa)
{
for (int index = 0; index < sizeOfa; index++)
contacts[index].output();
}
void menu()
{
cout<<"\nContact Menu"<<endl;
cout << "1. Add a New Contact. " << endl;
cout << "2. Search for a Contact. " << endl;
cout << "3. Delete Contact from list. " << endl;
cout << "4. View All Contacts. " << endl;
cout << "5. Exit the program. " << endl;
cout << "Enter your choice: ";
int opt; int result, a; char ans; string lastname;
cin>>opt;
switch (opt)
{
case 1: cout<<"func to Add a New Contact"<<endl;
cout<<"\nAdd another contact";
break;
case 2:
do
{
cout << "\nSearch contact by lastname: ";
cin >> lastname;
result = search_contacts(contacts, lastname, result);
if (result == -1)
cout << lastname << " Contact not found.\n";
else
contacts[result].output();
cout << lastname << " is stored in array position "<< result << endl;
cout << "Search again? (y/n): ";
cin >> ans;
} while ((ans != 'n') && (ans != 'N'));
menu();
break;
case 3: cout<<"func to Delete Contact from list";
break;
case 4: cout<<"\nAll Contacts"<<endl;
show(contacts, MAX);
cout<<endl;
menu();
break;
case 5: cout<<"\nContact Book is closed"<<endl;
exit(1);
break;
default: cout<<"\nInvalid entry...Closing Contact Book"<<endl;
}
}
contact::contact()
{
fn=""; ln=""; email=""; number="";
}
contact::contact(string f_n, string l_n, string emaila,string num)
{
fn= f_n; ln= l_n; email= emaila;number= num;
}
void contact::input()
{
cout<<"\nFirst Name: ";
cin>>fn;
cout<<"Last Name: ";
cin>>ln;
cout<<"Email: ";
cin>>email;
cout<<"Phone number: ";
cin>>number;
}
void contact::output()
{
cout<<"\nName: "<<fn<<" "<<ln;
cout<<"\nEmail: "<<email;
cout<<"\nPhone number: "<<number;
cout<<endl;
}
void contact::setfname(string f_n)
{
fn= f_n;
}
string contact::getfname();
{
return fn;
}
void contact::setlname(string l_n)
{
ln= l_n;
}
string contact::getlname()
{
return ln;
}
void contact::setemail(string emaila)
{
email= emaila;
}
string contact::getemail()
{
return email;
}
void contact::setnumber(string num)
{
number= num;
}
string contact::getnumber()
{
return number;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题是如何使用菜单函数,尝试在主函数中使用选择菜单,而不是将其声明为函数。如果您想将其用作函数,请考虑使用 if else 语句。应该照顾你的输入和输出。
另外,
您已经有一个函数来处理您的输入,您不需要将其放入主函数的 for 循环中。
your problem is how you use your menu function, try using your select menu in your main function instead of declaring it as a function. if you want to use it as a function consider using the if else statement. that should take care of your input and output.
}
also you already have a function that takes care of your input you dont need to put it in a for loop in the main function.
您的程序中有几个错误。阻止您显示联系人列表的原因是:
您有两个名为
contacts
的变量。首先,在main()
中声明了一个局部变量:接下来,在
main()
之后立即声明了一个全局变量:这两个变量是不同的 --除了名字巧合之外,它们没有任何关系。
EnterContact
写入其中一个数组,但show
显示另一个数组中的值。您可能应该将所有全局声明移至任何代码之前,并从
main()
中删除名称类似的声明。There are several errors in your program. The one that prevents you from displaying the contact list is this:
You have two variables named
contacts
. First, you have a local variable inmain()
declared thus:Next, you have a global variable declared immediately after
main()
thus:Those two variables are distinct -- they are not related in any way, except coincidentally by name.
EnterContact
writes into one of the arrays, butshow
displays the values from the other one.You should probably move all of your global declarations to before any of your code, and remove the similarly-named declarations from
main()
.我认为您需要将
i
传递给 EnterContact 函数,以便为contacts
数组中的每个对象提供输入。到目前为止,您在循环的每次迭代中都重写了相同的对象。I think you need to pass
i
to EnterContact function, so as to give input for each object in the array ofcontacts
. As of now, you are over writing the same object on every iteration of the loop.