banking management code returning junk values (using uml techniques) - class

my project code on banking doesnt work properly. when i enter the branch and department info as i run (void department) in main .
and i display(void display) them junk values are returned,i think the values are not going in the linked list.
code made in code blocks
part of the code below:
#include<iostream>
#include<conio.h>
#include<cstdio>
#include<string.h>
#include<cstdlib>
#include<malloc.h>
using namespace std;
int code=1100;
int acc=9900;
int loan_no=3300;
int emp=10;
void createmenu();
void depositormenu();
void employee();
void loanmenu();
void operationmenu();
class branch
{
char bname[20];
int bcode;
branch *link;
public:
branch()
{
bcode=code++;
}
void input()
{
fflush(stdin);
cout<<"\nEnter Branch Name: ";
gets(bname);
}
void output()
{
cout<<"\nBranch Name: "<<bname;
cout<<"\tBranch Code: "<<bcode;
}
};
class department
{
char dname[30];
int dcode;
public:
branch *b;
department *link;
department()
{
b=new branch;
}
void input()
{
b->input();
fflush(stdin);
cout<<"Enter Department Code: ";
cin>>dcode;
fflush(stdin);
cout<<"Enter Department Name: ";
gets(dname);
}
void output()
{
b->output();
fflush(stdout);
cout<<"\nDepartment Name: "<<dname;
cout<<"\tDepartment Code: "<<dcode;
}
};
void departments(department **);
void display(department **);
department *start,*p=NULL;
void departments(department **start)
{
department *temp,*r,*temp2;
temp2=*start;
system("cls");
cout<<"\nEnter data for Departments->";
department d;
if(*start==NULL)
{
temp=(department*)malloc(sizeof(department));
d.input();
temp=&d;
temp->link=NULL;
*start=temp;
}
else
{
temp=*start;
while(temp->link!=NULL)
{
temp=temp->link;
}
r=(department*)malloc(sizeof(department));
r->input();
r->link=NULL;
temp->link=r;
}
}
void display(department **start)
{
department *temp;
temp=*start;
fflush(stdout);
if(temp==NULL)
{
cout<<"\nList not created!";
cout<<"\tPress any key to return";
getch();
}
else
{
while(temp)
{
temp->output();
temp=temp->link;
getch();
}
}
}
pls help.
thanks in advance

Since I can't see your input values and expected output values, I can't say for sure. However, since you've opted to using pointers for everything, the one thing I see is with this block of code in the departments function:
department d;
if(*start==NULL)
{
temp=(department*)malloc(sizeof(department));
d.input();
temp=&d; // you are assigning the address of a local variable to temp,
// and then assigning it to the argument pointer.
temp->link=NULL;
*start=temp;
}
If you assign the address of a local variable to an argument pointer and then reference it from outside the function, you will have lost that data.
My suggestion would be to assign the contents of the department to the start object, rather than use the local pointer address.
That is likely why you're seeing garbage data.
I'm surprised the compiler didn't say anything about that.

Related

Segmentation fault in header file

