Run Time Error When Program is Running C++ - c++

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

Related

Code skips 6 lines of code on its own, what could be wrong here?

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)

Referencing vector causes error when running program

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;

c++ file reading into memory

i'm trying to load the file into an array when the program starts so i can
modify or search in it i don't know if my code works or not ( it's not reading the file )i have the file
and there's two books in
i have tried to debug it but couldn't find the problem the code works
but it think there's a problem with the load() function i don't know what
my code :
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct books{
//identfying books with all needed things
int id, status;
string title, p_name, p_address;
string date;
string aut_name, aut_nationality;
}newbook[10000], aut[10000];
int i = 0;
void load(){
ifstream myfile("books.txt", ios::in);
while (myfile >> newbook[i].id >> newbook[i].title >> newbook[i].p_name >> newbook[i].p_address >> aut[i].aut_name >> aut[i].aut_nationality >> newbook[i].date >> newbook[i].status)
i++;
}
void search_for_book(){
int temp = 0;
int idx;
cout << "enter the ID of the book you're looking for : ";
cin >> idx;
for (int srh = 0; srh < i; srh++){
if (newbook[srh].id == idx){
cout << setw(10) << "book found :" << endl;
cout << "title :" << newbook[srh].title << endl;
cout << "publisher name : " << newbook[srh].p_name << endl;
cout << "publisher address" << newbook[srh].p_address << endl;
cout << "author name :" << aut[srh].aut_name << endl;
cout << "author Nationality :" << aut[srh].aut_nationality << endl;
cout << "publish Date :" << newbook[srh].date << endl;
cout << "status :" << newbook[srh].status << endl;
temp++;
break;
}
else
srh++;
}
if (temp == 0){
cout << "couldn't find book" << endl << endl;
}
}
int main(){
load();
char choice;
cout << "enter your choice (3)";
cin >> choice;
if (choice == '3'){
search_for_book();
}
}
note :*( there are other functions like adding new book but not necessary to write )
*(i'm new to c++ don't really know how to read file into memory but i'm trying)
this is the code to save data into file :
void add_new_book_5(){
int booksnumber;
books newbook[1000], aut[100];
cout << "how many books you want to add ? ";
cin >> booksnumber;
cout << "what books you want to add :" << endl;
d_base.open(path, ios::out | ios::app);
for (int i = 0; i < booksnumber; i++){
cout << "id please : "; cin >> newbook[i].id;
cout << "title : "; cin.ignore(); getline(cin, newbook[i].title);
cout << "publisher name :"; getline(cin, newbook[i].p_name);
cout << "publisher address : "; getline(cin, newbook[i].p_address);
cout << "author" << " name : "; cin.ignore(); getline(cin, newbook[i].aut_name);
cout << "Nationality : "; getline(cin, newbook[i].aut_nationality);
cout << "Publish date :"; getline(cin, newbook[i].date);
cout << "How many copies of " << newbook[i].title << " "; cin >> newbook[i].status;
system("cls");
d_base << newbook[i].id << "\ " << newbook[i].title << "\ ";
d_base << newbook[i].p_name << "\ " << newbook[i].p_address << "\ ";
d_base << newbook[i].aut_name << "\ " << newbook[i].aut_nationality << "\ ";
d_base << newbook[i].date << "\ " << newbook[i].status << endl;
}
d_base.close();
cout << setw(76) << "Books Have Been Saved Sucessfully" << endl;
}

C++ LogIn.exe crashes in terminal

I am trying to create a LogIn screen in c++ in Microsoft Visual Studios 2013
the code works but after this:
cout << "Create account for user 1, 2, 3, 4, or 5?" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
p1.getData1();
}
The program will crash and show this:
Unhandled exception at 0x50E1DF58 (msvcp120d.dll) in LogIn.exe:
0xC0000005: Access violation reading location 0x008AD1D4.
Any help will be great... Thank you!!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class account
{
protected:
string username1;
string username2;
string username3;
string username4;
string username5;
string pswd1;
string pswd2;
string pswd3;
string pswd4;
string pswd5;
public:
int menuSelect;
void getData1()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username1;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd1;
cout << endl;
}
void getData2()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username2;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd2;
cout << endl;
}
void getData3()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username3;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd3;
cout << endl;
}
void getData4()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username4;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd4;
cout << endl;
}
void getData5()
{
cout << "Enter a username: (no spaces)" << endl;
cin >> username5;
cout << endl;
cout << "Enter a password: (no spaces)" << endl;
cin >> pswd5;
cout << endl;
}
void showData1()
{
cout << endl;
cout << "Your username is: " << username1 << endl;
cout << endl;
cout << "Your password is: " << pswd1 << endl;
cout << endl;
}
void showData2()
{
cout << endl;
cout << "Your username is: " << username2 << endl;
cout << endl;
cout << "Your password is: " << pswd2 << endl;
cout << endl;
}
void showData3()
{
cout << endl;
cout << "Your username is: " << username3 << endl;
cout << endl;
cout << "Your password is: " << pswd3 << endl;
cout << endl;
}
void showData4()
{
cout << endl;
cout << "Your username is: " << username4 << endl;
cout << endl;
cout << "Your password is: " << pswd4 << endl;
cout << endl;
}
void showData5()
{
cout << endl;
cout << "Your username is: " << username5 << endl;
cout << endl;
cout << "Your password is: " << pswd5 << endl;
cout << endl;
}
string getUsername1()
{
return username1;
}
string getUsername2()
{
return username2;
}
string getUsername3()
{
return username3;
}
string getUsername4()
{
return username4;
}
string getUsername5()
{
return username5;
}
string getPswd1()
{
return pswd1;
}
string getPswd2()
{
return pswd2;
}
string getPswd3()
{
return pswd3;
}
string getPswd4()
{
return pswd4;
}
string getPswd5()
{
return pswd5;
}
};
int accept()
{
int menuSelect;
cout << "Press 1 to do something" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
cout << "Do something 1" << endl;
cout << endl;
}
else
{
cout << "Error: Invalid Choice" << endl;
}
return 0;
}
int main()
{
account p1;
ifstream infile("DATA.DAT", ios::binary);
infile.read(reinterpret_cast<char*>(&p1), sizeof(p1));
char answer;
int menuSelect;
cout << "Have you created an account yet? y/n" << endl;
cin >> answer;
if (answer == 'n' || 'N')
{
cout << endl;
cout << "Create account for user 1, 2, 3, 4, or 5?" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
p1.getData1();
}
else if (menuSelect == 2)
{
p1.getData2();
}
else if (menuSelect == 3)
{
p1.getData3();
}
else if (menuSelect == 4)
{
p1.getData4();
}
else if (menuSelect == 5)
{
p1.getData5();
}
else
{
cout << "Error: Invalid Choice" << endl;
}
ofstream outfile("DATA.DAT", ios::binary);
outfile.write(reinterpret_cast<char*>(&p1), sizeof(p1));
}
else if (answer == 'y' || 'Y')
{
string nam;
cout << endl;
cout << "Please enter your username:" << endl;
cin >> nam;
cout << endl;
if (nam == p1.getUsername1() || nam == p1.getUsername2() || nam == p1.getUsername3() || nam == p1.getUsername4() || nam == p1.getUsername5())
{
string pwd;
cout << "Please enter your password:" << endl;
cin >> pwd;
cout << endl;
if (pwd == p1.getPswd1() || pwd == p1.getPswd2() || pwd == p1.getPswd3() || pwd == p1.getPswd4() || pwd == p1.getPswd5())
{
cout << "Access Granted" << endl;
cout << endl;
cout << "Press 1 to start program" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 1)
{
return accept();
}
}
else
{
cout << "Access Denied" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 0)
{
return main();
}
}
}
else
{
cout << "Access Denied" << endl;
cin >> menuSelect;
cout << endl;
if (menuSelect == 0)
{
return main();
}
}
}
else
{
cout << endl;
cout << "Error: Invalid Choice" << endl;
}
return 0;
}
You can't read p1 from file that way! p1 is a complex class where fields (strings) have non trivial initializations. If you save p1 to disk then you won't have text of string inside the file, but probably (depend on implementation) pointers to actual text content. When you read them you actually initialize the string with pointers to unallocated memory, hence the access violation.

