data validation in c++ for integer input - c++

void addItem(struct InventoryItem inventory[], int& size){
cout << "\nEnter the item name: ";
cin.getline(inventory[size].itemName, 100, '\n');
while (strlen(inventory[size].itemName) == 0){
cout << "Invalid item name!" << endl;
cout << "Enter item name: ";
cin.getline(inventory[size].itemName, MAX_CHAR, '\n');
}
cout << "Enter the item price: ";
cin >> inventory[size].itemPrice;
while(!cin || inventory[size].itemPrice < 0){
cout << "Please enter a valid number: ";
cin >> inventory[size].itemPrice;
clear();
}
cout << endl;
size++;
}
My !cin keeps accepting letters as numbers ...
any ideas why?
note: I'm new to programming.

If operator>> fails to extract a value, it puts the input stream into an error state. You are not clearing that state, or ignoring the bad input that caused it to fail, so operator>> keeps failing.
Try this instead:
void addItem(struct InventoryItem inventory[], int& size){
do {
cout << "Enter the item name: ";
if (cin.getline(inventory[size].itemName, MAX_CHAR, '\n')) {
if (strlen(inventory[size].itemName) > 0) {
break;
}
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid item name!" << endl;
}
while (true);
do {
cout << "Enter the item price: ";
if (cin >> inventory[size].itemPrice) {
if (inventory[size].itemPrice >= 0) {
break;
}
}
else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Invalid item price!" << endl;
}
while (true);
cout << endl;
size++;
}

Related

program skips over user input

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;
}

I've read a lot about the 2d array but I'm having troubles using it on my assignment

Here is the link to the assignment
I managed to get the majority of the project working except the void finalSales() function. I'm not too sure how to go about it with a one and two-dimensional array and display it accordingly. I have a midterm exam coming up next week and this chapter is included. I added some comments on what the functions do below:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
bool isPlaying = true, isPlaying_Two = true, isPlaying_Three = true, isLooping = true;
string salesPerson[6] = { "Jericho Rosales", "lisa Soberano", "Kim Chiu", "maja Salvador", "Katheryn Bern", "Daniel Padilla" },
items[3] = { "Washing Machine", "Refrigerator", "Music System" };
double table[6][3], totalSales[6] = {0};
int salesNum, productNum;
double saleAmount;
void Sales();
void person();
void prod();
void finalSales();
int main()
{
while (isLooping) {
Sales();
}
}
//main game
void Sales() {
while (isPlaying)
{
person();
if (salesNum > 0 && salesNum < 7) {
//cout << "debug " << salesPerson[salesNum - 1];//debug
cout << "\n";
while (isPlaying_Two)
{
prod();
if (productNum > 0 && productNum < 4) {
//cout << "debug " << items[productNum - 1];//debug
while (isPlaying_Three)
{
finalSales();
}
}
else
{
cout << "\n";
cout << "Input out of range, please try again\n";
cout << "\n";
}
}
}
else {
cout << "\n";
cout << "Input out of range, please try again\n";
cout << "\n";
}
}
}
//user selects which salesperson
void person() {
cout << "Sales Registry\n";
for (int i = 0; i < 6; i++)
{
cout << i + 1 << ". " << salesPerson[i] << "\n";
}
cout << "Select the Salesperson by typing his ordinal number: ";
cin >> salesNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> salesNum;
}
}
//user selects which product
void prod() {
for (int i = 0; i < 3; i++)
{
cout << i + 1 << ". " << items[i] << "\n";
}
cout << "Select the product by typing the product ordinal number: ";
cin >> productNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> productNum;
}
}
//get the sales amount(enter a random number in) and
//if the user wants to enter more sales it goes back to the beginning and asks again while remembering the previous values
//if not then the program adds up the amount (if there is more than one) and displays it accordingly to the salesperson and the product
void finalSales() {
string again;
cout << "Enter the sales amount: ";
cin >> saleAmount;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
cout << "Do you want to enter more sales? Type y for yes and n for no: ";
cin >> again;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
if (again == "y" || again == "Y") {
Sales();
}
else {
cout << "\n";
cout << "Salesperson" << "\t\t" << "Washing Machine" << "\t\t" << "Refrigerator" << "\t\t" << "Music system\n";
cout << "****************************************************************************************************";
cout << "\n";
for (int x = 0; x < 6; x++)
{
cout << salesPerson[x];
for (int y = 0; y < 3 ; y++)
{
cout << "\t\t\t" << table[x][y];
}
cout << "\n";
}
}
}
You need to return index from person() and prod() then pass them to finalSales. Also pass array to it and increase the amount of product by the saleAmount. And don't forget to make your bool variables false or you end up with infinite loop. Create two int variables in Sales(). Initialize them by returning the index from person() and the other one from product().
void finalSales(int idx1, int idx2,double table[6][3]) {
string again;
cout << "Enter the sales amount: ";
cin >> saleAmount;
table[idx1][ idx2] = saleAmount;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
cout << "Do you want to enter more sales? Type y for yes and n for no: ";
cin >> again;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
if (again == "y" || again == "Y") {
Sales();
}
else {
::isPlaying_Three = false;
::isPlaying_Two = false;
::isPlaying = false;
::isLooping = false;
cout << "\n";
cout << "Salesperson" << "\t\t" << "Washing Machine" << "\t\t" << "Refrigerator" << "\t\t" << "Music system\n";
cout << "****************************************************************************************************";
cout << "\n";
for (int x = 0; x < 6; x++)
{
cout << salesPerson[x];
for (int y = 0; y < 3; y++)
{
cout << "\t\t\t" << table[x][y];
}
cout << "\n";
}
}
}
That's for person() and the same for prod().
int person() {
cout << "Sales Registry\n";
for (int i = 0; i < 6; i++)
{
cout << i + 1 << ". " << salesPerson[i] << "\n";
}
cout << "Select the Salesperson by typing his ordinal number: ";
cin >> salesNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> salesNum;
}
return salesNum-1;
}
Don't forget to decrease index by 1.

