#include<iostream> #include<string> #include <stdio.h> #include <conio.h> using
ID: 3650228 • Letter: #
Question
#include<iostream>#include<string>
#include <stdio.h>
#include <conio.h>
using namespace std;
struct customerAccount
{
string Name;
string Address;
string City_State_Zip;
double TelNo;
double AccountBalance;
string Date_of_Pay;
};
void Display(customerAccount ca[], int s);
int main()
{
customerAccount customers[20];
customerAccount * ptrCustomerAccount[20];
int choice, i = 0, size = 0;
do
{
cout<<"Menu"<<endl;
cout<<"1.Entering Data in to array"<<endl;
cout<<"2.Modify Data"<<endl;
cout<<"3.Display Data"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter Choice:";
cin >> choice;
switch(choice)
{
case 1:
cout<<"Enter Customer Name:";
cin >>ptrCustomerAccount[i]->Name;
cout<<"Enter Address:";
cin >>ptrCustomerAccount[i]->Address;
cout<<"Enter city state and zip:";
cin >>ptrCustomerAccount[i]->City_State_Zip;
cout<<"Enter Telephone number:";
cin>>ptrCustomerAccount[i]->TelNo;
cout<<"Enter Account Balance:";
cin>>ptrCustomerAccount[i]->AccountBalance;
if(ptrCustomerAccount[i]->AccountBalance < 0)
{
cout<<"Account balance cannot be negative please reenter your balance:"<<endl;
cin>>ptrCustomerAccount[i]->AccountBalance;
}
cout<<"Enter Date of payment:";
cin >>ptrCustomerAccount[i]->Date_of_Pay;
i++;
break;
case 2: int element;
cout<<"Enter element to be modified:";
cin>>element;
cout<<"Re-Enter data of a particular element:"<<endl;
cout<<"Enter Customer Name:";
cin>>ptrCustomerAccount[element-1]->Name;
cout<<"Enter Address:";
cin>>ptrCustomerAccount[element-1]->Address;
cout<<"Enter city state and zip";
cin>>ptrCustomerAccount[element-1]->City_State_Zip;
cout<<"Enter Telephone number:"<<endl;
cin>>ptrCustomerAccount[element-1]->TelNo;
cout<<"Enter customers account balance:"<<endl;
cin>>ptrCustomerAccount[element-1]->AccountBalance;
if(ptrCustomerAccount[element-1]->AccountBalance < 0)
{
cout<<"Account balance cannot be negative please reenter your balance:"<<endl;
cin>>ptrCustomerAccount[i]->AccountBalance;
}
cout<<"Ente date of payment:";
cin>>ptrCustomerAccount[element-1]->Date_of_Pay;
case 3:
Display(customers,i);
break;
case 4: exit(0);
break;
}
}while(choice!=4);
delete [] ptrCustomerAccount;
return 0;
}
void Display(customerAccount ca[], int s)
{
for(int i=0; i < s; ++i)
{cout<<"Name: "<<ca[i].Name<<endl;
cout<<"Address:"<<ca[i].Address<<endl<<ca[i].City_State_Zip<<endl;
cout<<"Telephone number:"<<ca[i].TelNo<<endl;
cout<<"Account Balance:"<<ca[i].AccountBalance<<endl<<endl;
}
}
//Having trouble during run time.
Explanation / Answer
I do not have C++ on this machine and I am not sure what runtime issues you are encountering. However, I see some potential issues that you might have encountered.
1. You may get jibberish returned if you choose option 2 or 3 immediately upon starting the program. This is because the entire customer account array is empty.
I suggest creating a dummy account in element 0 of the array...just put a dummy name, address, phone, etc.
2. In case 2 of the switch statement, even though you have a cin for the element variable, it is not initialized when you instantiate it. It is best to do int element = 0.
3. You can change case 4 to default. Since the only valid cases are 1,2,3 then anything else should fall out of the Switch. So after case 3, it should look like
default:
break; //no need for exit(0)
4. Obviously this is a program where you expect the user to enter only valid data, but I would change the "while" statement to be "while(choice < 4)
5. I suspect that when you enter 4 as the choice, it still goes into the Switch because the check for the choice !=4 does not occur until the end of the Switch. You can remedy this by using a "while" loop rather than a do..while loop. Just place the "while(choice<4)" where "do" is. You need to copy the statements that ask for and cin the choice. Then paste the statements above the "while" statement.
int choice = 1...;
cout<<"Menu"< cout<<"1.Entering Data in to array"< cout<<"2.Modify Data"< cout<<"3.Display Data"< cout<<"4.Exit"< cout<<"Enter Choice:";
cin >> choice;
while(choice<4) {
Switch(choice)
.
.
.
default:
break;
cout<<"Menu"<cout<<"1.Entering Data in to array"<cout<<"2.Modify Data"<cout<<"3.Display Data"<cout<<"4.Exit"<cout<<"Enter Choice:";
cin >> choice;
}//end while loop
6. This statement should be changed from "if" to "while", other if a negative value is entered twice, it will be allowed.
if(ptrCustomerAccount[i]->AccountBalance < 0)
{
cout<<"Account balance cannot be negative please reenter your balance:"<cin>>ptrCustomerAccount[i]->AccountBalance;
}
Hope that helps, good luck!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.