I have defined a Parent.h file, having Parent class with following data members:-
#pragma once
#include <string>
using namespace std;
class Person
{
private:
string id;
string name;
string email;
int contact_number;
string address;
public:
Person();
~Person();
void set_id(string);
void set_name(string);
void set_email(string);
void set_contact_number(int);
void set_address(string);
string get_id();
string get_name();
string get_email();
int get_contact_number();
string get_address();
};
Person::Person()
{
this->id = "";
this->name = "";
this->email = "";
this->contact_number = 0;
this->address = "";
}
void Person::set_id(string id)
{
this->id =id;
}
I have defined the rest of the functions(setters and getters) likewise in Parent.h file.
After that I am making a child class in Student.h header file, this Student class will publicly inherit the Parent class
#pragma once
#include"Courses.h" //course is aggregating to a student
#include"AcademicRecord.h"
#include "Person.h"
#include "Department.h"
#include<string>
#include<vector>
using namespace std;
class Student:public Person,public AcademicRecord,public Department
{
private:
string institute_email;
public:
Courses A;//A contains all information about the courses a student has opt for.
Student();
~Student();
//Using polymorphism between methods
void ShowStudentAcademicRecord(); //shows the academic record of current student
void ShowStudentCoursesInfo(); //getting the information for all courses of student
//setter
void set_institute_email(string email);
void setDepartmentID(string);
//getter
string get_institute_email();
};
After that I have a Display.h file having vector of Students as its Data member and using a member function in Display, I am asking user to input data members like ID,Name,Address,... for individual students.
#include "Student.h"
using namespace std;
class Display
{
public:
vector<Student> students;
Display();//default constructor
//setter
void setStudentData();
};
void Display::setStudentData()
{
int size;
cout<<"Enter the number of students: ";
cin>>size;
Student *current; //current Student
for (int i = 0; i < size; i++)
{
current = &students[i];
cout<<"For student "<<i+1<<"\nEnter the following: \n";//For general details
cout<<"ID Name Email Contact_Number Address Institute_Email\n";
string ID,Name,EMail,Address,insti_email;
int contact_number;
cin>>ID>>Name>>EMail>>contact_number>>Address>>insti_email;
current->set_id(ID);
current->set_name(Name);
current->set_email(EMail);
current->set_contact_number(contact_number);
current->set_address(Address);
current->set_institute_email(insti_email);
}
And when I try inserting value insides students by executing main.cpp file.
I get segmentation fault at run-time inside Person class at setter function, where the first setter I have declared inside the class is void set_id(string).
#include "Display.h"
#include<vector>
#include<string>
int main()
{
Display display;
display.setStudentData();
}
Error:-
I tried:-
1.Rechecking the setter function for any error
2.Bring all the classes inside the main.cpp file,instead of header files, but still the error continues.
I expected the program to take the desired input for various data members of student elments of vector students inside Display class.
Instead I got a seg-fault.
Please if anyone can tell me what I have done wrong, or do I have to do malloc somewhere, inside a function?
As Nate Eldredge, has said I should use push_back instead of accessing ith element of students vector in setStudentData() function, as initially during default declaration of a Display class, the students vector is empty. Hence trying to access students[i] in current = &students[i], I am getting segmentation fault.
The appropriate code for Display.h should be then
#include "Student.h"
using namespace std;
class Display
{
public:
vector<Student> students;
Display();//default constructor
//setter
void setStudentData();
};
void Display::setStudentData()
{
int size;
cout<<"Enter the number of students: ";
cin>>size;
total_students += size;
Student *temp =new Student(); //temp Student
for (int i = 0; i < size; i++)
{
cout<<"For student "<<i+1<<"\nEnter the following: \n";//For general details
cout<<"ID Name Email Contact_Number Address Faculty_Type Faculty_Description\n";
string ID,Name,EMail,Address,insti_email;
int contact_number;
cin>>ID>>Name>>EMail>>contact_number>>Address>>insti_email;
temp->set_id(ID);
temp->set_name(Name);
temp->set_email(EMail);
temp->set_contact_number(contact_number);
temp->set_address(Address);
temp->set_institute_email(insti_email);
cout<<"Enter the Course Details: \n";//For Course Details
int size_course;
cout<<"Enter the number of courses: ";
cin>>size_course;
cout<<"Enter the following for each course: \n";
cout<<"Course_Name Marks Attendance Percentage \n";
string name;
int marks;
float attendance;
for (int i = 0; i < size_course; i++)
{
cin>>name>>marks>>attendance;
temp->A.set_courses_enrolled(name);
temp->A.set_course_wise_marks(marks);
temp->A.set_coursewise_attendance_percentage(attendance);
}
cout<<"Enter the Academic Record Details: \n";//For Academic Record Details
string program_name; int admission_no, enroll_no, begin_year, end_year, credits; float CGPA;
cout<<"Enter Program_name, admission_no enroll_no begin_year end_year credits CGPA";
cin>>program_name>>admission_no>>enroll_no>>begin_year>>end_year>>credits>>CGPA;
setStudentAcademicRecord(temp,i,program_name,admission_no,enroll_no,begin_year,end_year,credits,CGPA); //passing temp pointer in this one
students.push_back(*temp); //adding a new student in students vector
}

Can Kaitai Struct be used to describe TLV data without creating new types for each field?

I'm reverse engineering a file format that stores each field as TLV blocks (type, length, value).
The fields do not have to be in order, or even present at all. Their presence is denoted with a sentinel, which is a 16-bit type identifier and a 32-bit end offset. There are hundreds of unique identifiers, but a decent chunk of those are just single primitive values. aside from denoting the type, they can also identify what field the data should be stored in.
It is also worth noting that there will never be a duplicate id on a parent structure. The only time is can occur is if there are multiple of the same object type in an array/list.
I have successfully written a Kaitai definition for one of them:
meta:
id: struct_02ea
endian: le
seq:
- id: unk_00
type: s4
- id: fields
type: field_block
repeat: eos
types:
sentinel:
seq:
- id: id
type: u2
- id: end_offset
type: u4
field_block:
seq:
- id: sentinel
type: sentinel
- id: value
type:
switch-on: sentinel.id
cases:
0xF0: u1
0xF1: u1
0xF2: u1
0xF3: u1
0xF4: u4
0xF5: u4
size: sentinel.end_offset - _root._io.pos
Handling things this way does work, and I could likely map out the entire format like this. However, when it comes time to compiling this definition into another format, things get nasty.
Since I am wrapping each field in a field_block, the generated code stores these values in that type of object. This is incredibly inefficient when half of the generated field_block objects store a single integer. It would also require the consuming code to iterate through a list of each field block in order to get the actual field's value.
Ideally, I would like to define this structure so that the sentinels are only parsed while Kaitai is reading the data, and each value would be mapped to a field on the parent structure.
Is this possible? This technology is really cool, and I'd love to use it in my project, but I feel like the overhead that this is generating is a lot more trouble than it's worth.
Here's an example of the definition when compiled into C#:
using System.Collections.Generic;
namespace Kaitai
{
public partial class Struct02ea : KaitaiStruct
{
public static Struct02ea FromFile(string fileName)
{
return new Struct02ea(new KaitaiStream(fileName));
}
public Struct02ea(KaitaiStream p__io, KaitaiStruct p__parent = null, Struct02ea p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root ?? this;
_read();
}
private void _read()
{
_unk00 = m_io.ReadS4le();
_fields = new List<FieldBlock>();
{
var i = 0;
while (!m_io.IsEof) {
_fields.Add(new FieldBlock(m_io, this, m_root));
i++;
}
}
}
public partial class Sentinel : KaitaiStruct
{
public static Sentinel FromFile(string fileName)
{
return new Sentinel(new KaitaiStream(fileName));
}
public Sentinel(KaitaiStream p__io, Struct02ea.FieldBlock p__parent = null, Struct02ea p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
_read();
}
private void _read()
{
_id = m_io.ReadU2le();
_endOffset = m_io.ReadU4le();
}
private ushort _id;
private uint _endOffset;
private Struct02ea m_root;
private Struct02ea.FieldBlock m_parent;
public ushort Id { get { return _id; } }
public uint EndOffset { get { return _endOffset; } }
public Struct02ea M_Root { get { return m_root; } }
public Struct02ea.FieldBlock M_Parent { get { return m_parent; } }
}
public partial class FieldBlock : KaitaiStruct
{
public static FieldBlock FromFile(string fileName)
{
return new FieldBlock(new KaitaiStream(fileName));
}
public FieldBlock(KaitaiStream p__io, Struct02ea p__parent = null, Struct02ea p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
_read();
}
private void _read()
{
_sentinel = new Sentinel(m_io, this, m_root);
switch (Sentinel.Id) {
case 243: {
_value = m_io.ReadU1();
break;
}
case 244: {
_value = m_io.ReadU4le();
break;
}
case 245: {
_value = m_io.ReadU4le();
break;
}
case 241: {
_value = m_io.ReadU1();
break;
}
case 240: {
_value = m_io.ReadU1();
break;
}
case 242: {
_value = m_io.ReadU1();
break;
}
default: {
_value = m_io.ReadBytes((Sentinel.EndOffset - M_Root.M_Io.Pos));
break;
}
}
}
private Sentinel _sentinel;
private object _value;
private Struct02ea m_root;
private Struct02ea m_parent;
public Sentinel Sentinel { get { return _sentinel; } }
public object Value { get { return _value; } }
public Struct02ea M_Root { get { return m_root; } }
public Struct02ea M_Parent { get { return m_parent; } }
}
private int _unk00;
private List<FieldBlock> _fields;
private Struct02ea m_root;
private KaitaiStruct m_parent;
public int Unk00 { get { return _unk00; } }
public List<FieldBlock> Fields { get { return _fields; } }
public Struct02ea M_Root { get { return m_root; } }
public KaitaiStruct M_Parent { get { return m_parent; } }
}
}
Affiliate disclaimer: I'm a Kaitai Struct maintainer (see my GitHub profile).
Since I am wrapping each field in a field_block, the generated code stores these values in that type of object. This is incredibly inefficient when half of the generated field_block objects store a single integer. It would also require the consuming code to iterate through a list of each field block in order to get the actual field's value.
I think that rather than trying to describe the entire format with an ultimate Kaitai Struct specification, it's better for you not to let the generated code parse all the fields automatically. Move the parsing control to your application code, where you use the type Struct02ea.FieldBlock that represents the individual field and basically replicate the "repeat until end of stream" loop that the generated code that you posted was doing:
_fields = new List<FieldBlock>();
{
var i = 0;
while (!m_io.IsEof) {
_fields.Add(new FieldBlock(m_io, this, m_root));
i++;
}
}
The advantage of doing so is that you can adjust the loop to fit your needs. To avoid the overhead you describe, you'll probably want to keep the Struct02ea.FieldBlock object in a local variable inside the loop body, pull only the values you care about (save them in your compact, consumer-friendly output structures) and let it leave the scope after the loop iteration ends. This will allow each original FieldBlock object to get garbage-collected once you process it, so the overhead they have will be limited to a single instance and not multiplied by the number of fields in the file.
The most straightforward and seamless way to prevent the Kaitai Struct-generated code parse fields (but otherwise keep everything the same) is to add if: false in the KSY specification, as #webbnh suggested in a GitHub issue:
seq:
- id: unk_00
type: s4
- id: fields
type: field_block
repeat: eos
if: false # add this
The if: false works better than omitting it from seq entirely, because the kaitai-struct-compiler has occasional troubles with unused types (when compiling the KSY spec with unused types, you may get an error "Unable to derive _parent type in ..." due to a compiler bug). But with this if: false trick, you can't run into them because the field_block type is no longer unused.

C++\CLI Windows Forms - creating external class

While making a simple windows forms application with VC++ I have a listview containing some files and their info. What I want to do is only show the filename in the list while storing the full filepath. I created an external class to do this:
class FileInfoContainer {
vector<string> filenames;
vector<int> uncompsize;
vector<int> compsize;
int getFileSize(string filepath);
public:
FileInfoContainer() {};
void AddItem(string filepath);
void AddItem(string filepath, int uncompsize, int compsize);
void AddItem(string filepath, int uncompsize);
string getItemFilename(int index);
int getItemCompsize(int index);
int getItemUncompsize(int index);
};
Now back to the automatic code generated by the VS template for this application, I tried to declare an instance of this class so that I can have the file information available anywhere within:
public ref class MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
}
protected:
~MainForm()
{
if (components)
{
delete components;
}
}
System::Windows::Forms::ListViewItem^ files; //my addition
FileInfoContainer compfiles; //my addition
private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
private: System::Windows::Forms::SaveFileDialog^ saveFileDialog1;
...........
}
This is generated code where I've only added those 2 lines, of which the first works normally while the second gives a member of managed class can't be non-managed class type error.
What can I do to go around this issue?

