I am working at my programming project for school and i came up with idea of "Student Manager Program".
The: name, roll, year, group, course, adress, email, contact, grade are stored as variables in class named Student.
Student's data is written by user keyboard input and saved in studentRecord.txt file, by object.
How can i prevent this while loop to be a not infinite loop. I can't find solution to this problem.
'''
//Displaying Students Record Table
void Student::display() {
system("cls");
ifstream file;
int total = 1;
int x;
cout << "\n-----------------------------Students Record Table-----------------------------" << endl;
file.open("studentRecord.txt");
if (!file) {
cout << "\n\t\t\tNo Data Is Present.";
file.close();
}
else {
file >> name >> roll >> year >> group >> course >> adress >> email >> contact >> grade;
while(!file.eof()) {
cout << "\n\n\t\t\t Student Consecutive Number: " << total++ << endl;
cout << "\t\t\t Student's Name: " << name << endl;
cout << "\t\t\t Student's ID Number: " << roll << endl;
cout << "\t\t\t Student's Year: " << year << endl;
cout << "\t\t\t Student's Group: " << group << endl;
cout << "\t\t\t Student's Course: " << course << endl;
cout << "\t\t\t Student's Adress: " << adress << endl;
cout << "\t\t\t Student's Email: " << email << endl;
cout << "\t\t\t Student's Contact: " << contact << endl;
cout << "\t\t\t Student's Final Grade: " << grade << endl;
file >> name >> roll >> year >> group >> course >> adress >> email >> contact >> grade;
}
if (total == 0) {
cout << "\n\t\t\tNo Data Is Present.";
}
}
do {
cout << "\n\t\t\t If You Want to Back at previous page press 0 and Enter: ";
cin >> x;
if (x == 0) {
file.close();
}
} while (x != 0);
}
'''
Related
I have written a simple program in two ways. The program is for getting data from a user, storing it in a txt file, retrieving the data and displaying it. The problem is that one approach works while the other does not. I dont understand why.
The one that works is this:
#include <iostream>
#include <fstream>
using namespace std;
class customer{
public:
// Declaring member variables and functions
string name, address, telNo;
int age;
void createCustomer();
void displayInfo(string inputName);
};
// Member function of customer to enter new customer details
void customer::createCustomer(){
customer c;
ofstream file;
file.open("customers.txt", ios::out);
cout << "Enter Name: ";
cin >> c.name;
cout << "Enter Age: ";
cin >> c.age;
cout << "Enter Address: ";
cin >> c.address;
cout << "Enter Telephone Number: ";
cin >> c.telNo;
file.write((char*)&c, sizeof(c));
file.close();
cout << "\n";
}
// Member function of customer to display customer information
void customer::displayInfo(string inputName){
customer c;
ifstream file;
file.open("customers.txt", ios::in);
if(!file){
cout << "Could Not Open customers.txt File\n";
return;
}
while(!file.eof()){
file.read((char*)&c, sizeof(c));
if (inputName == c.name){
cout << "\n";
cout << "Name-------------> " << c.name << endl;
cout << "Age--------------> " << c.age << endl;
cout << "Telephone Number-> " << c.telNo << endl;
cout << "Address----------> " << c.address << endl;
cout << "\n";
break;
}
}
file.close();
}
int main(){
customer c;
c.createCustomer();
c.displayInfo("name");
return 0;
}
It gives the following output:
Enter Name: name
Enter Age: 21
Enter Address: add
Enter Telephone Number: telno
Name-------------> name
Age--------------> 21
Telephone Number-> telno
Address----------> add
The one that doesnt work is this:
#include <iostream>
#include <fstream>
using namespace std;
class customer{
public:
// Declaring member variables and functions
string name, address, telNo;
int age;
void createCustomer();
void displayInfo();
};
// Member function of customer to enter new customer details
void customer::createCustomer(){
customer c;
ofstream file;
file.open("customers.txt", ios::out);
cout << "Enter Name: ";
cin >> c.name;
cout << "Enter Age: ";
cin >> c.age;
cout << "Enter Address: ";
cin >> c.address;
cout << "Enter Telephone Number: ";
cin >> c.telNo;
file.write((char*)&c, sizeof(c));
file.close();
cout << "\n";
}
// Member function of customer to display customer information
void customer::displayInfo(){
string inputName = "name";
customer c;
ifstream file;
file.open("customers.txt", ios::in);
if(!file){
cout << "Could Not Open customers.txt File\n";
return;
}
while(!file.eof()){
file.read((char*)&c, sizeof(c));
if (inputName == c.name){
cout << "\n";
cout << "Name-------------> " << c.name << endl;
cout << "Age--------------> " << c.age << endl;
cout << "Telephone Number-> " << c.telNo << endl;
cout << "Address----------> " << c.address << endl;
cout << "\n";
break;
}
}
file.close();
}
int main(){
customer c;
c.createCustomer();
c.displayInfo();
return 0;
}
All I have done is put the string inputName variable inside the function instead of passing it as an argument.
It gives only the following output:
Enter Name: name
Enter Age: 21
Enter Address: add
Enter Telephone Number: telno
If I remove the if condition:
while(!file.eof()){
file.read((char*)&c, sizeof(c));
//if (inputName == c.name){
cout << "\n";
cout << "Name-------------> " << c.name << endl;
cout << "Age--------------> " << c.age << endl;
cout << "Telephone Number-> " << c.telNo << endl;
cout << "Address----------> " << c.address << endl;
cout << "\n";
break;
//}
}
i get the following output:
Enter Name: name
Enter Age: 21
Enter Address: add
Enter Telephone Number: telno
Name-------------> 0∙⌂┼
Age--------------> 21
Telephone Number-> name
Address----------> §
Why are the name and address fields outputting random symbols and the telephone number field outputting value of the name field?
Neither program is correct. It's an error use read on a type like customer because it contains sub-objects that need constructing, namely the string members name, address and telNo. Calling read does not call any constructor for the objects you are reading and so this is invalid.
Since read does not work for strings it doesn't make any sense to use write on a class containing a string either because you won't be able to read back what you have written.
If you want to write a customer to a file you can do it something like this
file << c.name << ' ' << c.age << ' ' << c.telNo << ' ' << c.address << '\n';
and then something similar for reading (although you then you would have to be careful of any spaces in your data).
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 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
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;
}