when running program it has me enter two lines after name? help please

my program seems to want to enter two inputs for name variable instead of just entering one thing and moving on to phone number?
i'm sure its simple but can someone help me fix this please? is it something it do with the getline?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//define Car struct
struct Speaker
{
string name;
string phoneNumber;
string emailAddress;
string theme;
double fee;
};
Speaker *getSpeaker();
int main()
{
Speaker thespeaker;
thespeaker = *getSpeaker();
cout << "The speaker entered is!" << endl;
cout << thespeaker.name << endl;
cout << "phone number: " << thespeaker.phoneNumber << endl;
cout << "email: " << thespeaker.emailAddress << endl;
cout << "theme: " << thespeaker.theme << endl;
cout << "fees: " << thespeaker.fee << endl;
}
Speaker *getSpeaker()
{
Speaker *theSpeaker;
theSpeaker = new Speaker;
cout << "Please enter Speakers information" << endl;
cout << "name: " ;
getline(cin, theSpeaker->name);
cin.ignore(100, '\n');
cin.clear();
cout << theSpeaker->name;
cout << "\nphone number: ";
cin >> theSpeaker->phoneNumber;
cout << "\nEmail Address: ";
cin >> theSpeaker->emailAddress;
cout << "\nTheme: ";
cin >> theSpeaker->theme;
cout << "\nFee: ";
cin >>theSpeaker->fee;
return theSpeaker;
}
There's no need for cin.ignore();
Simply write it as:
Speaker *getSpeaker()
{
Speaker *theSpeaker;
theSpeaker = new Speaker;
cout << "Please enter Speakers information" << endl;
cout << "name: " ;
getline(cin, theSpeaker->name);
cout << theSpeaker->name;
cout << "\nphone number: ";
cin >> theSpeaker->phoneNumber;
cout << "\nEmail Address: ";
cin >> theSpeaker->emailAddress;
cout << "\nTheme: ";
cin >> theSpeaker->theme;
cout << "\nFee: ";
cin >>theSpeaker->fee;
return theSpeaker;
}