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;
}
Related
I have a problem with my code, it skips 6 lines of code, and I don't know what the problem could be. I'm just practicing C++, making a bank app, and in the registration menu it skips 6 lines of code for some reason. I would appreciate any help or suggestion! The code can look a little dirty. I don't understand why the code skips the input for the cityAddress, stateAddress, zipAddress after I type the houseAddress input.
// Registration menu code
void registerMenu() {
bool registerSuccess = false;
bool usernameSuccess = false;
string saveInfo;
system("CLS"); // Clear Console
cout << "Please create your account.\n";
cout << "First Name: ";
cin >> firstName;
cout << "Last Name: ";
cin >> lastName;
cout << "Phone Number: ";
cin >> phoneNumber;
cout << "Address: ";
cin >> houseAddress;
cout << "City: ";
cin >> cityAddress;
cout << "State: ";
cin >> stateAddress;
cout << "Zip code: ";
cin >> zipAddress;
cout << "\n\n";
cout << "Save information?\nY/N\n";
cin >> saveInfo;
if (saveInfo == "Y") {
cout << "-----------INFORMATION SAVED!-----------\n";
}
else if (saveInfo == "N") {
registerMenu();
}
else {
registerMenu();
}
cout << "\n\n";
do {
cout << "Username: ";
cin >> username;
ifstream usernameCheck("user_" + username + ".txt");
if (usernameCheck.is_open()) {
cout << "This username already exists. Create a different username.\n\n";
Sleep(1000);
}
else {
cout << "\n\t! USERNAME AVAILABLE !\n";
usernameSuccess = true;
}
} while (!usernameSuccess);
do {
cout << "Password: ";
cin >> password;
if (password.length() >= 8) {
cout << "Initial deposit to your account: $";
cin >> balance;
system("CLS"); // Clear Console
cout << "Registration complete!\n";
// [START] Create Account file
ofstream registration;
registration.open("user_" + username + ".txt");
registration << username << endl << password << endl << balance;
registration.close();
// [FINISH] Create Username file
registerSuccess = true;
password = password;
Sleep(1000);
system("CLS"); // Clear Console
cout << "--------------------------------" << endl;
cout << " Account Information\n";
cout << "Username: " << username << endl;
cout << "Password: " << password << endl;
cout << "Balance: $" << balance << endl;
cout << "--------------------------------" << endl;
cout << "Forwarding you in 5 seconds..." << endl;
Sleep(5000);
mainMenu();
}
else {
cout << "\n\nPassword must contain at least 8 characters. (You entered " << password.length() << " characters)\nPlease try again.\n";
}
} while (!registerSuccess);
}
Home addresses usually have spaces in them. Cin reads up to the first delimiter which is a space. Instead, try
std::getline(cin, houseAddress)
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 trying to make a program that will take a users input to make multiple forms. I am stuck on trying to get the vector (that will be filled with objects of the form class that the user creates) to be use-able in other functions. When I use the address-of operator (&) it gives me this error when the program gets to letting the user input the data to the objects.
This is the screen capture of the program and the error.
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Form {
public:
string Fname;
string Lname;
string City;
string Street;
string State;
string ZipCode;
};
void menuMain();
void menu1st(vector<Form> &Fvect);
void menu1st(vector<Form> &Fvect)
{
int MainM;
int n;
cout << "NEW FORM(s)" << endl;
cout << "Enter the number of forms you would like to make (Maximum of 5): "; cin >> n; cout << endl;
for (int i = 0; i < n; i++)
{
cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
cout << "City: "; cin >> Fvect[i].City; cout << endl;
cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
cout << "State: "; cin >> Fvect[i].State; cout << endl;
cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
}
cout << "Enter 1 to go back to main: "; cin >> MainM;
if (MainM == 1)
{
menuMain();
}
else
{
cout << "Error not a correct input." << endl;
}
}
void menu2nd()
{
int MainM;
//int Fnum;
vector<Form> Fvect;
cout << "EDIT A FORM" << endl;
cout << Fvect[1].Fname;
cout << "Enter the ";
cout << "Enter 1 to go back to main: "; cin >> MainM;
if (MainM == 1)
{
menuMain();
}
else
{
cout << "Error not a correct input." << endl;
}
}
void menuMain()
{
int Pnum;
cout << "INFORMATION FORMATTING PROGRAM" << endl;
cout << "1. Create new form's." << endl;
cout << "2. Edit a form." << endl;
cout << "3. Print forms." << endl;
cout << "4. Erase a form." << endl;
cout << "5. Exit Program." << endl;
cout << "Enter the action you want to take (1-5): "; cin >> Pnum;
vector<Form> Fvect;
if (Pnum == 1)
{
menu1st(Fvect);
}
if (Pnum == 2)
{
menu2nd();
}
else
{
cout << "Error not a correct input." << endl;
}
}
int main()
{
menuMain();
}
You are accessing Fvect using an invalid index in the following lines:
cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
cout << "City: "; cin >> Fvect[i].City; cout << endl;
cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
cout << "State: "; cin >> Fvect[i].State; cout << endl;
cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
Consequently, your program has undefined behavior.
You need to have items in a std::vector before you can access an item from it using the array syntax. What you need to do is:
Read the data to an object of type Form.
Add the object to the std::vector.
Replace those lines with:
Form form;
cout << "First Name: "; cin >> form.Fname; cout << endl;
cout << "Last Name: "; cin >> form.Lname; cout << endl;
cout << "City: "; cin >> form.City; cout << endl;
cout << "Street: "; cin >> form.Street; cout << endl;
cout << "State: "; cin >> form.State; cout << endl;
cout << "Zip Code: "; cin >> form.ZipCode; cout << endl;
Fvect.push_back(form);
PS
I am not sure why you have the cout << endl; in those lines. You don't need them. It will be sufficient to use:
cout << "First Name: "; cin >> form.Fname;
cout << "Last Name: "; cin >> form.Lname;
cout << "City: "; cin >> form.City;
cout << "Street: "; cin >> form.Street;
cout << "State: "; cin >> form.State;
cout << "Zip Code: "; cin >> form.ZipCode;
I am getting a runtime error when the program is running it takes the username but then when it comes for password it shows me: Debug Error Run Time Check Failure #3-T.
#include <iostream>
using namespace std;
int main()
{
int choice;
float username, password; //login
int name, age, gender, dob, address, workinfo;
cout << "Welcome To HDFC Bank" << endl;
//Menu Option
cout << "Choose an option: " << endl;
cout << "===========================" << endl;
cout << "1. Login" << endl;
cout << "2. Register" << endl;
cout << "===========================" << endl;
cin >> choice;
if (choice == 1) {
cout << "Please Enter Your Username: " << endl;
cin >> username;
cout << "Please Enter your Password: " << endl;
cin >> password;
if (choice == 1 || password = 2) {
cout << "Welcome To The Program!!!" << endl;
}
else {
cout << "Wrong Details!!" << endl;
}
}
else if (choice == 2) {
cout << "Enter Your Full Name: " << endl;
cin >> name;
cout << "Enter Your Age" << endl;
cin >> age;
cout << "Enter Your Date of Birth(dd/mm/yyyy): " << endl;
cin >> dob;
cout << "Enter Your Gender(M/F)" << endl;
cin >> gender;
cout << "Enter Your Address: " << endl;
cin >> address;
cout << "Enter Your Work Details: " << endl;
cin >> workinfo;
}
if (age < 21) {
cout << "Sorry You cannot Register as you are below 21 years. Please try later." << endl;
}
else {
cout << "You have succesfully registered. Please check your email." << endl;
}
return 0;
}
You should definitely learn more about Types and STL Streams.
But assuming you're experimenting here is little bit more meaningful version of your code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int choice = 0;
std::string username, password; //login
int age = 0;
std::string name, gender, dob, address, workinfo;
cout << "Welcome To HDFC Bank" << endl;
//Menu Option
cout << "Choose an option: " << endl;
cout << "===========================" << endl;
cout << "1. Login" << endl;
cout << "2. Register" << endl;
cout << "===========================" << endl;
cin >> choice;
cin.ignore();
if (choice == 1) {
cout << "Please Enter Your Username: " << endl;
getline(cin, username);
cout << "Please Enter your Password: " << endl;
getline(cin, password);
if (password == "1" || password == "2") {
cout << "Welcome To The Program!!!" << endl;
}
else {
cout << "Wrong Details!!" << endl;
return 0;
}
}
else if (choice == 2) {
cout << "Enter Your Full Name: " << endl;
getline(cin, name);
cout << "Enter Your Age: " << endl;
cin >> age;
cin.ignore();
cout << "Enter Your Date of Birth(dd/mm/yyyy): " << endl;
getline(cin, dob);
cout << "Enter Your Gender(M/F)" << endl;
getline(cin, gender);
cout << "Enter Your Address: " << endl;
getline(cin, address);
cout << "Enter Your Work Details: " << endl;
getline(cin, workinfo);
}
if (age < 21) {
cout << "Sorry You cannot Register as you are below 21 years. Please try later." << endl;
}
else {
cout << "You have successfully registered. Please check your email." << endl;
}
return 0;
}
Note that we use cin.ignore() after reading int as described here
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.