java-error in null pointer exception

PLEASE HELP ME TO FIND THIS ANSWER
NULL POINTER EXCEPTION ERROR HELP ME
on these two lines
25,83
null pointer exception please correct my code
i created a object l with reference to class linkk
to access methods in the class
but while accessing the methods in the class with different conditions
it is showing null pointer exception
i didnt finish the code due to this error im struck at this point i cant move
further
this is my post
in overflow
i have read answers in overflow but this is the
first time fr me to post a question in overflow ,
today i created a ac nd posting this question please help me frnds
import java . util.Scanner;
class node
{
int i,q;
node next;
node prev;
}
class link{
public static void main(String args[])
{
linkk l = new linkk();
l.op();
int user=0;
while(user!=10)
{Scanner a=new Scanner(System.in);
if(user==1)
{
System.out.println("\nenter data\n");
l.create(a.nextInt());
}System.out.println("\n1.create link\n2.insert beginning\n3.insert middle\n4.insert end\n5.delete data\n6.reverse");
user=a.nextInt();
}
if(user==2)
l.insertbeg();
if(user==3)
l.insertmid();
if(user==4)
l.insertend();
if(user==5)
l.del();
if(user==6)
l.reverse();
if(user==7)
l.display();
}
}
class linkk
{
node temp4;
int ch,add,cnt=0,t=0,b;
node p= new node();
node q;
node last;
node first=null;
public boolean isEmpty()
{
return first == null;
}
public void insertbeg()
{
}
public void insertmid()
{
}
public void insertend()
{
}
public void del()
{
}
public void reverse()
{
}
public void display()
{
}
public void create(int val)
{
first.i=val;
first.next=null;
cnt++;
}
public void ob()
{
}
public void op()
{
}
}
Your defined first as null as node first=null; and you are trying to access i with first object using first.i=val; by calling l.create(a.nextInt());.
You should initialize first as below:
node first = new node();//and then access i of it and so on.

