Im basically making a database that stores a users account info. (Name, Phone number, ID, etc) I want to be able to display a specific persons info by entering in their ID num.
I have a struct with basic info and an array struct that stores each person and their info. I need to be able to type in a persons ID and have it display their info.
(I am still in first year of CS degree plz be gentle lol)
struct Account{
string name;
string city;
string state;
int ZIP;
int phone;
int IDNUM;
double ACT_BAL;
string LST_PMNT;};
Main fuction
const int SIZE = 20;
Account customers[SIZE];
const int NEW_INFO = 1, CHNG_INFO = 2, DISP = 3, EXIT = 4;
char choice1;
int choice;
int n;
int NEWCUST;
int results;
do
{ // menu display
cout << "Customer Database\n"
<< "----------------------------\n"
<< "1. Enter new account info\n"
<< "2. Change account info\n"
<< "3. Display all account info\n"
<< "4. Exit\n";
cin >> choice;
//respond to user input
switch (choice)
case NEW_INFO:
cout << "Would you like to enter a new cusomter?\n"
<< "(Y/N)";
cin >> choice1;
if (choice1 == 'Y' || choice1 == 'y')
{
cout << "How many new customers?" << endl; //User eneters in new customer info without having to enter in a full array worth of customers.
cin >> NEWCUST;
for (n = 0; n < NEWCUST; n++)
{
cout << "ID Number: ";
cin >> customers[n].IDNUM;
cout << "Enter in a name: ";
cin >> customers[n].name;
cout << "City: ";
cin >> customers[n].city;
cout << "State: ";
cin >> customers[n].state;
cout << "ZIP code: ";
cin >> customers[n].ZIP;
cout << "Phone number: ";
cin >> customers[n].phone;
cout << "Account Balance: ";
cin >> customers[n].ACT_BAL;
cout << "Lasy payment date: ";
cin >> customers[n].LST_PMNT;
}
}
break;
case CHNG_INFO: // Changes info
break; // displays all info (work in progress)
case DISP:
cout << "Enter customers ID number" << endl;
cin >> customers[].IDNUM;
results = linearSearch(customers, SIZE, customers[].IDNUM);
break;
case EXIT:
cout << "Cosing......" << endl; //exits progeam
break;
To get a specific user details, you can do the following:
Declaring num as short int to hold a simple User ID, you can change it. N = 50 as you've provided the constant. Now, unless the loop finds the inputted ID matching with one of the struct's idNum, it'll be executed 50 times (max).
cout << "Input account code: ";
cin >> num;
for (int i = 0; i < N; i++)
{
if (num == acc[i].idNum)
{
cout << "Name: " << acc[i].name << endl
<< "..." << endl;
}
}
Related
So I am trying to create a ATM system that lets user to input value such as Account number, account name and amount. But I can't figure out what exactly I have to do
int AccNum[2];
string AccName[2];
float AccBal[2];
cout << "********** ENTER ACCOUNT **********"<<endl;
for(int num = 0; num < 2; num++){
cout << "Enter Account number: ";
cin >> AccNum[num];
for(int name = num; name < 2; name++){
cout << "Enter Account Name: ";
getline(cin, AccName[name]);
for(int bal = name; bal < 2; bal++){
cout << "Enter Amount: ";
cin >> AccBal[bal];
}
}
}
I have tried something like this but it does not give the result that I want. The ideal result would be
********** ENTER ACCOUNT **********
Enter Account number: 1231232
Enter Account name: James white
Enter amount: 1000
it will run 2 times so there would be 2 accounts after this loop that will have a result like this
********** THE ACCOUNT IS **********
Account number: 1231232
Account name: James white
Balance: 1000
So the code you wrote is nearly good. Too many loops in my opinion
int AccNum[2];
string AccName[2];
float AccBal[2];
cout << "********** ENTER ACCOUNT **********"<<endl;
for(int num = 0; num < 2; num++){ // lets call this loop a. happens 2 times
cout << "Enter Account number: ";
cin >> AccNum[num];
for(int name = num; name < 2; name++){ // loop b happens 2 times * loop a times
cout << "Enter Account Name: ";
getline(cin, AccName[name]);
for(int bal = name; bal < 2; bal++){ loop c = 2 * a * b = 8
cout << "Enter Amount: ";
cin >> AccBal[bal];
}
}
}
Direct fix:
int main()
{
int AccNum[2];
string AccName[2];
float AccBal[2];
cout << "********** ENTER ACCOUNT **********" << endl;
for(int num = 0; num < 2; num++){
cout << "Enter Account number: ";
cin >> AccNum[num];
cout << "Enter Account Name: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, AccName[num]);
cout << "Enter Amount: ";
cin >> AccBal[num];
}
for(int i = 0; i < 2; i++)
cout << "********** ENTER ACCOUNT **********" << endl
<< "Account number: " << AccNum[i] << endl
<< "Account name: " << AccName[i] << endl
<< "Balance: " << AccBal[i] << endl << endl;
}
But if you want to expand it a little bit:
#include <iostream>
#include <vector>
using namespace std;
struct Account
{
int Number;
string Name;
float Balance;
Account(int num, string nam, float bal) : Number(num), Name(nam), Balance(bal) {}
Account() {}
void getData()
{
cout << "Enter Account number: ";
cin >> Number;
cout << "Enter Account Name: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, Name);
cout << "Enter Amount: ";
cin >> Balance;
}
void printAccount()
{
cout << "********** ENTER ACCOUNT **********" << endl
<< "Account number: " << Number << endl
<< "Account name: " << Name << endl
<< "Balance: " << Balance << endl << endl;
}
};
int main()
{
vector<Account> accounts;
for(int i = 0; i < 2; i++)
{
Account person;
person.getData();
accounts.emplace_back(person);
}
for(int i = 0; i < 2; i++) accounts[i].printAccount();
}
Both codes give the exact same output:
I am new to c++ and my textbook is not very helpful. I have a few errors in my code. Where I am being told the identifier for customerAccount is undefined, and I have an incompatible declaration with my int search before and after my main. I will post some code below as I have been trying to figure this out for a while and I am at a loss.
#include<iostream>
#include<string>
using namespace std;
struct {
string Name;
string Address;
string City_State_Zip;
double phoneNumber;
double actBalance;
string Payment;
};
//This is where the errors start, saying customer account is undefined
void Display(customerAccount ca);
//declaration is incompatible with int search
int Search(customerAccount, string);
int main() {
customerAccount customers[10];
string SName;
int choice, i = 0, size = 0;
do {
cout << "****Menu****" << endl;
cout << "1. Enter Customer Information" << endl;
cout << "2. Change Customer Information" << endl;
cout << "3. Search For Customer" << endl;
cout << "4. Display Customer Data" << endl;
cout << "5. Exit" << endl;
cout << "Please enter a choice 1,2,3,4 or 5";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter customer name: ";
cin >> customers[i].Name;
cout << "Enter customer address: ";
cin >> customers[i].Address;
cout << "Enter city state and zip: ";
cin >> customers[i].City_State_Zip;
cout << "Enter phone number: ";
cin >> customers[i].phoneNumber;
cout << "Enter account balance: ";
cin >> customers[i].actBalance;
if (customers[i].actBalance < 0) {
cout << "Account balance cannot be negative. Please re-enter: "
<< endl;
cin >> customers[i].actBalance;
}
cout << "Enter last payment: ";
cin >> customers[i].Payment;
i++;
break;
case 2: int ele;
cout << "Enter customer information to modify: " << endl;
cout << "Enter customer name: ";
cin >> customers[ele - 1].Name;
cout << "Enter customer address: ";
cin >> customers[ele - 1].Address;
cout << "Enter city state and zip";
cin >> customers[ele - 1].City_State_Zip;
cout << "Enter phone number: ";
cin >> customers[ele - 1].phoneNumber;
cout << "Enter account balance: ";
cin >> customers[ele - 1].actBalance;
if (customers[ele - 1].actBalance < 0) {
cout << "Account balance cannot be negative. Please re-enter: "
<< endl;
cin >> customers[i].actBalance;
}
cout << "Enter date of payment: ";
cin >> customers[ele - 1].Payment;
break;
case 3: cout << "Enter customer name to search: ";
cin >> SName;
for (size = 0; size < i; size++) {
int check;
check = Search (customers[size], SName);
if (check == 1)
Display(customers[size]);
}
break;
case 4:
for (size = 0; size < i; size++)
Display(customers[size]);
break;
case 5: exit(0);
break;
}
} while (choice != 5);
system("pause");
return 0;
}
void Display(customerAccount ca) {
cout << " Customer name:";
cout << ca.Name << endl;
cout << " Address:";
cout << ca.Address << endl;
cout << " city state and zip:";
cout << ca.City_State_Zip << endl;
cout << " Phone number:";
cout << ca.phoneNumber << endl;
cout << " Account balance:";
cout << ca.actBalance << endl;
cout << " Date of payment:";
cout << ca.Payment << endl;
}
//declaration is incompatible with int search
int Search(customerAccount ca, string str) {
if (str.compare(ca.Name) == 0)
return 1;
else
return 0;
}
case 2: int ele;
On this line you have an uninitialized variable which is causing your bug.
I am in desperate need of help. I am supposed to create a student record using structs and vectors.
In the program, I should have:
For a given student, identified either by name or student number
(a) Enter a new student into the record. (Assume no courses completed.)
(b) Grade point average
(c) Transcript, i.e. a list of courses taken, including credit value and grade earned
(d) Record a newly completed course
A listing, sorted by name, of all students who have taken a given course.
A listing, sorted by grade point average, of all students and their grade point averages,
who are on probation, i.e. have a grade point average < 2.0 .
This is what I have so far... and I seem to be having troubles with the user input how read it into my structs
using namespace std;
struct courses {
string courseName;
int courseNum;
double credit;
char grade;
};
struct students {
string name;
int id;
vector<courses> c;
};
int main() {
// variables
string name;
char selector;
students s;
courses d;
vector<students> student;
vector<courses> course;
// (1) create a menu: (a) user input, (b) echo record (with overall gpa), (c) failed students
do {
// prompt for user input
cout << "Enter Q to (Q)uit, (C)reate new student record, (S)how all record(s) on file, show students on (P)robation: ";
cin >> selector;
selector = toupper(selector);
switch (selector) {
// (a) ask and get for user input:
// student info first
// courses second
case 'C':
// variables within case C
char answer;
char answerAddAnotherCourseEntry;
char answerToAnotherStudentRecord;
do {
cout << "Enter your name and student ID number: ";
cin >> s.name >> s.id;
student.push_back(s);
do {
cout << "Do you want to create a student course entry ('y' or 'n')? ";
cin >> answer;
answer = toupper(answer);
cout << "Enter your course number, course name, grade received and credit worth: ";
cin >> d.courseNum >> d.courseName >> d.grade >> d.credit;
course.push_back(d);
cout << "Add another student course entry ('y' or 'n'): ";
cin >> answerAddAnotherCourseEntry;
answerAddAnotherCourseEntry = toupper(answerAddAnotherCourseEntry);
} while (answer == 'N');
cout << "Add another student record ('y' or 'n'): " << endl;
cin >> answerToAnotherStudentRecord;
answerAddAnotherCourseEntry = toupper(answerToAnotherStudentRecord);
} while (answerToAnotherStudentRecord == 'N');
break;
// (b) echo record of vectors
// sort by name
case 'S':
if (student.empty()) {
cout << "\nSorry, no records exist in the database.";
}
else
for (int count = 0; count < student.size(); count++) { //For Loop to Display All Records
cout << "Student name: " << student[count].name << "ID Number: " << student[count].id << endl;
count++;
// another for loop i think
for (int i = 0; i < course.size(); i++) {
cout << "Course info: " << " " << course[i].courseNum << " " << course[i].courseName << " " << course[i].credit << " " << course[i].grade << endl;
i++;
}
}
cout << endl;
break;
// (c) separate failed student into another vector
// sort by gpa
case 'P':
break;
} // bracket closing switch
} // bracket closing do while
while (selector != 'q' && selector != 'Q'); // first do while
The issues I see with your input is that you are trying to store the user input directly in a string, here e.g. in the variable name of the struct s:
cout << "Enter your name and student ID number: ";
cin >> s.name >> s.id;
This does not compile. Instead you can store the name in a temporary char array first and then copy it to s.name.
char studentName[128];
cout << "Enter your name and student ID number: ";
cin >> studentName >> s.id;
s.name = studentName;
You can also work with getline() if you want to store your input directly in a string.
Furthermore you try to directly output the variable student[count].name, which is a string, to cout:
cout << "Student name: " << student[count].name << "ID Number: " << student[count].id << endl;
Converting the string to a char array first makes this compile:
cout << "Student name: " << student[count].name.c_str() << "ID Number: " << student[count].id << endl;
I have to write a program for an array based database that will allow the user to enter new data, update existing data, delete entries and view a list of the entries. The requirements are a structure called DATE, a structure called CustData that holds the user data, an array of type CustData with a size of 10 and requires an individual function for entering, updating, deleting and displaying the customer data. It also needs to use a while loop to initialize each index in the array with everything initialized to 0. I have a rough program written but the more I work on it the more I feel like I am doing it completely wrong. So far I have it working to the point that it lets me add multiple entries without overwriting previous ones, but I can't seem to be able to limit the number of entries I can input. I also have the displaying of entries correct. Any help that could be offered is greatly appreciated, below is my program so far, with some of the data input sections commented out to make testing it easier. My apologies for leaving this out, this is in C++, written with visual studio 2013.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct Date
{
int month,
day,
year;
};
struct cust
{
int ID;
string name;
string address;
string city;
string state;
string zip;
string phone;
double balance;
Date lastpayment;
};
const int SIZE = 10;
int menuchoice;
int num = 0;
int i;
void showmenu();
void funcentercustdata(cust[], int);
void funcupdatecustdata();
void funcdeletecustdata();
void funcdisplaycustdata(cust[], int);
cust custdb[SIZE];
int main()
{
cout << "Welcome to Michael's Marvelous Database Contrabulator!\n";
cout << setw(10) << "Customer Database\n\n\n";
showmenu();
int index;
for (index = 0; index < SIZE; index++)
{
custdb[index].ID = 0;
custdb[index].name = "";
custdb[index].address = "";
custdb[index].city = "";
custdb[index].state = "";
custdb[index].zip = "";
custdb[index].phone = "";
custdb[index].balance = 0.00;
custdb[index].lastpayment.month = 0;
custdb[index].lastpayment.day = 0;
custdb[index].lastpayment.year = 0;
}
return 0;
}
void showmenu()
{
cout << "\n\n1) Enter new customer data.\n";
cout << "2) Update customer data.\n";
cout << "3) Delete customer data.\n";
cout << "4) Display Customer data.\n";
cout << "5) Quit the program.\n\n";
cout << "Please enter your choice: ";
cin >> menuchoice;
do
{
switch (menuchoice)
{
case 1:
funcentercustdata(custdb, SIZE);
showmenu();
break;
case 2:
funcupdatecustdata();
showmenu();
break;
case 3:
funcdeletecustdata();
showmenu();
break;
case 4:
funcdisplaycustdata(custdb, SIZE);
showmenu();
break;
case 5:
cout << "Thank you and have a nice day!\n";
break;
default:
cout << "Please enter a correct choice\n";
cin >> menuchoice;
break;
}
} while (menuchoice != 5);
}
void funcentercustdata(cust custinfo[], int size)
{
if (custinfo[i].ID != 0)
{
i++;
cout << "\n\nEnter ID: ";
cin >> custinfo[i].ID;
cout << "Enter name: ";
cin.ignore(0);
cin >> custinfo[i].name;
/* cout << "Enter address: ";
cin.ignore(0);
cin>>custinfo[i].address;
cout << "Enter city: ";
cin.ignore(0);
cin>>custinfo[i].city;
cout << "Enter state: ";
cin.ignore(0);
cin>>custinfo[i].state;
cout << "Enter zip: ";
cin.ignore(0);
cin>>custinfo[i].zip;
cout << "Enter phone number (###-###-####): ";
cin.ignore(0);
cin>>custinfo[i].phone;
cout << "Enter balance: ";
cin >> custinfo[i].balance;
cout << "Enter last payment (mo day year, e.g. 11 17 2014): ";
cin >> custinfo[i].lastpayment.month >> custinfo[i].lastpayment.day
>> custinfo[i].lastpayment.year;
cin.ignore(1);
// }*/
cout << "Customers successfully added.\n";
}
else if (custinfo[i].ID != 0 && custinfo[i].ID >= 4)
{
cout << "No further inputs allowed\n";
}
else
{
cout << "\n\nEnter ID: ";
cin >> custinfo[i].ID;
cout << "Enter name: ";
cin.ignore(0);
cin >> custinfo[i].name;
/* cout << "Enter address: ";
cin.ignore(0);
cin>>custinfo[i].address;
cout << "Enter city: ";
cin.ignore(0);
cin>>custinfo[i].city;
cout << "Enter state: ";
cin.ignore(0);
cin>>custinfo[i].state;
cout << "Enter zip: ";
cin.ignore(0);
cin>>custinfo[i].zip;
cout << "Enter phone number (###-###-####): ";
cin.ignore(0);
cin>>custinfo[i].phone;
cout << "Enter balance: ";
cin >> custinfo[i].balance;
cout << "Enter last payment (mo day year, e.g. 11 17 2014): ";
cin >> custinfo[i].lastpayment.month >> custinfo[i].lastpayment.day
>> custinfo[i].lastpayment.year;
cin.ignore(1);
// }*/
cout << "Customers successfully added.\n";
}
}
void funcupdatecustdata()
{
cout << "insert function 2\n\n";
}
void funcdeletecustdata()
{
cout << "insert function 3\n\n";
}
void funcdisplaycustdata(cust custinfo[], int size)
{
for (int i = 0; i < size; i++)
{
if (custinfo[i].ID == 0)
cout << " ";
else if (custinfo[i].ID != 0)
{
cout << "\n\nClient ID: " << custinfo[i].ID << endl;
cout << "Client name: " << custinfo[i].name << endl;
/* cout << "Client address: " << custinfo[i].name << endl;
cout << "Client city: " << custinfo[i].name << endl;
cout << "Client state: " << custinfo[i].name << endl;
cout << "Client zip: " << custinfo[i].name << endl;
cout << "Client phone: " << custinfo[i].name << endl;*/
cout << "Client balance: " << custinfo[i].balance << endl;
cout << "Client last deposit: " << custinfo[i].lastpayment.month << "/" <<
custinfo[i].lastpayment.day << "/" << custinfo[i].lastpayment.year << endl;
}
}
}
You've asked multiple questions concerning the issues in your program. So I will look at the first question:
I can't seem to be able to limit the number of entries I can input
First, your code has some fundamental flaws. One flaw is the repeated calling of showmenu() while you're in the showmenu() function. This is a recursive call, and is totally unnecessary. Imagine if your program or similar program that was structured this way had to be running 24 hours a day, and there were thousands of entries added. You will evenutally blow out the stack with all the recursive calls. So this has to be fixed.
Second, showmenu(), at least to me, should do what it says, and that is "show the menu". It should not be processing input. Do the processing of input in a separate function.
Here is a more modularized version of the program:
#include <iostream>
void processChoice(int theChoice);
void showmenu();
void addCustomer();
void deleteCustomer();
int getMenuChoice();
int customerCount = 0;
int main()
{
cout << "Welcome to Michael's Marvelous Database Contrabulator!\n";
cout << setw(10) << "Customer Database\n\n\n";
int choice = 0;
do
{
showmenu();
choice = getMenuChoice();
if (choice != 5)
processChoice(choice);
} while (choice != 5);
}
void showmenu()
{
cout << "\n\n1) Enter new customer data.\n";
cout << "2) Update customer data.\n";
cout << "3) Delete customer data.\n";
cout << "4) Display Customer data.\n";
cout << "5) Quit the program.\n\n";
}
int getMenuChoice()
{
int theChoice;
cout << "Please enter your choice: ";
cin >> theChoice;
return theChoice;
}
void processChoice(int theChoice)
{
switch (theChoice)
{
case 1:
addCustomer();
break;
//...
case 3:
deleteCustomer();
break;
}
}
void addCustomer()
{
if ( customerCount < 10 )
{
// add customer
// put your code here to add the customer
//...
// increment the count
++customerCount;
}
}
void deleteCustomer()
{
if ( customerCount > 0 )
{
// delete customer
--customerCount;
}
}
This is the basic outline of keeping track of the number of customers. Code organization and modularity is the key. The count of the current number of entries is either incremented or decremented wihin the addCustomer and deleteCustomer functions. Also note the test that is done in add/deleteCustomer().
In the main() function, note the do-while loop and the way it is set up. The showMenu function shows itself, the getMenuChoice function gets the choice and returns the number that was chosen.
If the choice is not 5, process the request. If it is 5, then the while part of the do-while kicks you out of the processing, otherwise you start back at the top (showMenu, getMenuChoice, etc). This avoids the recursive calls that your original code was doing.
Hi I am creating a simple password program. The program requires the user to enter an account number and password. The following code works fine however the only problem I have is after 3 incorrect attempts I want the program to terminate with an appropriate message. I can't figure out how to get the loop to stop after 3 incorrect attempts and was hoping someone could help me with this. From what I've gathered I think I may have to use a for loop but I just can't seem to get it working properly. Thanks!
int A;
string guess;
const string pass;
const int number;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
while(A!=number || guess!=pass)
{
cout<<"Incorrect password. Try again"<<endl;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
}
How about:
for (int counter = 0; counter < 2 && (A != number || guess != pass); ++counter)
{
...
}
int attempts = 0;
while(A!=number || guess!=pass)
{
if( attempts++ == 3 )
{
cout << "Tough luck; exitting ..." << endl;
break;
}
cout<<"Incorrect password. Try again"<<endl;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
}
int A;
string guess;
const string pass = /* some value */;
const int number = /* some value */;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
int i = 0;
const int MAX_ATTEMPT = 3;
bool success;
while( ( success = ( A!=number || guess!=pass ) ) && ( ++i < MAX_ATTEMPT ) )
{
cout<<"Incorrect password. Try again"<<endl;
cout << "Please Enter Account Number:" << endl;
cin >> A;
cout << "Enter Password Account Password:"<< endl;
cin >>guess;
}
if ( success ) /* other stuff */