I want to design an account management system.I have an account class for storing the details of each account, a withdrawal class for storing the information regarding the money withdrawn from a particular account and an account list class with an array of accounts and member function add_acc,search_acc that searches for a particular account and show_acc that displays information of all the accounts. But before adding an account, I want to check if the account no. to be added, already exists or not. If it exists then an error message is displayed else the new account is added.But the show_acc function is not functioning, everytime it is called, it prints some garbage value.
#include <iostream>
#include <stdlib.h>
using namespace std;
#define MAX 100
void Withdrawal(class Account_list &a);
class Account
{
long long int acc_no;
long double balance;
public :
void get_data(long long int acc,long double b=0)
{
acc=acc_no;
b=balance;
}
void show_data()
{
cout<<"Account no. : "<<acc_no<<endl;
cout<<"Balance : "<<balance<<endl;
}
long long int get_acc_no()
{
return acc_no;
}
long double get_balance()
{
return balance;
}
void update(long double b)
{
balance=balance-b;
}
friend class Account_list;
};
class Account_list
{
static int count;
Account A[MAX];
public:
void add_acc();
void show_acc();
void search_acc(long long int);
friend void Withdrawal(Account_list &a);
};
int Account_list::count=0;
void Account_list::add_acc()
{
long long int acc;
long double b;
cout<<"Enter account number : ";
cin>>acc;
cout<<"Enter balance : ";
cin>>b;
for (int i = 0; i <= count; i++)
{
if (A[i].get_acc_no() == acc)
{
cout << "\nAccount already exists enter a unique account number\n";
return;
}
}
A[count].get_data(acc, b);
count++;
}
void Account_list::search_acc(long long int ac)
{
for(int i=0;i<count;i++)
{
if(A[i].get_acc_no()==ac)
{
A[i].show_data();
return;
}
}
cout<<"Account no. not found please enter a valid account no.";
}
void Account_list::show_acc()
{
for(int i=0;i<count;i++)
{
A[i].show_data();
cout<<endl<<endl;
}
}
void Withdrawal(Account_list &a)
{
long long int acc;
long double amount;
cout<<"Enter the account no. : ";
cin>>acc;
cout<<"Enter the amount to be withdrawn : ";
cin>>amount;
for(int i=0;i<a.count;i++)
{
if(a.A[i].get_acc_no()==acc && a.A[i].get_balance()>amount)
{
a.A[i].update(amount);
cout<<"Amount withdrawn successfully\n";
return;
}
}
cout<<"Account no. not found or invalid withdrawal amount entered";
}
int main()
{
Account_list A;
char ch='Y';
long long int acc;
int i;
while(ch)
{
cout<<"1. Add account"<<"\n"<<"2. Find account"<<"\n"<<"3. Withdraw amount"<<"\n"<<"4. Display information"<<"\n";
cout<<"Enter your choice : ";
cin>>i;
switch(i)
{
case 1: A.add_acc();
break;
case 2: cout<<"Enter the account no. to be searched : ";
cin>>acc;
A.search_acc(acc);
break;
case 3: Withdrawal(A);
break;
case 4: A.show_acc();
break;
default:cout<<"Enter a valid choice";
}
cout<<"Do you wish to continue?"<<" : ";
cin>>ch;
if(ch=='Y' || ch=='y')
{
cout<<endl;
continue;
}
else
break;
}
}
The problem with my output -:
In the first run I entered the account no. as 123, int the second run I enter the acc. no. as 345 and when in the third run again I entered the acc. no. as 345, the error message was not displayed.
Related
I'm trying to write a program that simulates an ATM. The user can log in with a pin and account number, check their account balance, and make a withdrawal. I'm having trouble initializing the array that contains the account information, here's what I have so far:
#include <iostream>
#include <string>
using namespace std;
class Account
{
private: int accountNum;
string accountPin;
double balance;
void setPin();
void setAccountNum();
public: Account ()//default constructor
{
accountNum = -1;
accountPin = -1;
balance = 0.0;
};
Account (int accountNum, string accountPin, double balance)
//overloaded construtor
{
accountNum = accountNum;
accountPin = accountPin;
balance = balance;
};
void setAccountBalance(double bal);//acc balance setter
int getAccountNum() //acc balance getter
{
return balance;
}
bool confirmPin(string)//confirm pin# func
{
}
void updateBalance(double)
};
int main ()
{
int option;
//accounts array
Account account[]= (123, "abc123", 100.00), (456, "def456", 50.00),(789,"ghi789", 500.63);
//login code, unfinished
cout << "LOGIN\nEnter Account#: "<< endl;
cin >> accNum;
cout << "Enter pin#: "<<endl;;
getline(accPin);
//menu do while loop, unfinshed
do {
cout << "1. Check balance\n2.Make a deposit\n3.Logout\n";
cin >> option;
switch (option)
//check balance case
case 1:
// make a deposit case
case 2:
}
while (option !=3);
return 0;
}
Line 48 is where the array needs to be initialized, it contains the account number, the pin code, and the account balance (in that order). Can anyone point out the mistake I'm making? Thanks for the help in advance.
You need curly braces around the entire initializer list, and also around the initializer for each element.
Account account[]= {
{123, "abc123", 100.00},
{456, "def456", 50.00},
{789,"ghi789", 500.63}
};
A basic student database using classes, I accept
name,
roll number, and
sgpa(cgpa equivalent but out of 10).
I used while loop, and switch case,but if the code for sgpa validation, and code for displaying all students with same sgpa is removed, the program works neatly, but if not, the program goes into continuous loop, the while loop inside the main, as it accepts the options itself and keeps doing it.
The code fails when i accept the sgpa(while taking the student data) or call the displaySGPA fucntion(option 3)
int totalStudents=0;
class database{
float sgpa;
int roll;
string name;
public:
void getSGPA();
void getData(database []);
friend void displaySGPA(int,database []);
};
//Display Students with same SGPA
void displaySGPA(int temp,database students[]){
int i,sameSGPA=0;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
sameSGPA+=1;
}
}
if(sameSGPA>1){
cout<<"\nStudents with SGPA "<<temp<<"."<<endl;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
cout<<" "<<students[i].roll<<" "<<students[i].name;
}
}
}
else if(sameSGPA==1){
cout<<"Only one student with the SGPA , "<<temp<<". :"<<endl;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
cout<<" "<<students[i].roll<<" "<<students[i].name;
break;
}
}
}
else{
cout<<"No Student with given SGPA."<<endl;
}
}
void database :: getData(database students []){
cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter Roll. ";
cin>>roll;
getSGPA();
}
//SGPA validation
void database :: getSGPA(){
int x=1,temp;
while(x==1){
cout<<"Enter SGPA : ";
cin>>temp;
if(temp<=10){
sgpa=temp;
break;
}
else{
cout<<"Please enter a valid SGPA.";
}
}
}
//main Loop
int main() {
int x,temp;
database students[50];
while (x!=5){
cout<<"\n1.Enter a New Student.\n3.Display students with same SGPA.\n5.Exit.\n\nYour Choice : ";
cin>>x;
switch(x){
case 1:
students[totalStudents].getData(students);
totalStudents++;
break;
case 3:
cout<<"\nEnter the SGPA.";
cin>>temp;
displaySGPA(temp,students);
break;
case 5:
cout<<"Exiting the program.....";
break;
default:
cout<<"Please select a valid option."<<endl;
}
}
}
I found the bug, it was a type conversion bug, i was accepting a float value but passing it as a int,just worked, don't know why.But thanks for each comment and suggestion.
I am currently studying c++ but I fell behind a little bit, so I apologize if my question is obvious.
I have to create a program that asks for a student's name, GPA, Year of admission, and get a random 5 digit number generated for that person. The number of students will not exceed 42.
My program compiled (somehow) and I am able to get the error for invalid menu selection, however, whenever I give a valid selection (currently 1) nothing happens.
Maybe I am missing something, this is why I need help.
Here is my code.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//print all the menu options
void print_menu()
{
cout<<"\nRCNJ Registrar Menu:"<<"\n"
<<"\n"
<<"[1] Add a student"<<"\n"
<<"[2] Display all students"<<"\n"
<<"[3] Display by year"<<"\n"
<<"[4] Display statistics"<<"\n"
<<"[5] Quit"<<"\n";
}
//get and return the student's name
void get_name(string& student_name) //call student_name after that.
{
cout<<"Please enter the sudent's name: ";
cin >> student_name;
cout<<"\n";
}
//validate and return gpa
double get_gpa()
{
double student_gpa = 0;
cout<<"Please enter the GPA: ";
cin >>student_gpa;
cout<<"\n";
while (student_gpa > 4 || student_gpa < 0)
{
cout<<"Please enter a valid GPA for the student (0.00 - 4.00): ";
cin >> student_gpa;
cout<<"\n";
}
return student_gpa;
}
//validateand return year
int get_year()
{
int student_year = 0;
cout<<"Please enter the year: ";
cin >> student_year;
cout<<"\n";
while (student_year >2016 || student_year <1972)
{
cout<<"Please enter a valid year (min 1972, max 2016): ";
cin >> student_year;
cout<<"\n";
}
return student_year;
}
//generate the student's R#
int generate_number()
{
int r_number;
srand (time(NULL));
r_number = rand() % 89999 + 10000;
return r_number;
}
//save info. Include get_name, get_gpa, get_year
void input_new_student()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int s_name, s_gpa, s_year, r_num;
get_name(student_name);
get_gpa();
get_year();
generate_number();
}
//display all students in the proper format
void print_all()
{
}
//get a year as selection and print all students that are the same year
void print_by_year()
{
}
//display statistics based on entered students
void print_statistics()
{
}
//validate and return the menu option selected by the user.
//it should call print_menu defined earlier
int get_selection(int menu_choice)
{
menu_choice = 0;
cout<<"\n"
<<"Selection: ";
cin >> menu_choice;
cout<<"\n";
while (menu_choice > 5 || menu_choice< 1)
{
cout<<" Menu choice is invalid. Please re-enter (1 - 5): ";
cin>> menu_choice;
cout<<"\n";
}
return menu_choice;
}
int main()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int menu_choice;
int s_name=0;
int s_gpa=0;
int s_year=0;
int r_num=0;
string nameArray[42];
s_name++;
double gpaArray[42];
s_gpa++;
int yearArray[42];
s_year++;
int ramapoArray[42];
r_num++;
print_menu();
get_selection(menu_choice);
switch (menu_choice)
{
case 1:
input_new_student();
nameArray[s_name] = student_name;
gpaArray[s_gpa] = student_gpa;
yearArray[s_year] = student_year;
ramapoArray[r_num] = r_number;
break;
}
return 0;
}
I dont have permission to comment, hence adding it here.
In you main(),
get_selection(menu_choice);
switch (menu_choice)
You return menu_choice, but there is none to take the value, you end you using garbage value as it is uninitialized.
So two ways you can do it, either by passing the address/reference of menu_choice or by return value. try either of these it should work, though I have not gone through the rest of your program.
As suggested by others, try a debugger e.g. gdb?
Why is the following code not giving results and how to get results?
Whenever I run the code, it first asks for the names of the players of two teams playing the match, then it shows the menu from which if we select any one of the option it again asks for the batsman name which is not according to the program designed. My research on the code and the problem is that I think buffer memory is full but I don't know how to free it, any help would be beneficial. Thank you
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class scorecard{
char batname[11][20];
int runscored[11];
char situation[11][10];
char mode[11][15];
char bowlername[11][20];
float oversplayed[11];
int maiden[11];
int runsgiven[11];
int wicketstaken[11];
public:
void updatebatsman(void);
void updatebowler(void);
void displaybat(void);
void displaybowl(void);
void menu(void);
scorecard()
{for(int n=0;n<12;n++)
{
runscored[n]={0};
oversplayed[n]={0};
maiden[n]={0};
runsgiven[n]={0};
wicketstaken[n]={0};
}
}
};
int main()
{
int jb=0;
scorecard s1;
int kb;
s1.menu();
do
{
cout<< "Enter the option"<<endl;
cout<<"(1) Display batting score"<<endl<<"(2) Display Bowling score"<<endl<<"(3) Update batting score"<<endl;
cout<<"(4) Update Bowling score"<<endl;
cin >>kb;
switch(kb)
{
case 1 : s1.displaybat();
break;
case 2 :s1.displaybowl();break;
case 3:s1.updatebatsman();break;
case 4:s1.updatebowler();break;
default:cout<<"Wrong choice";
}
}while (jb<1);
}
void scorecard::updatebowler(void)
{char bowlname[20];
int str,k,option,overnumbers,maidenumb,uprun,upwicket;
cout<<"Enter Bowler name:";
cin.getline(bowlname,20);
for( k=0;k<11;k++)
{str= strcmp(bowlername[k],bowlname);
if (str== 0)
{
cout<<"Menu for Bowler information update "<<endl;
cout<<"(1) Update Number of overs"<<endl<<"(2) Update maiden overs"<<endl<<"(3) Update runs given"<<endl;
cout<<"(4) Update wickets taken"<<endl;
cin >> option;
switch(option)
{
case 1:{cout<<"Enter Numbers of overs to be updated:";
cin >>overnumbers;
cout<<endl;
oversplayed[k]+=overnumbers;
break;
}
case 2:{cout <<"Enter the number of maiden overs to be updated:";
cin>>maidenumb;
cout<<endl;
maiden[k]+=maidenumb;
break;
}
case 3:{cout <<"Enter the number of runs to be added:";
cin>>uprun;
cout<<endl;
runsgiven[k]+=uprun;
break;
}
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
}
default:cout<<"wroung choice";
}
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::updatebatsman(void)
{char batsmaname[20];
int str,k;
cout<<"Enter Batsman name:";
cin.getline(batsmaname,20);
for( k=0;k<11;k++)
{str= strcmp(batname[k],batsmaname);
if (str== 0)
{
cout<<"enter runs scored:";
cin>>runscored[k];
cout<<endl<<"enter weather out or not out:";
cin>>situation[k];
cout<<endl<<"enter mode(if batsman out) by which batsman was out:";
cin>>mode[k];
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::displaybat(void)
{
cout << "Batsman name"<<'t'<<"Runs scored"<<'t'<<"situation"<<'t'<<"mode"<<endl;
for(int j=0;j++;j<12)
{
cout<<batname[j]<<'t'<<runscored[j]<<'t'<<situation[j]<<'t'<<mode[j]<<endl;
}
}
void scorecard::displaybowl(void)
{
cout << "Bowler name"<<'t'<<"overs played"<<'t'<<"maiden overs"<<'t'<<"wicket taken"<<'t'<<"Runs given"<<endl;
cout<<endl;
for(int j=0;j++;j<12)
{
cout<<bowlername[j]<<'t'<<oversplayed[j]<<'t'<<maiden[j]<<'t'<<wicketstaken[j]<<'t'<<runsgiven[j]<<endl;
}
}
void scorecard::menu(void)
{
cout<<"Enter the name of players of batting team"<<endl;
for (int k=0;k<11;k++)
{
cout <<"Enter name of player "<<k+1<<":";
cin>>batname[k];
}
cout <<"Enter the name of players of bowling team"<<endl;
for (int n=0;n<11;n++)
{
cout <<"Enter name of player "<<n+1<<":";
cin>>bowlername[n];
}
}
This is very wrong:
for(int j=0;j++;j<12)
It should be:
for(int j=0; j < 11; j++)
You are also missing a break in your case 4 statement for the options:
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
break;
}
default:cout<<"wroung choice";
Without the break you will see also the output wrong choice when the user selects option 4.
This is my simple code for a bookshop
There is nothing wrong with the code. I am using DevC++ to run the code and after compling it gives out an error which says 'gets' was not declared in this scope & the same error for puts. Please help me.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<iomanip>
#include<cstring>
using namespace std;
class Book
{
char *title,*author,*publisher,ans;
int price,quant,quant_ent;
public:
Book()
{
title = new char[50];
author = new char[50];
publisher = new char[50];
price = quant = quant_ent = 0;
}
void getdata()
{
cout<<"\nEnter The Title";
gets(title);
cout<<"\nEnter The Author";
gets(author);
cout<<"\nEnter The Publisher";
gets(publisher);
cout<<"\nEnter The Price";
cin>>price;
cout<<"\nEnter The Quantity";
cin>>quant;
}
void display()
{
cout<<setw(15)<<title<<setw(15)<<author<<setw(15)<<publisher<<setw(10)<<quant
<<setw(10)<<price;
}
void search(char search_title[],char search_author[])
{
if(strcmpi(author,search_author)==0)
{
if(strcmpi(title,search_title)==0)
{
cout<<"\nBook Found!";
cout<<"\nEnter The Quantity: ";
cin>>quant_ent;
if(quant_ent <= quant)
{
cout<<"\nThe Title is: ";
puts(title);
cout<<"\nThe Author is: ";
puts(author);
cout<<"\nThe Publisher is: ";
puts(publisher);
cout<<"\nPrice Of Single Copy: "<<price;
cout<<"\nTotal Price = "<<price*quant_ent;
quant = quant - quant_ent;
}
else
{
cout<<"\nSufficient Quantity Not Available!";
}
}
}
}
};
int main()
{
Book obj[10];
int i=0,ch;
char author[50],title[50];
for(;;)
{
cout<<"\n*******MENU********\n1)Enter Details\n2)Buy Book\n3)Display All Books\n4)Exit";
cin>>ch;
switch(ch)
{
case 1:
obj[i].getdata();
i++;
break;
case 2:
cout<<"\nEnter The Authors Name: ";
gets(author);
cout<<"\nEnter The Title: ";
gets(title);
for(int j=0;j<i;j++)
{
obj[j].search(title,author);
}
break;
case 3:
cout<<setw(15)<<"TITLE"<<setw(15)<<"AUTHOR"<<setw(15)<<"PUBLISHER"<<setw(15)<<"QUANTITY"<<setw(15)<<"PRICE";
cout<<"\n"<<setw(75)<<"-----------------------------------------------------------------------------------------------------";
for(int j=0;j<i;j++)
{
cout<<"\n";
obj[j].display();
}
case 4:
exit(1);
};
}
}
Because it's declared in stdio.h (cstdio in C++) header and you haven't included it.
But you shall not use gets. It's a hopelessly broken function. Use fgets instead. Even better, ditch the naked pointers to char arrays and use std::string class instead.