malloc: *** error for object 0x00: pointer being freed was not allocated

I've created this small Player Management System. But getting this error whenever I search or update information of a player. Also, I'm not doing any dynamic memory allocation, so I am not sure why there is a problem of freeing the pointer.
a.out(1599,0x10802e5c0) malloc: *** error for object 0x7fd4d0d000c0: pointer being freed was not allocated
a.out(1599,0x10802e5c0) malloc: *** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Segmentation fault: 11 is also printed out.
Player.cc
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
fstream f;
class Player
{
public:
string name;
string dob;
string bowling_skill;
string batting_hand;
string country;
string team;
int runs;
int fours;
int sixes;
void info()
{
cout << "Name: " << this->name << "\n";
cout << "Date of Birth: " << this->dob << "\n";
cout << "Bowling Skill: " << this->bowling_skill << "\n";
cout << "Batting hand: : " << this->batting_hand << "\n";
cout << "Country: " << this->country << "\n";
cout << "Team: " << this->team << "\n";
cout << "Runs: " << this->runs << "\n";
cout << "No. of fours: " << this->fours << "\n";
cout << "No. of sixes: " << this->sixes << "\n\n";
}
};
void searchPlayer(string name)
{
Player player;
int found = 0;
f.open("Database/Player.dat", ios::in | ios::binary);
if (!f)
{
cerr << "\nOops! The file failed to open.\n";
exit(1);
}
while ((f.read((char *)&player, sizeof(player))))
{
if (player.name == name)
{
player.info();
found = 1;
break;
}
}
if (!found)
cout << "\nPlayer doesn't exist.\n";
f.close();
}
void addPlayer()
{
f.open("Database/Player.dat", ios::out | ios::binary | ios::app);
if (!f)
{
cerr << "Oops! The file failed to open.\n";
exit(1);
}
Player input;
cout << "Name: ";
cin.ignore(100, '\n');
getline(cin, input.name);
cout << "Date of Birth (e.g. 02/10/2001): ";
getline(cin, input.dob);
cout << "Bowling Skill (e.g. Good): ";
getline(cin, input.bowling_skill);
cout << "Batting Hand (e.g. Right): ";
getline(cin, input.batting_hand);
cout << "Country: ";
getline(cin, input.country);
cout << "Team: ";
getline(cin, input.team);
cout << "Runs: ";
cin >> input.runs;
cout << "No. of fours: ";
cin >> input.fours;
cout << "No. of sizes: ";
cin >> input.sixes;
f.write((char *)&input, sizeof(input));
f.close();
cout << "\nWriting...Done\n\n";
}
void updatePlayer(string name)
{
Player player;
string value;
int option;
f.open("Database/Player.dat", ios::in | ios::out | ios::binary | ios::ate);
if (!f)
{
cerr << "Oops! The file failed to open.\n";
exit(1);
}
f.seekg(0);
while (f.read((char *)&player, sizeof(player)))
{
if (player.name == name)
{
cout << "\nWhat do you want to update?\n";
cout << "1 Name\n";
cout << "2 Date of Birth\n";
cout << "3 Bowling Skill\n";
cout << "4 Batting Hand\n";
cout << "5 Country\n";
cout << "6 Team\n";
cout << "7 Runs\n";
cout << "8 No. of fours\n";
cout << "9 No. of sixes\n\n";
cout << "OPTION: ";
cin >> option;
switch (option)
{
case 1:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.name = value;
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
case 2:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.dob = value;
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
case 3:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.bowling_skill = value;
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
case 4:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.batting_hand = value;
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
case 5:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.country = value;
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
case 6:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.team = value;
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
case 7:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.runs = stoi(value);
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
case 8:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.fours = stoi(value);
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
case 9:
f.seekp(-sizeof(player), ios::cur);
cin.ignore(100, '\n');
cout << "\nNew value: ";
getline(cin, value);
player.sixes = stoi(value);
f.write((char *)&player, sizeof(player));
cout << "\nUpdating...Done\n\n";
return;
}
}
}
f.close();
}
Main.cc
#include <iostream>
#include "Classes/Player.cc"
using namespace std;
void playerMenu()
{
while (1)
{
string input;
int option;
cout << "1 Search\n";
cout << "2 Add\n";
cout << "3 Update\n";
cout << "4 Delete\n";
cout << "5 Back to main menu\n\n";
cout << "OPTION: ";
cin >> option;
system("clear");
switch (option)
{
case 1:
cout << "Enter name of the player: ";
cin.ignore(100, '\n');
getline(cin, input);
cout << "\n";
searchPlayer(input);
break;
case 2:
addPlayer();
break;
case 3:
cout << "Enter name of the player: ";
cin.ignore(100, '\n');
getline(cin, input);
updatePlayer(input);
break;
case 5:
return;
default:
cout << "Please choose a valid option\n";
}
}
}
int main()
{
while (1)
{
int option;
cout << "1 Player\n\n";
cout << "OPTION: ";
cin >> option;
system("clear");
switch (option)
{
case 1:
playerMenu();
break;
default:
cout << "\nPlease choose a valid option\n";
}
}
}
Any help you can provide is appreciable. Thanks
You're writing and reading your Player objects directly as their binary representation, but they contain non-POD data such as std::string, which contains pointers inside. This is a guaranteed ticket to UndefinedBehaviour-land.
You must change your input/output routines so that they serialise the Player object in some sensible way, such as storing the length of each string followed by its contents, and reading appropriately.

Program stuck in an infinite loop

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.

Need help to figure why my array its not saving an input?

so this is a ship's database and my problem is that I have to check if the user enter an integer, but when the program saves registry to the array and I show the result the registry variable where the user puts the number its an scramble of numbers
int tester1 = 0;
cout << "What is the registry of the ship?: ";
cout << " >>";
if (cin >> tester1)
{
tester1 >> array[counter].registry;
cin.clear();
cin.ignore(1000, '\n');
}
else
{
while (!(cin >> tester1))
{
cout << "Please enter an integer!!" << endl;
cout << " >>";
cin >> tester1;
cin.clear();
cin.ignore(1000, '\n');
}
}
cin>>tester1;
while(!cin) {
cin.clear();
cin.sync();
cout << "Please enter an integer!!" << endl;
cout << " >>";
cin>>tester1;
}
array[counter].registry=tester1;