Student Management System C++ - c++

I have this program in C++ for Student Management System , Everything is working fine , except one place where i try to delete a student based on his roll number .
What it should do : After asking the roll number search the record and delete it
What is it doing : It deletes all the other records which do not match that roll number
Here is my code :
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
using namespace std;
int main() {
FILE *fp, *ft;
char another, choice;
struct student {
char first_name[50], last_name[50];
int roll_num; //new code added
char course[100];
int section;
};
struct student e;
char xfirst_name[50], xlast_name[50];
int xroll_num ; // new code added
long int recsize;
fp=fopen("users.txt","rb+");
if (fp == NULL) {
fp = fopen("users.txt","wb+");
if (fp==NULL)
{
puts("Cannot open file");
return 0;
}
}
recsize = sizeof(e);
while(1) {
system("cls");
cout << "\t\t====== STUDENT INFORMATION SYSTEM ======";
cout <<"\n\n ";
cout << "\n\n";
cout<<" \n\t\t\t======================";
cout << "\n \t\t\t 1. Add Records";
cout << "\n \t\t\t 2. List Records";
cout << "\n \t\t\t 3. Modify Records";
cout << "\n \t\t\t 4. Delete Records";
cout << "\n \t\t\t 5. Exit Program";
cout<<" \n\t\t\t======================";
cout << "\n\n";
cout << "\t\t\t Select Your Choice ::";
fflush(stdin);
choice = _getche();
switch(choice)
{
case '1' :
fseek(fp,0,SEEK_END);
another ='Y';
while(another == 'Y' || another == 'y')
{
system("cls");
cout << "Enter the First Name : ";
cin >> e.first_name;
cout << "Enter the Last Name : ";
cin >> e.last_name;
cout << "Enter the Course : ";
cin >> e.course;
cout << "Enter the Section : ";
cin >> e.section;
cout << "Enter the roll number :";
cin >> e.roll_num;
fwrite(&e,recsize,1,fp);
cout << "\n Add Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;
case '2':
system("cls");
rewind(fp);
cout << "=== View the Records in the Database ===";
cout << "\n";
while (fread(&e,recsize,1,fp) == 1){
cout << "\n";
cout <<"\nName :: " <<e.first_name <<" "<<e.last_name;
//cout << "\n";
cout <<"\nRoll Number :: " << e.roll_num ;
cout <<"\nCourse :: " <<e.course ;
cout <<"\nSection :: "<<e.section;
}
cout << "\n\n";
system("pause");
break;
case '3' :
system("cls");
another = 'Y';
while (another == 'Y'|| another == 'y')
{
// cout << "\n Enter the last name of the student : ";
cout << "\n Enter the Roll number of the student : ";
cin >> xroll_num;
rewind(fp);
while (fread(&e,recsize,1,fp) == 1)
{
//if (strcmp(e.last_name,xlast_name) == 0)
if(e.roll_num == xroll_num )
{
cout << "Enter the new Firt Name : ";
cin >> e.first_name;
cout << "Enter the new Last Name : ";
cin >> e.last_name;
cout << "Enter the new Roll Number : ";
cin >> e.roll_num;
cout << "Enter the new Course : ";
cin >> e.course;
cout << "Enter the new Section : ";
cin >> e.section;
fseek(fp, - recsize, SEEK_CUR);
fwrite(&e,recsize,1,fp);
break;
}
else
cout<<"record not found";
}
cout << "\n Modify Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;
case '4':
system("cls");
another = 'Y';
while (another == 'Y'|| another == 'y')
{
// cout << "\n Enter the last name of the student to delete : ";
cout <<"\n Enter the roll number of the student to delete : ";
cin >> xroll_num;
ft = fopen("temp.dat", "wb");
rewind(fp);
while (fread (&e, recsize,1,fp) == 1)
// if (strcmp(e.last_name,xlast_name) != 0)
if(e.roll_num == xroll_num )
{
fwrite(&e,recsize,1,ft);
}
fclose(fp);
fclose(ft);
remove("users.txt");
rename("temp.dat","users.txt");
fp=fopen("users.txt","rb+");
cout << "\n Delete Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;
case '5':
fclose(fp);
cout << "\n\n";
cout << "\t\t THANK YOU FOR USING THIS SOFTWARE";
cout << "\n\n";
exit(0);
}
}
system("pause");
return 0;
}

It deletes all the other records which do not match that roll number
Well you're only writing the records that match the roll number to the temp file, and then using that file to overwrite the users.txt file
if (e.roll_num == xroll_num) {
fwrite(&e, recsize, 1, ft);
}
I suppose what you really wanted to do is
if (e.roll_num != xroll_num) {
fwrite(&e, recsize, 1, ft);
}
You should probably read a good C++ i/o tutorial, as your code is mostly C. Consider writing your student struct as simple text instead of writing it wholesale to the file.

Related

How to ignore .txt for cin for output file in C++?

This is my C++ code and I wish to create an output file. In order to create the output file, I request the user to include .txt at this input, cin >> accnum ; , hence, when output file is produced, it will show accnum.txt as plain text. My question is, what should I do in order to ignore .txt to be appeared in the text file, provided that I must input .txt(this is to produce txt file, without .txt at the input, no output file will be produced). I have put a comment line, kindly refer to that line. Thanks for your help!
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
double withdraw,deposit,number,transfer,num,amt;
string name,first_name,middle_name,last_name;
int x=0;
int option;
int A = 300;
int B = 500;
int C,W,y;
string accnum;
ifstream infile;
ofstream outfile;
do {
cout <<" Welcome to ATM Machine "<<endl;
cout <<"\n\t****Menu****";
cout <<"\n\t* 1.Creating a new account *";
cout <<"\n\t* 2.Cash Deposit *";
cout <<"\n\t* 3.Cash withdrawal *";
cout <<"\n\t* 4.Fund transfer between two account *";
cout <<"\n\t* 5.Exit *";
cout <<"\n\t* *";
cout <<"\n\t********";
cout <<"\n\t Please choose an option: ";
cin>>option;
switch(option){
case 1:
cout <<"Press 1 to Confirm" << endl << "Press 0 to exit " << endl ;
cin >> C ;
{
if( C == 1 )
{
cout << "Enter first name: " << endl;
cin >> first_name;
cout << "Enter middle name: " << endl;
cin >> middle_name;
cout << "Enter last name: " << endl;
cin >> last_name;
name = first_name + " " + middle_name + " " + last_name;
cout << "enter a 4 digit number : " << endl << "gentle reminder : please add .txt at the end of your number" << endl << "e.g 1234.txt" << endl;
cin >> accnum ; //how to ignore .txt
cout << "What is your primary balance: " << endl;
cin >> amt;
infile.open(name);
outfile.open(name);
cout << "your account has been created " << endl ;
outfile << "name" << name << endl;
outfile << "account no. : " << accnum << endl ;
outfile << "current balance : RM" << amt << endl ;
}else {
cout << "Bye, have a nice day";
}
infile.close();
outfile.close();
}
break;
case 2:
cout<<"Deposit amount: "<<endl;
cin>>deposit;
if(deposit<10) {
cout<<"The smallest note acceptable is RM10."<<endl;
}else {
x=static_cast<int>(deposit)%10;
if(x!=0)
{
cout<<"Invalid deposit amount"<<endl;
cout<<"Example of deposit amount: RM10, RM20, RM50, RM320..."<<endl;
}else {
A+=deposit;
cout<<"current balance: RM"<< A <<endl;
}
}
break;
case 3:
cout<<"Amount you want to withdraw"<<endl;
cin>>withdraw;
if(A < withdraw || withdraw < 10) { // A is balance
cout <<"Failed to withdraw money! Please try again."<<endl;
}else {
A-=withdraw;
cout <<"current balance: "<< A <<endl;
}
break;
case 4:
infile.open("1212.txt");
infile >> x;
cout<<"Enter Details of transfering Account(Account A)"<<endl;
cout<< "Your account number is:"<<endl;
cin>>W;
cout<< "What is your name:"<<endl;
cin.ignore();
getline(cin,name);
cout<<"Enter Details of receiving Account(Account B)"<<endl;
cout<<"Enter account number:"<<endl;
cin>>W;
cout<<"Enter name:"<<endl;
cin.ignore();
getline(cin, name);
cout <<"your account balance is : " << x << endl << "how much do you want to transfer ? ";
cin >> num ;
outfile.open("1212.txt");
outfile << x - num ;
cout << "your account balance is : " << x - num ;
infile.close();
outfile.close();
infile.open("2121.txt");
infile >> y ;
outfile.open("2121.txt") ;
outfile << y + num ;
infile.close();
outfile.close();
break;
case 5:
cout<<"Thank you for using ATM machine. Bye, have a nice day !"<<endl;
return 0;
default:
if(option != 5) cout<<"Invalid option.Please try again!"<<endl;
break;
}
}while(option != 5);
system("pause");
return 0;
}
Just create the file without a format, the name will be the file_name content exactly, be aware that in windows has reserved names such as CON.
#include <iostream>
#include <fstream>
#include <string>
int main (void) {
std::string file_name = "1234";
std::fstream f_handle;
f_handle.open(file_name, std::fstream::out);
if (f_handle.is_open()){
f_handle << "Some text" << std::endl;
f_handle.close();
}
f_handle.open(file_name, std::fstream::in);
if (f_handle.is_open()){
std::cout << f_handle.rdbuf() << std::endl;
f_handle.close();
}
return 0;
}
You asked about how to remove the .txt part from accnum and here's one way to do that:
cin >> accnum ; //how to ignore .txt
// check that the length/size of the entered accnum is at least 4 characters
// and
// that the last 4 characters is ".txt"
if(accnum.size() >= 4 && accnum.substr(accnum.size() - 4) == ".txt") {
// extract a substring from accnum without the last 4 characters
accnum = accnum.substr(0, accnum.size() - 4);
}
However, I have my doubts about the requirement that the user should actually add .txt to the account number, because seeing this at an ATM would be really confusing:
enter a 4 digit number :
gentle reminder : please add .txt at the end of your number"
e.g 1234.txt
It's especially confusing since the only thing you want to do with .txt is to remove it. I would consider changing this user interface.
Suggestions:
Let the user add the account number without .txt
Instead of saving the account information in a file with the account holders name as the filename, save it in a file named accnum + ".txt". This makes it possible for a person to have more than one account - just as in real life.

Trying to make an bank management System but having errors

The things that i would like to accomplish is the functions of methods of the applications but getting many errors from the code which i don't completely understand and try to solve but came up with nothing.
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
class bank
{
char name [100],add[100],y;
int balance, amount;
public:
void open_account();
void deposite_money();
void withdraw_money();
void display_account();
};
void bank:open_account()
{
cout << "Enter your full name: ";
cin.ignore();
cin.getline(name,100);
cout << "What type of account you want to open savings (s) or current (c)";
cin >> y;
cout << "Enter amount of deposite: ";
cin >> balance;
cout << "Your account is created ";
}
void bank:deposite_money()
{
int a ;
cout << "Enter how much money you want to deposit: ";
cin >> a;
balance += a;
cout << "Your total deposit amount\n ";
}
void bank:display_account()
{
cout << "Enter the name: "<<name<<endl;
cout << "Enter your address: "<<add<<endl;
cout << "Type of account that you open: "<<y<<endl;
cout << "Amount you deposite: "<<balance<<endl;
}
void bank:withdraw_money()
{
cout << "Withdraw: ";
cout << "Enter the amount of withdrawing";
cin >> amount;
balance = balance - amount;
cout << "Now your total amount left is" <<balance;
}
int main()
{
int ch,x,n;
bank obj;
do
{
cout <<"01")open account\n";
cout <<"02")deposite money\n";
cout <<"03")withdraw money\n";
cout <<"04")display account\n";
cout <<"05")Exit\n";
cout << "Please select from the options above";
cin>>ch;
switch(ch)
{
case 1:"01")open account\n";
obj.open_account();
break;
case 2:"02")deposite money\n";
obj.deposite_money();
break;
case 3:"03")withdraw money\n";
obj.withdraw_money();
break;
case 4:"04")display account\n";
obj.display_account();
break;
case 5:
if(ch == 5)
{
cout << "Exit";
}
default:
cout<< "This is not the proper exit please try again";
}
cout << "\ndo you want to select the next step then please press : y\n";
cout << " If you want to exit then press : N";
x=getch();
if(x == 'y' || x == 'Y');
cout<< "Exit";
}
while (x == 'y' || x == 'Y');
getch();
return 0;
}
The errors that i am getting are
error stray '\'
error missing terminating character
error found ':' in nested name specifier
expected ; before ')' token
These are the errors that usually appears on the logs and has repeated a few times in different lines please help me so that i can finish this application i have learned a lot from making it and is looking forward to build more before my school starts Any help and suggestions on what i should do next is appreciated and advices on what i should do next will greatly be welcome for me to learn from this experience
Im using c++ btw and am trying to build some projects any advice for next time? How can improve myself after this errors and what should i next practice on like a next project that you guys would suggest me
See here:
#include <stdio.h>
#include <iostream>
using namespace std;
class bank
{
char name [100],add[100],y;
int balance, amount;
public:
void open_account();
void deposite_money();
void withdraw_money();
void display_account();
};
void bank::open_account()//: missing
{
cout << "Enter your full name: ";
cin.ignore();
cin.getline(name,100);
cout << "What type of account you want to open savings (s) or current (c)";
cin >> y;
cout << "Enter amount of deposite: ";
cin >> balance;
cout << "Your account is created ";
}
void bank::deposite_money()//: missing
{
int a ;
cout << "Enter how much money you want to deposit: ";
cin >> a;
balance += a;
cout << "Your total deposit amount\n ";
}
void bank::display_account()//: missing
{
cout << "Enter the name: "<<name<<endl;
cout << "Enter your address: "<<add<<endl;
cout << "Type of account that you open: "<<y<<endl;
cout << "Amount you deposite: "<<balance<<endl;
}
void bank::withdraw_money()//: missing
{
cout << "Withdraw: ";
cout << "Enter the amount of withdrawing";
cin >> amount;
balance = balance - amount;
cout << "Now your total amount left is" <<balance;
}
int main()
{
int ch,x,n;
bank obj;
do
{
cout <<"01)open account\n";//Delete " after Number
cout <<"02)deposite money\n";
cout <<"03)withdraw money\n";
cout <<"04)display account\n";
cout <<"05)Exit\n";
cout << "Please select from the options above";
cin>>ch;
switch(ch)
{
case 1:"01)open account\n";//Delete " after Number
obj.open_account();
break;
case 2:"02)deposite money\n";
obj.deposite_money();
break;
case 3:"03)withdraw money\n";
obj.withdraw_money();
break;
case 4:"04)display account\n";
obj.display_account();
break;
case 5:
if(ch == 5)
{
cout << "Exit";
}
default:
cout<< "This is not the proper exit please try again";
}
cout << "\ndo you want to select the next step then please press : y\n";
cout << " If you want to exit then press : N";
cin >> x;
if(x == 'y' || x == 'Y');
cout<< "Exit";
}
while (x == 'y' || x == 'Y');
}
Also don't use using namespace in headers, and in overall learn a better style. This is 80s C with C++ mixed in. Use containers not built in arrays, use iostream and overall get a good C++ book.

