I'm trying to get this program to run properly. it should do as the pseudo code do as described although when I execute the program and try to open a new account if I put more than one character in the customer name field the program just goes into an infinite loop and I have clue how to fix this issue.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <iomanip>
using namespace std;
int choice, account_number;
long acc_entry;
long acc_no = 112280;
double balance, deposit, withdrawal;
int interest = 1.67;
string customer_name;
void display_menu();
void get_choice();
void menu_selection(int selection);
void open_account();
void make_withdrawal();
void make_deposit();
void add_interest();
void display_transaction();
void main()
{
get_choice();
}
void display_menu()
{
system("CLS");
cout << "\n\n\t\t\t\tACCOUNT MENU";
cout << "\n\t\t\t\t============\n";
cout << "\n\t\t\t\t1. Open Account";
cout << "\n\t\t\t\t2. Make Withdrawal";
cout << "\n\t\t\t\t3. Make Deposit";
cout << "\n\t\t\t\t4. Add Interest";
cout << "\n\n\t\t\t\t5. Exit";
}
void open_account()
{
system("CLS");
cout << "\n\n\t\t\t\tOPEN ACCOUNT";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter your name\n\n\t";
cin >> customer_name;
cout << "\n\n\tPlease enter initial despoit\n\n\t";
cin >> deposit;
balance = balance + deposit;
account_number = acc_no + 1;
cout << "\n\n\tYour new account number\n\n\t" << setfill('0') << setw(8) << account_number;
get_choice();
}
void make_withdrawal()
{
system("CLS");
cout << "\n\n\t\t\t\tMAKE WITHDRAWAL";
cout << "\n\t\t\t\t===============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tPlease enter amount to withdraw\n\n\t";
cin >> withdrawal;
if (withdrawal > balance)
{
cout << "\n\n\tYou are exceeding your limit";
cin.ignore();
cin.get();
}
else
{
balance = balance - withdrawal;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void make_deposit()
{
system("CLS");
cout << "\n\n\t\t\t\tMAKE DEPOSIT";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tPlease enter amount to deposit\n\n\t";
cin >> deposit;
balance = balance + deposit;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void add_interest()
{
string yn;
system("CLS");
cout << "\n\n\t\t\t\tADD INTEREST";
cout << "\n\t\t\t\t============\n\n";
cout << "\tPlease enter Account Number\n\n\t";
cin >> acc_entry;
if (acc_entry == account_number)
{
cout << "\n\n\tDo you wish to add interest [Y/N]\n\n\t";
getline(cin, yn);
if (yn == "Y" || yn == "y")
{
balance = balance * interest;
cout << "\n\n\tYour new balance\n\n\t" << fixed << setprecision(2) << (char)156 << balance;
cin.ignore();
cin.get();
}
}
else
{
cout << "\n\n\tAccount number does not exist.";
cin.ignore();
cin.get();
}
get_choice();
}
void display_transaction()
{
system("CLS");
cout << "\n\n\t\t\t\tCLOSED";
cout << "\n\t\t\t\t======\n\n";
if (account_number != 112280)
{
cout << "\tCustomer Name : - " << customer_name;
cout << "\n\n\tAccount Number : - " << setfill('0') << setw(8) << account_number;
cout << "\n\n\tBalance : - " << fixed << setprecision(2) << (char)156 << balance << "\n\n";
}
cin.get();
}
void get_choice()
{
display_menu();
do
{
cout << "\n\n\t\t\t\tEnter Number [1-5] : ";
cin >> choice;
menu_selection(choice);
} while
(choice << 1 || choice >> 5);
cin.ignore();
}
void menu_selection(int a)
{
switch (a)
{
case 1:
{
open_account();
break;
}
case 2:
{
make_deposit();
break;
}
case 3:
{
make_withdrawal();
break;
}
case 4:
{
add_interest();
break;
}
case 5:
{
display_transaction();
break;
}
default:
{
cout << "hello";
}
}
}
void get_choice()
{
display_menu();
do
{
cout << "\n\n\t\t\t\tEnter Number [1-5] : ";
cin >> choice;
menu_selection(choice);
} while
(choice < 1 || choice > 5); // << and >> aren't for comparison
cin.ignore();
}
In your get_choice function, you have a do-while loop with the following condition:
while (choice << 1 || choice >> 5); // this is going to run for a bit
<< and >> are not comparison operators; rather, they are bit shift operators.
Because your choice can be shifted either left or right, it goes into an infinite loop. It's a simple fix, really - change them to comparison operators!
As for the customer name, if there are spaces or newlines, std::cin will stop reading at the first one. Be sure to use std::getline for reading strings from stdin.
In open_account, replace this:
cin >> customer_name;
with this:
std::getline(cin, customer_name);
Also, fix this line:
int interest = 1.67; // This should be a double
You shouldn't be using globals really, but that's a whole different story.
Related
I am currently working on a personal project. For now, I am creating the part where it gets the users information. I am running into an issue where if the user chooses to correct their name, it steps over it and does not let the user re input the correct name. I have tried clearing before getting to the switch statement and in different locations.
Here is the switch statement:
switch (correction)
{
case 1 : cout << "Name: ";
cin.clear();
getline(cin,name);
checkInfo(age,number,name);
break;
case 2 : cout << "Age: ";
cin >> age;
while (age < 21)
{
cout << "Age is too low, try again." << endl;
cin >> age;
}
checkInfo(age,number,name);
break;
case 3 : cout << "Number: ";
cin >> number;
while(!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
break;
default : cout << "Please select options 1 through 3, nothing else is accepted: ";
cin >> correction;
break;
}
And here is where the user first in puts their information in the beginning:
cout << "Enter your name: ";
getline(cin,name);
cin.clear();
cout << "Enter your age: ";
cin >> age;
cin.clear();
cout << "Enter your phone number: ";
cin >> number;
cout << endl;
while (age >= 21)
{
while (!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
}
}
I have a feeling i'm not clearing it somewhere correctly. Any help or recommendations to make this better is also appreciated. Thank you.
info.h file:
//info.h file
#ifndef INFO
#define INFO
#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>
using namespace std;
namespace info
{
class user
{
public:
char yesno;
char confirm;
int correction;
void getInfo (int age, string number, string name);
void checkInfo(int age, string number, string name);
void changeInfo(int age, string number, string name);
private:
bool isNumeric(string str);
};
}
void info::user::changeInfo(int age, string number, string name)
{
cout << "Age: " << age << endl;
cout << endl;
cout << "Please select the number according to the information that needs to be changed." << endl;
cout << endl;
cout << "1. Name" << endl;
cout << "2. Age" << endl;
cout << "3. Number" << endl;
cout << "Selection: ";
cin >> correction;
switch (correction)
{
case 1 : cout << "Name: ";
cin.clear();
getline(cin,name);
checkInfo(age,number,name);
break;
case 2 : cout << "Age: ";
cin >> age;
while (age < 21)
{
cout << "Age is too low, try again." << endl;
cin >> age;
}
checkInfo(age,number,name);
break;
case 3 : cout << "Number: ";
cin >> number;
while(!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
break;
default : cout << "Please select options 1 through 3, nothing else is accepted: ";
cin >> correction;
break;
}
}
void info::user::getInfo (int age, string number, string name)
{
cout << "Enter your name: ";
getline(cin,name);
cin.clear();
cout << "Enter your age: ";
cin >> age;
cin.clear();
cout << "Enter your phone number: ";
cin >> number;
cout << endl;
while (age >= 21)
{
while (!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
}
}
void info::user::checkInfo(int age, string number, string name)
{
cout << "Is the given information correct?" << endl;
cout << "-------------------" << endl;
cout << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Number: " << number << endl;
cout << endl;
cout << "If yes, please press y for yes, n for no: ";
cin >> confirm;
while (age >= 21)
{
if (confirm == 'y' || confirm == 'Y')
{
cout << "In my actual project, this is where i would save the information" << endl;
break;
}
else
{
changeInfo(age,number,name);
checkInfo(age,number,name);
break;
}
}
}
bool info::user::isNumeric(string str)
{
for (int i = 0; i < str.length(); i++)
{
if(isdigit(str[i]) == false)
{
return false;
}
}
return true;
}
#endif
Main.cpp file:
#include <iostream>
#include "info.h"
using namespace std;
int main ()
{
int age;
string number;
string name;
info::user userInfo;
userInfo.final(age, number,name);
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
#include <iostream>
using namespace std;
// ATM menu
void Menu()
{
cout << " MENU " << endl;
cout << "1.Deposit" << endl;
cout << "2.Balance" << endl;
cout << "3.Withdraw" << endl;
cout << "4.Transfer" << endl;
cout << "5.Exit" << endl;
cout << "-------------------------------" << endl;
}
// Tasks of ATM
int Task(int balance, int balance2, int option)
{
int amount;
cin >> option;
switch(option)
{
case 1:
cout << "please enter the amount" << endl;
cin >> amount;
balance += amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
case 2:
cout << balance << endl;
break;
case 3:
cout << "please enter the amount" << endl;
cin >> amount;
if(amount <= balance)
{
balance -= amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
}
else
cout << "insufficent amount";
break;
case 4:
cout << "please enter the amount" << endl;
cin >> amount;
if(amount <= balance)
{
balance -= amount;
balance2 += amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
}
else
cout << "insufficent amount";
break;
}
}
// Main func
int main1()
{
cout << "Please choose Your account " << endl;
int Account1, Account2;
cout << "Account 1 , Account 2" << endl;
char x;
cin >> x;
int option;
// Account 1 lines:Balance = 500 , balance 2= 700
do
{
if(x == Account1)
{
Menu();
cout << "option:" << endl;
cin >> option;
Task(500, 700, option);
}
// Account 2 lines : Balance = 700 , balance 2= 500
else
{
int option;
cout << "option:" << endl;
cin >> option;
Task(700, 500, option);
}
} while(option != 5);
}
im new to c++ and i did some coding which looks like like this and when i try to build the executable file i get this error: no return statement in function returning non void [-Wreturn-type] in line 40 and 64.i really have no idea what the problem is and i searched alot in the internet to understand what it is but i didn't understand the explanations at all.
It's a really simple solution. You created int function that never returns a value. If you have a function that you don't want any value to be returned, just make it void. To make your code work simply change function type from int to void
void Task(int balance, int balance2, int option)
The second issue is that you did declare balance1 and balance2, but you forgot to declare their values which lead to another error ( how is the compiler supposed to know their starting value?):
int Account1; Account2; // incorrect
int Account1=0, Account2=0; //fixed
Of course, you can also set these values later, but in this case, you should do it while declaring.
Another thing, why do you have int main1() function?
compiler will not treat it as you desire - it has to be called int main()
in order to do what you want.
You are missing "return balance" at the end of the Task() function. You define your function to return integer, that means you have to have return statement inside the function. Also, you have to have main() function, not main1() as in your case. Every C/C++ needs function that is called main() and it represent the entry point of the program. Try this code:
#include <iostream>
using namespace std;
// ATM menu
void Menu() {
cout << " MENU " << endl;
cout << "1.Deposit" << endl;
cout << "2.Balance" << endl;
cout << "3.Withdraw" << endl;
cout << "4.Transfer" << endl;
cout << "5.Exit" << endl;
cout << "-------------------------------" << endl;
}
// Tasks of ATM
int Task(int balance, int balance2, int option) {
int amount;
cin >> option;
switch (option) {
case 1:
cout << "please enter the amount" << endl;
cin >> amount;
balance += amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
case 2:
cout << balance << endl;
break;
case 3:
cout << "please enter the amount" << endl;
cin >> amount;
if (amount <= balance) {
balance -= amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
} else
cout << "insufficent amount";
break;
case 4:
cout << "please enter the amount" << endl;
cin >> amount;
if (amount <= balance) {
balance -= amount;
balance2 += amount;
cout << "your balance is now:" << balance << "$" << endl;
break;
} else
cout << "insufficent amount";
break;
}
return balance;
}
// Main func
int main() {
cout << "Please choose Your account " << endl;
int Account1, Account2;
cout << "Account 1 , Account 2" << endl;
char x;
cin >> x;
int option;
// Account 1 lines:Balance = 500 , balance 2= 700
do {
if (x == Account1) {
Menu();
cout << "option:" << endl;
cin >> option;
Task(500, 700, option);
}
// Account 2 lines : Balance = 700 , balance 2= 500
else {
int option;
cout << "option:" << endl;
cin >> option;
Task(700, 500, option);
}
} while (option != 5);
}
In the adding user section in the code below, I am unable to type any characters for the "Add another person?(y/n): " question. it just jumps back to entering age. How do I fix this?
I've tried to change ans into a string, implement a while loop to force the question to show up, and many other things. It just seems that nothing works and I've been trying it for the good part of two hours
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
char ans;
int people;
int option;
int count = 0;
struct data
{
string name;
int age;
char gender;
string comments;
}person[100];
// homescreen
homescreen:
cout << "Welcome to the Data Base!" << endl;
cout << endl;
// displaying all people
for (int list = 0; list < count; list++)
{
cout << list << ".) " << person[list].name << endl;
}
cout << endl;
cout << "[1] View Person" << endl;
cout << "[2] Add Person" << endl;
cout << "[3] Edit Person" << endl;
cout << "[4] Delete Person" << endl;
cout << "[5] Exit" << endl;
cout << "Choose Option: "; cin >> option;
// using options
while (option != 5)
{
if (option == 1)
{
view:
for (int list2 = 0; list2 < count; list2++)
{
cout << list2 << ".) " << person[list2].name << endl;
}
cout << endl;
cout << "Enter number of person you want: "; cin >> people;
system("cls");
cout << "Name: " << person[count].name << endl;
cout << "Age: " << person[count].age << endl;
cout << "Gender: " << person[count].gender << endl;
cout << "Comments: " << person[count].comments << endl << endl;
cout << "View another person?(y/n): "; cin >> ans;
if (ans == 'y')
{
system("cls"); goto view;
}
else if (ans == 'n')
{
system("cls"); goto homescreen;
}
}
if (option == 2)
{
add:
system("cls");
cout << "Name: "; cin >> person[count].name;
system("cls");
cout << "Age: "; cin >> person[count].age;
system("cls");
cout << "Gender(M/F/H): "; cin >> person[count].gender;
system("cls");
cout << "Comments: "; cin >> person[count].comments;
count++;
system("cls");
cout << "Add another person?(y/n): "; cin >> ans;
if (ans == 'y')
{
system("cls");
goto add;
}
else if (ans == 'n')
{
system("cls");
goto homescreen;
}
}
}
}
If you anybody can help me I'd be grateful
The goto statements in your code makes the program really good
spaghetti
structure and that is not
good.
Therefore, think instead of goto other options, such as infinite
while loop which will break once the user enters the n or moving
the code to the function.
Secondly what if you have not entered any persons and choosing the
option 1. You still output the attributes of the person as
count is initialized zero at least. Remember the attributes are
not initialized at this point. Accessing the uninitialized
variables will invoke undefined
behavior. Therefore,
provide a check (something like if(count > 0) )before you execute the code in option 1.
In addition to that, remember that
std::endl flushes the output buffer, and '\n' doesn't. Therefore, most of
the cases you might wanna use just
\n.
Last but not the least, use std::vector instead of the using C style arrays with some predefined size. What if the user has more than 100 inputs? The solution in C++ is std::vector, which can expand dynamically as its storage is handled automatically.
Following is a possible solution to your program, in which the comments will guide you through to the things that I mentioned above.
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
struct Data
{
std::string name;
int age;
char gender;
std::string comments;
Data(const std::string& n, int a, char g, const std::string& c) // provide a Constructor
:name(n), age(a), gender(g), comments(c)
{}
};
void debugMsg(const std::string& msg)
{
system("cls");
std::cout << "\n\n\t\t" << msg << "\n\n";
Sleep(3000);
}
int main()
{
std::vector<Data> person; // use std::vector to store the datas
while (true) // loop: 1
{
system("cls");
std::cout << "Welcome to the Data Base! \n\n";
std::cout << "[1] View Person\n";
std::cout << "[2] Add Person\n";
std::cout << "[3] Edit Person\n";
std::cout << "[4] Delete Person\n";
std::cout << "[5] Exit\n";
std::cout << "Choose Option: ";
int option; std::cin >> option;
switch (option) // use switch to validate the options
{
case 1:
{
while (true) // loop - 2 -> case 1
{
// if no data available to show -> just break the loop 2 and return to the outer loop(i.e, loop 1)
if (person.empty()) { debugMsg("No person available to show ....going to main manu...."); break; }
// otherwise: displaying all people
for (std::size_t index = 0; index < person.size(); ++index)
std::cout << index << ".) " << person[index].name << "\n";
std::cout << "\nEnter number of person you want: ";
std::size_t index; std::cin >> index;
// if the index is not valid -> just break the loop 2 and return to the outer loop(i.e, loop 1)
if (index < 0 || index >= person.size()) { debugMsg("Sorry, wrong index!... returning to the main menu......"); break; }
system("cls");
std::cout << "Name: " << person[index].name << std::endl;
std::cout << "Age: " << person[index].age << std::endl;
std::cout << "Gender: " << person[index].gender << std::endl;
std::cout << "Comments: " << person[index].comments << std::endl << std::endl;
std::cout << "View another person?(y/n): ";
char ans; std::cin >> ans;
if (ans == 'y') { system("cls"); continue; } // just continue looping
else if (ans == 'n') { break; } // this will break the loop 2 and return to the outer loop(i.e, loop 1)
else { debugMsg("Sorry, wrong option!... returning to the main menu......"); break; }
}
} break;
case 2:
{
while (true) // loop - 3 -> case 2
{
system("cls");
std::string name, comments; int age; char gender;
std::cout << "Name: "; std::cin >> name;
std::cout << "Age: "; std::cin >> age;
std::cout << "Gender(M/F/H): "; std::cin >> gender;
std::cout << "Comments: "; std::cin >> comments;
// simply construct the Data in person vector in place
person.emplace_back(name, age, gender, comments);
std::cout << "\n\nAdd another person?(y/n): ";
char ans; std::cin >> ans;
if (ans == 'y') { system("cls"); continue; }
else if (ans == 'n') { system("cls"); break; } // same as case 1
else { debugMsg("Sorry, wrong option!... returning to the main menu......"); break; }
}
} break;
case 3: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
case 4: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
case 5: return 0; // if its 5, just retun the main
default: break;
}
}
return 0;
}
As mentioned above, using "goto" is a bad style, so i would suggest structure your program a little. Below is my version.
Naturally, I did not add any checks and controls, the author will be able to do this on his own. But main logics should work. And, of course, it is better to use vector instead of static array.
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
enum options { OPT_VIEW = 1, OPT_ADD = 2, OPT_EDIT = 3, OPT_DELETE = 4, OPT_EXIT = 5 };
struct data
{
string name;
int age;
char gender;
string comments;
};
class App
{
private:
data person[100];
int count = 0;
public:
App();
void Run();
int HomeScreen();
void View();
void Add();
};
App::App() : count(0)
{}
void App::Run()
{
int option = HomeScreen();
while(option != OPT_EXIT)
{
switch(option)
{
case OPT_VIEW:
View();
break;
case OPT_ADD:
Add();
break;
}
option = HomeScreen();
}
}
int App::HomeScreen()
{
int option = 0;
cout << "Welcome to the Data Base!" << endl;
cout << endl;
// displaying all people
for(int list = 0; list < count; list++)
{
cout << list << ".) " << person[list].name << endl;
}
cout << endl;
cout << "[1] View Person" << endl;
cout << "[2] Add Person" << endl;
cout << "[3] Edit Person" << endl;
cout << "[4] Delete Person" << endl;
cout << "[5] Exit" << endl;
cout << "Choose Option: "; cin >> option;
return option;
}
void App::View()
{
char ans = 0;
do
{
int people = 0;
for(int list2 = 0; list2 < count; list2++)
{
cout << list2 << ".) " << person[list2].name << endl;
}
cout << endl;
cout << "Enter number of person you want: "; cin >> people;
system("cls");
cout << "Name: " << person[people].name << endl;
cout << "Age: " << person[people].age << endl;
cout << "Gender: " << person[people].gender << endl;
cout << "Comments: " << person[people].comments << endl << endl;
cout << "View another person?(y/n): "; cin >> ans;
}
while(ans == 'y');
system("cls");
}
void App::Add()
{
char ans = 0;
do
{
system("cls");
cout << "Name: "; cin >> person[count].name;
system("cls");
cout << "Age: "; cin >> person[count].age;
system("cls");
cout << "Gender(M/F/H): "; cin >> person[count].gender;
system("cls");
cout << "Comments: "; cin >> person[count].comments;
count++;
system("cls");
cout << "Add another person?(y/n): "; cin >> ans;
}
while(ans == 'y');
system("cls");
}
int main()
{
App program;
program.Run();
}
Not under standing looping for arrays. Looping through all of grab some or search. Can someone explain the process? Thanks in advance. Sorry if duplicate. I looked around and couldnt find a solid explaination that I could understand.
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void allContacts(string names[], string phones[])
{
cout << "Showing all contacts... Press Q to go back to main menu" << endl;
}
void addName(string names[], string phones[])
{
bool keepGoing;
string input;
beginning:
for (int i = 0; i < sizeof(names); i++)
{
cout << "Enter contact name: ";
cin >> names[i];
cout << "Enter contact number: ";
cin >> phones[i];
cout << "Do you have another contact to add? y or no" << endl;
cin >> input;
if(input == "y" || input == "Y")
{
goto beginning;
}
if(input == "n" || input == "N")
{
cout << "Contacts you have entered: " << endl;
cout << names[i] << " : " << phones[i] << endl;
}
}
}
void searchName(string names[], string phones[])
{
string name;
cout << "Enter Name: ";
cin >> name;
cout << "Search for a name or Press Q to go back to main menu" << endl;
for (int i = 0; i < sizeof(names); i++){
if (name == names[i])
{
cout << counter << names[i] << " 's phone number is: " << phones[i] << endl;
} else {
cout << "No results found";
}
}
}
int main()
{
string names[100];
string phones[100];
int choice;
cout << "============================" << endl;
cout << "=== Welcome to PhoneBook ===" << endl;
cout << "============================" << endl;
cout << "1- Add a New Contact" << endl;
cout << "2- Search By Name" << endl;
cout << "3- Display All" << endl;
cout << "0- Exit" << endl;
cout << "Select a number: " << endl;
cin >> choice;
switch(choice)
{
case 1:
addName(names, phones);
break;
case 2:
searchName(names, phones);
break;
case 3:
allContacts(names, phones);
break;
case 0:
cout << "Exiting PhoneBook...";
break;
}
}
In C++ arrays lose attributes when passed to functions. Those attributes are capacity and size (number of filled slots). You will need to pass this additional information for each array:
void addName(string names[], unsigned int names_capacity, unsigned int names_size,
string phones[], unsigned int phones_capacity, unsigned int phones_size)
To get around this, you can use std::vector. The std::vector knows its capacity and size, so you don't have to pass additional attributes to your function.
Also, if you use tolower or toupper before you compare, you only need to make one comparison:
char input;
cout << "Do you have another contact to add? y or n" << endl;
cin >> input;
input = toupper(input);
if(input == 'Y')
When using strings, you can convert them to all uppercase or all lowercase by using std::transform, such as:
std::transform(input.begin(),
input.begin(), input.end(),
tolower);
I am making a program with which to save banking information (i.e., account, password, balance). I am having trouble with a certain error but I'm not sure of the cause.
Here is the entire code.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int x = 0;
void addUser();
void login();
void deposit();
void withdrawl();
struct bankUser
{
double balance;
string account, password;
};
bankUser user[20];
int menu()
{
ofstream myfile("bankmachine.txt", ios::app);
char choice;
cout << "1) Add account" << endl;
cout << "2) Log in" << endl;
cout << "3) Make deposit" << endl;
cout << "4) Make withdrawl" << endl;
cout << "5) Quit" << endl << endl;
cout << "What would you like to do?: ";
cin >> choice;
cout << endl;
switch (choice)
{
case '1':
addUser();
break;
case '2':
login();
break;
case '3':
deposit();
break;
case '4':
withdrawl();
break;
case '5':
myfile << user[x].balance << endl;
myfile.close();
cout << "Thank you for using the banking system." << endl << endl;
system ("pause");
return 0;
break;
default:
cout << "That is not a valid option. Please choose again." << endl << endl;
menu();
}
}
void addUser()
{
if ((x >= 0) && (x < 20))
{
cout << "Please enter your desired account name: ";
cin >> user[x].account; // Account name.
cout << "Thank you, now please enter your desired password: ";
cin >> user[x].password; // Account password.
cout << "\nAccount created. You may now log in." << endl << endl;
ofstream myfile("bankmachine.txt", ios::app); // Opens the text file at the end of the file (if data is already present).
myfile << user[x].account << endl; // Writes to text file.
myfile << user[x].password << endl; // ^
myfile.close(); // Close text file (important).
x++; // Increases to simulate the addition of another user.
menu();
}
else // Will display if user has entered 20 users.
{
cout << "You have entered the maximum number of users." << endl;
menu();
}
}
void deposit()
{
int deposit;
string answer;
do
{
cout << "Please enter the amount of money that you would like to deposit: ";
cin >> deposit;
user[x].balance += deposit;
cout << "Thank you. Your new balance is " << user[x].balance << "." << endl << endl;
cout << "Would you like to make another deposit? (Y/N): ";
cin >> answer;
} while ((answer != "N") && (answer == "Y"));
cout << endl;
menu();
}
void withdrawl()
{
int withdraw;
string answer;
do
{
cout << "Please enter the amount of money that you would like to withdraw: ";
cin >> withdraw;
if (withdraw <= user[x].balance)
{
user[x].balance -= withdraw;
}
else
{
cout << "\nSorry, you do not have sufficient funds to complete this withdrawl.\nPlease try again." << endl << endl;
withdrawl();
}
cout << "Thank you. Your new balance is " << user[x].balance << "." << endl << endl;
cout << "Would you like to make another withdrawl? (Y/N): ";
cin >> answer;
} while (answer != "N" && answer == "Y");
cout << endl;
menu();
}
void login() // Function to log in.
{
string user, pw, usernameCheck, passwordCheck;
double balance;
cout << "Please enter your login information." << endl << endl;
cout << "Account name: ";
cin >> user;
cout << "Password: ";
cin >> pw;
cout << endl;
ifstream myfile("bankmachine.txt", ios::app);
while (!myfile.eof()) // Loops until end of file.
{
getline(myfile, usernameCheck);
if (usernameCheck == user)
{
getline(myfile, passwordCheck);
if (passwordCheck == pw)
{
myfile >> balance;
cout << "Login successful." << endl;
cout << "Your balance is " << balance << "." << endl << endl;
user[x].balance = balance;
}
else // If not, display:
{
cout << "Password incorrect." << endl << endl;
}
}
}
myfile.close(); // Close text file (important).
menu();
}
int main()
{
cout << "Welcome to the banking system." << endl << endl;
menu();
}
I keep getting this error (on line 172):
request for member 'balance' in 'user.std::basic_string<_CharT, _Traits,
_Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
std::allocator<char>](((unsigned int)x))', which is of non-class type 'char'|
What is this caused by? How can I fix it? Any answers are appreciated.
Judging by the error provided it seems as if user is not of type struct bankUser, but a std::string.
You are trying to assign a std::string (balance) to character at offset x of your std::string named user, which is doomed to fail.
TL;DR user is not declared to be a struct bankUser.