How can I dump the normal properties on an IEnumerable in Linqpad

If I have an object that among other things is an IEnumerable and I dump this object I get the enumerated values.
Is there a way to get Linqpad to list the other properties:
Se example below:
Can I get Dump to include Hello and digits properties?
void Main()
{
var t = new test();
var d = new Dictionary<string,string> {{"Hello","World"},{"Good by","Sky"}};
t.Dump();
d.Dump();
}
// Define other methods and classes here
public class test : IEnumerable
{
public string Hello { get { return "World"; }}
public List<int> digits { get { return new List<int> {0,1,2,3,4,5,6,7,8,9}; }}
public IEnumerator GetEnumerator() { yield return "Hej"; }
}
You could write a DumpPayload extension method as follows:
void Main()
{
var t = new test();
t.DumpPayload();
}
public static class Extensions
{
public static void DumpPayload (this IEnumerable o)
{
if (o == null)
{
o.Dump();
return;
}
var d = new Dictionary<string,object>();
foreach (var member in o.GetType().GetProperties())
try
{
d[member.Name] = member.GetValue (o, null);
}
catch (Exception ex)
{
d[member.Name] = ex;
}
d["AsEnumerable"] = o;
d.Dump();
}
}
If you put this extension method into "My Extensions", it will be available to all queries.
Another solution is to implement ICustomMemberProvider in the test class:
public class test : IEnumerable, ICustomMemberProvider
{
public string Hello { get { return "World"; }}
public List<int> digits { get { return new List<int> {0,1,2,3,4,5,6,7,8,9}; }}
public IEnumerator GetEnumerator() { yield return "Hej"; }
IEnumerable<string> ICustomMemberProvider.GetNames()
{
return "Hello Enumerator".Split();
}
IEnumerable<Type> ICustomMemberProvider.GetTypes ()
{
return new [] { typeof (string), typeof (IEnumerable) };
}
IEnumerable<object> ICustomMemberProvider.GetValues ()
{
return new object [] { Hello, this.OfType<Object>() };
}
}
Note that if the test class is defined in another assembly, you don't need to reference LINQPad in order to implement ICustomMemberProvider. You can just paste in the following definition into your project and LINQPad will pick it up:
namespace LINQPad
{
public interface ICustomMemberProvider
{
IEnumerable<string> GetNames ();
IEnumerable<Type> GetTypes ();
IEnumerable<object> GetValues ();
}
}
As far as I can tell if the object you're trying to dump implements IEnumerable then LINQPad always wants to dump it as an IEnumerable list. Getting rid of the interface correctly shows the Hello and digits properties in the dumped info.
Going from this link it appears you can write your own dump which accomplishes something like LINQPad by enumerating the collection and all it's properties then outputting the whole thing as an XHTML string. I haven't tried this.
Use a Serializer?
Json.NET will do all of this for you in a json format.
Newtonsoft.Json.JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented)
if you don't want json, then pick a serializer you do want, or you'll just have to do what a serializer would do, use reflection to iterate the properties on the object.