Adding and displaying information

Hello guys, I'm trying to add customer details in one function and display the added details in another function. I used a for loop to do this therefore I have to add all the details with the number of index I used (if that makes sense). What I want to do is add them one at a time and display them all in the other function (idk if it's even possible). This should be done inside the console application and no database engine is connected. This is the part of my code

struct CustomerData {
string accNo;
string custName;
string custAddress;
string custTel;
string connType;
string InternetConnType;
};

CustomerData cd[3];

void AddCustomerData()
{

for (int i = 0; i < 3; ++i)
{
cout << "\n Customer Account Number: ";
cin >> cd[i].accNo;
cout << " Customer Name: ";
cin >> cd[i].custName;
cout << " Customer Address: ";
cin >> cd[i].custAddress;
cout << " Customer Telephone Number: ";
cin >> cd[i].custTel;
cout << " Customer Connection Type: ";
cin >> cd[i].connType;
cout << " Customer Internet Connection Type: ";
cin >> cd[i].InternetConnType;
cout << " \nDetails Entered" << endl;

cout << endl << " Inserted data given" << endl;
for (int i = 0; i < 3; ++i)
{
cout << "\n Customer Account Number: " << cd[i].accNo << endl;
cout << " Customer Name: " << cd[i].custName << endl;
cout << " Customer Address: " << cd[i].custAddress << endl;
cout << " Customer Telephone Number: " << cd[i].custTel << endl;
cout << " Customer Connection Type: " << cd[i].connType << endl;
cout << " Customer Internet Connection Type: " << cd[i].InternetConnType << endl;

}
}
}

void DisplayCustomerData()
{
cout << endl << " Inserted data given" << endl;
for (int i = 0; i < 3; ++i)
{
cout << "\n Customer Account Number: " << cd[i].accNo << endl;
cout << " Customer Name: " << cd[i].custName << endl;
cout << " Customer Address: " << cd[i].custAddress << endl;
cout << " Customer Telephone Number: " << cd[i].custTel << endl;
cout << " Customer Connection Type: " << cd[i].connType << endl;
cout << " Customer Internet Connection Type: " << cd[i].InternetConnType << endl;

}
}


For more clarity, when I say add customer details it should be added once and return to the main menu and it should be stored. and in display it should display what I've added. It doesn't make sense to add the details of three customers only in a program.
Hello snoozycs1,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


It is arduous to talk about code you have not included in your post.

You have defined CustomerData cd[3]; as a global variable. You may think this is easy, but just as it is easy to use any line of code in the file can change a global variable and that can be hard to track down.

Suggestions: Define CustomerData cd[3]; inside "main" and pass it to the functions that will need it. You will have better control over the array. Come up with a better name than "cd". Something like "customerData" or "custData" is easier to read and understand along with being more descriptive of what it is. in your array and for loops you hard code the number "3". Above the struct put constexpr size_t MAXSIZE{ 3 };. And when you define the array it would be CustomerData cd[MAXSIZE];.
and for (int i = 0; i < MAXSIZE; ++i)

As you say
It doesn't make sense to add the details of three customers only in a program.
But it dose make sence when yo are first writing the program and testing. Later you change the value of "MAXSIZE" and everywhere in the program where you have used "MAXSIZE" it will change, but you only have to make the change in one place.

size_t" should be a typedef for an "unsigned int".

If you can use a vector it would be a better choice over the array.

In your add customer function I would define "i" outside of the for loop and might call it something like "count" or "index". This way it will hold the total records entered. Later in the program it can be used to limit for loops to use the amount of the array used when you increase the size of the array. Not something you have to worry about with a vector.

In the "add' function instead of the second for loop to print the entire array delete the the second for loop and put the "cout" statements at the end of the first for loop and you will only print what you just entered.

A suggestion for the future when your array becomes larger:
1
2
3
4
5
6
7
8
size_t index;

for (index = 0; index < MAXSIZE && accNo[0] != 'q'; ++index)
{
    cout << "\n Customer Account Number: ";
    cin >> cd[index].accNo;

    if (std::tolower(cd[index].accNo[0]) == 'q') continue;

This way you have an out if your array is 50 or 100 in size and you do not need all of it. Also "index" will hold the number of records that were actually entered.

The function "std::tolower(...)" or "std::toupper(...) requires the header file "cctype" to work.

There are a few questions I have:

What does an account number look like?
What does a telephone number look like?
What does a Customer Connection Type look like?
What does a Customer Internet Connection Type look like?

I noticed you are using formatted input of "cin >> cd[i].custName" and "cin >> cd[i].custAddress". Given a customer name of "John Doe" the "cin >>" will extract from the input buffer up to the first white space putting "John" in "custName" and leaving "Doe" in the input buffer for the next "cin >>" to extract. Name address and maybe other variables would work better with "std::getline(...)" to get everything that was typed for the given variable.

In the above for loop I defined "index" out side of the for loop. This variable can either be returned to "main" for later use or passed by reference from main. Either way would work.

That is something to work on for now. I will load up what you have and see what else I can figure out.

Hope that helps,

Andy
Topic archived. No new replies allowed.