Click here to Skip to main content
15,311,561 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
it keeps giving me a garbage value

I know something is wrong but I do not know what is it

the question want to use the array of size 3 and both function with return type (void)
and pointer (*) and int as a parameter

this is my code:

What I have tried:

#include<iostream>
#include<string>
using namespace std;

struct Bankinfo{
	string name,location,manager_name;
	int bank_id,emp_numbers;
};


void getlistoftBankinfo(Bankinfo *n,int size){

	for(int i=0;i<size;i++){
		cout<<"enter the name"<<endl;
		cin>>n[i].name;
		cout<<"-----------"<<endl;
		cout<<"enter the location"<<endl;
		cin>>n[i].location;
		cout<<"-----------"<<endl;
		cout<<"enter the manager_name"<<endl;
		cin>>n[i].manager_name;
		cout<<"-----------"<<endl;
		cout<<"enter the bank id"<<endl;
		cin>>n[i].bank_id;
		cout<<"-----------"<<endl;
		cout<<"enter the employees number "<<endl;
		cin>>n[i].emp_numbers;
	}
}

void displaylistoftBankinfo(Bankinfo *n,int size){

	for(int i=0;i<3;i++){

		cout<<n[i].name;
		cout<<n[i].location;
		cout<<n[i].manager_name;
		cout<<n[i].bank_id;
		cout<<n[i].emp_numbers;


}
}



int main()
{

Bankinfo n[3];



getlistoftBankinfo(n,3);

displaylistoftBankinfo(n,3);
return 0;
}
Posted
Updated 6-Nov-21 7:16am
Comments
Richard Deeming 9-Nov-21 6:34am
   
REPOST
You have already posted this:
Why it keeps giving me a garbage value[^]

When you plan to read a List of records you should ask how many entrys are needet.

C++
int n;
cout << "Number of records: ";
cin >> n;

Bankinfo *b = new Bankinfo[n];
if (b == nullptr) {
    cout << "Memory Problem!\n";
    exit(1);
}
   
You are printing all the details without any spaces or end of line characters. Change to something like:
C++
void displaylistoftBankinfo(Bankinfo *n,int size){
    for(int i=0;i< size; i++){  // the loop count here should be size not hardcoded 3
        cout<<n[i].name << ", ";
        cout<<n[i].location << ", ";
        cout<<n[i].manager_name << ", ";
        cout<<n[i].bank_id << ", ";
        cout<<n[i].emp_numbers << endl << endl;
    }
}
   
v2
Comments
merano99 6-Nov-21 12:56pm
   
It seems that the hard coded 3 is the real problem.
Another problem arises when someone enters two words with spaces. It would make sense to read in the entire line with getline.
Richard MacCutchan 6-Nov-21 13:05pm
   
No, that is a minor issue.
Member 15420232 6-Nov-21 13:09pm
   
it worked!

but i have 3 questions
1- why should use size instead of 3?
2-why I did not use the & operator in main function when I pass the array?
3- why I did not assign n=getlistoftBankinfo(n,3)?
merano99 6-Nov-21 13:20pm
   
If you accept a variable length list, you cannot hardcode 3.
See my answer on a variable number of elements.
Richard MacCutchan 6-Nov-21 13:26pm
   
1. You should use the size value because this method does not know how many Bankinfo structures n points to. The size value is the only certain way of processing the correct number.
2. Because n in the main method is an array, and the name of the array is a pointer to the first entry.
3. Because the statement Bankinfo n[3]; tells the compiler to reserve the space for an array of Bankinfo structures at that point. So when the program runs, the variable n already points to the array.
Member 15420232 6-Nov-21 13:35pm
   
thanks for helping me I appreciate it
Richard MacCutchan 6-Nov-21 13:38pm
   
You are welcome.
Member 15420232 6-Nov-21 13:26pm
   
there is error happens after the first time in the loop
the output like

enter the name
r
-----------
enter the location
anything
-----------
enter the manager_name
anything
-----------
enter the bank id
22
-----------
enter the employees number
3
enter the name
-----------(it does not take the name from the user)
enter the location
anything
-----------


so I used cin instead of getline
Richard MacCutchan 6-Nov-21 13:31pm
   
I just tested it (with my corrections) and it works fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900