C++ Program crashes/infinite looping if a letter is inputted as an answer instead of a number [duplicate]

Sorry if I fail to be clear enough or make any mistakes, this is my first time posting.
My code runs without errors when complied but the first while loop (in int main) gets stuck looping whenever a user types a letter (like "a") for cin >> select; instead of the required 1, 2, or 3.
However, when I input "4" or any other random string of numbers, it runs fine and goes to my error message like it should.
Why is this and what can I do to make it run normally? (run the error message in response to letters entered as if they were numbers).
My code:
#include <iostream>
#include <string>
using namespace std;
void calculator();
void unavailableitem();
int main()
{
string select;
while (true)
{
cout << "\t[Main Menu]\n";
cout << " 1. Calculator\n";
cout << " 2. [unavailable]\n";
cout << " 3. [unavailable]\n";
cout << "\n Enter the number of your selection: ";
cin >> select;
if (select == "1")
{
cout << endl;
calculator();
break;
}
else if (select == "2")
{
unavailableitem();
break;
}
else if (select == "3")
{
unavailableitem();
break;
}
else
cout << "\nInvalid response.\n";
}
}
void unavailableitem()
{
string react;
cout << "\n \t [ITEM UNAVAILABLE]\n";
while (true)
{
cout << "\nEnter 'menu' to return to main menu: ";
cin >> react;
if (react == "menu")
{
cout << endl;
main();
break;
}
else
cout << "\nInvalid response.\n";
}
}
void calculator()
{
int choice;
double num1;
double num2;
double answer;
string choicesymbol;
cout << "List of operations:\n";
cout << " 1. Addition\n";
cout << " 2. Subtraction\n";
cout << " 3. Multiplication\n";
cout << " 4. Division\n";
cout << "Enter the number on the left to pick an operation: ";
cin >> choice;
cout << "\nEnter number 1: ";
cin >> num1;
cout << "\nEnter number 2: ";
cin >> num2;
if (choice == 1)
{
answer = num1 + num2;
choicesymbol = " + ";
}
if (choice == 2)
{
answer = num1 - num2;
choicesymbol = " - ";
}
if (choice == 3)
{
answer = num1 * num2;
choicesymbol = " * ";
}
if (choice == 4)
{
answer = num1 / num2;
choicesymbol = " / ";
}
cout << endl;
cout << num1 << choicesymbol << num2 << " = " << answer;
}
New code:
#include <iostream>
#include <string>
using namespace std;
void calculator();
void unavailableitem();
int main()
{
int select;
while (true)
{
cout << "\t[Main Menu]\n";
cout << " 1. Calculator\n";
cout << " 2. [unavailable]\n";
cout << " 3. [unavailable]\n";
cout << "\n Enter the number of your selection: ";
cin >> select;
if(!(cin >> select))
{
cout << "Input must be an integer.\n";
cin.clear();
continue;
}
else if (select == 1)
{
cout << endl;
calculator();
break;
}
else if (select == 2)
{
unavailableitem();
break;
}
else if (select == 3)
{
unavailableitem();
break;
}
}
}
void unavailableitem()
{
string react;
cout << "\n \t [ITEM UNAVAILABLE]\n";
while (true)
{
cout << "\nEnter 'menu' to return to main menu: ";
cin >> react;
if (react == "menu")
{
cout << endl;
return;
break;
}
else
cout << "\nInvalid response.\n";
}
}
void calculator()
{
int choice;
double num1;
double num2;
double answer;
string choicesymbol;
cout << "List of operations:\n";
cout << " 1. Addition\n";
cout << " 2. Subtraction\n";
cout << " 3. Multiplication\n";
cout << " 4. Division\n";
cout << "Enter the number on the left to pick an operation: ";
cin >> choice;
cout << "\nEnter number 1: ";
cin >> num1;
cout << "\nEnter number 2: ";
cin >> num2;
if (choice == 1)
{
answer = num1 + num2;
choicesymbol = " + ";
}
if (choice == 2)
{
answer = num1 - num2;
choicesymbol = " - ";
}
if (choice == 3)
{
answer = num1 * num2;
choicesymbol = " * ";
}
if (choice == 4)
{
answer = num1 / num2;
choicesymbol = " / ";
}
cout << endl;
cout << num1 << choicesymbol << num2 << " = " << answer;
}
Ad Ed Heal mentioned, the issue here is cin's failbit. When you do cin >> choice, and the user types "a", then the conversion to int fails. This sets cin's failbit, making all future reads from it fail until the failbit is cleared. So the next time you reach cin >> choice, the user won't even get to type anything.
You can use cin.clear() to restore to working order.
To do this a bit more robustly, you could do something like
while(true)
{
cout >> "Enter choice [1-4]: ";
if(!(cin >> choice))
{
cout << "Input must be an integer.\n";
cin.clear();
continue;
}
do_stuff_with_choice();
}
I am a newbie to programming in general, but playing with your code and looking up stuff made me find some sort of solution.
The cin.clear only clears the error log of the input, and I believe that it still retains the value of the letter.
What you should add right after is a cin.ignore(#,'\n') (# being a very, very large number) to have it avoid the line and skip right through it.
Found the solution in another question that explains the use of both cin commands.

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.

Type casting error in Visual C++

The following code is illustrating data file handling in text mode.
Here's my code:
#include "stdafx.h"
#include "fstream"
#include "conio.h"
#include "string"
#include "iostream"
using namespace std;
int count = 0;
class student
{
char name[50], grade;
float mks;
public:
student()
{
mks = 0;
}
void getdata();
void putdata();
void moddata();
};
void student :: getdata()
{
cout << "Enter data for record number: " << "(++count)" << ": ";
cout << "\nEnter name of the student: ";
cin >> name;
cout << "\nEnter marks: ";
cin >> mks;
cout << "\nEnter grade: ";
cin >> grade;
_getch();
}
void student :: putdata()
{
cout << "\nDisplaying data of required record: ";
cout << "\nName: " << name << "\nMarks: " << mks << "\nGrade: " << grade;
_getch();
}
void student :: moddata()
{
cout << "Enter correct details:-" << endl;
cout << "Enter name: ";
cin >> name;
cout << "\nEnter marks: ";
cin >> mks;
cout << "\nEnter grade: ";
cin >> grade;
_getch();
}
int _tmain(int argc, _TCHAR* argv[])
{
fstream stu ("stud.txt", ios::in | ios::out);
student s1;
char ans = 'y';
int ch, offset, mrec;
do
{
cout << "\nMAIN MENU:" << endl;
cout << "1. Add Record" << endl;
cout << "2. Modify Record" << endl;
cout << "3. Display Record" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice (1-4): ";
cin >> ch;
switch (ch)
{
case 1: s1.getdata();
stu.write ( (char*) &s1, sizeof (student) );
break;
case 2: if (!count)
{
cout << "No record added yet. Type option number first.";
_getch();
break;
}
cout << "Enter the record number to be modified: ";
cin >> mrec;
if (mrec > count)
{
cout << "Error. Only " << count << " records have been added.";
_getch();
break;
}
offset = (mrec - 1)* sizeof (student);
stu.seekg (offset);
stu.read ( (char*) &s1, sizeof (student) ); //C2440
s1.putdata();
cout << "Do you want to modify your record (y/n): ";
cin >> ans;
if (ans == 'y' || ans == 'Y')
{
s1.moddata();
stu.seekg(offset);
stu.write ( (char)* &s1, sizeof (student) );
break;
}
break;
case 3: if (!count)
{
cout << "No record added yet. Type option number first.";
_getch();
break;
}
cout << "Enter record number to be displayed: ";
cin >> mrec;
if (mrec > count)
{
cout << "Error. Only " << count << " records have been added.";
_getch();
break;
}
offset = (mrec - 1)*, sizeof (student);
stu.seekg (offset);
stu.read ( (char)* &s1, sizeof (student) ); //C2440
s1.putdata();
break;
case 4: break;
default: cout << "Wrong choice.";
}
} while (ch != 4);
_getch();
return 0;
}
I'm getting error number
C2440: 'type cast' : cannot convert from 'student' to 'char' 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
This seems to be a very weird problem. I don't have much experience with C++, so I can't really come with a solution to this.
Don't try to convert a student to a char *. Why would you do that?
Have an auxiliary char * which you can use as a parameter for read(), and then set the data in student.
It might seem that the cast works now, as hmjd suggested, but I urge you not to do it. It just happens that the character array is the first member of the class and it just happens that it's at the same address in memory as the object. But all hell will break lose if you change the order in which the members are declared, or introduce virtual functions, or even use a different compiler.
Don't cast.
you should use the member of s1 object that you want to modify, not the object itself