THESE ARE ALL FUNCTIONS (NOT IN VOID MAIN!) CAN SOMEONE EXPLAIN MY FEW QUESTIONS
ID: 3626409 • Letter: T
Question
THESE ARE ALL FUNCTIONS (NOT IN VOID MAIN!) CAN SOMEONE EXPLAIN MY FEW QUESTIONS BELOW? specially the dynamic array part? ALL Functions are part of the same program, so in a sense are connected!
WHY WAS A DYNAMIC ARRAY USED IN THE PROGRAM AFTER ALL??
**************************************************************************
string IntToString(int intValue) {
char *myBuff;
string strRetVal;
// Create a new char array
myBuff = new char[100]; //IS THIS HOW YOU DECLARE DYNAMIC ARRAYS GENERALLY?,what is "new char"?
// Set it to empty
memset(myBuff,'',100);
// Convert to string
itoa(intValue,myBuff,10); // Itoa, how does it work HERE?
// Copy the buffer into the string object
strRetVal = myBuff;
// Delete the buffer
delete[] myBuff;
return(strRetVal); //strRetVal is a "what" at this stage :) ?
}
************************
void making_profile(int profile_counter,int password, string dest,int row,int column)
{ifstream infile; //WHAT IS NEED OF THIS FILE HERE?
ofstream outfile;
string str=IntToString(profile_counter)+".txt"; //WHAT DOES THIS MEAN,WHAT DOES IT DO?
outfile.open(str.c_str());
outfile<<password<<endl;
outfile<<"you booked a seat to "<<dest<<endl;
outfile<<"your location is in row "<<row+1<<" and column "<<column+1<<endl;
outfile<<endl<<endl;
outfile.close();
*********************************
void open_profile(int profile_number,int password)
{ifstream infile;
string line;
string stri=IntToString(profile_number)+".txt";
infile.open(stri.c_str()); //WHY IS CASTING DONE HERE,ANY DETAILS?
string str=IntToString(password); //WHY R WE CONVERTING PASSWORD(INTEGER) TO STRING?
char choise;
if(!infile)
cout<<"profile not found"<<endl;
else
{
getline(infile,line);
if(line == str)
{
cout<<"Do you want to print your profile information"<<endl;
cin>>choise;
if(choise =='y' || choise =='Y')
{
while (!infile.eof())
{
getline(infile,line);
cout<<line<<endl;
}
}
}
else
cout<<"wrong credinatals"<<endl;
}
infile.close();
}
****************************
char add_profile(int profile_number,int password,string dest,int row,int column)
{
fstream infile;
string stri=IntToString(profile_number)+".txt";
infile.open(stri.c_str());
string str=IntToString(password);
string line;
if(!infile)
cout<<"profile nou found"<<endl;
else
{
getline(infile,line);
if(line==str)
{
infile.seekp (0, ios::end);
infile<<"you booked a seat to "<<dest<<endl;
infile<<"your location is in row "<<row+1<<" and column "<<column+1<<endl;
return 'y';
}
else
{
cout<<"wrong credinatals"<<endl;
return 'n';
}
}
infile.close();
}
Explanation / Answer
IS THIS TRUE? outfile.open(str.c_str()); out file is the variable of fstream and it is just like ifstrean and ofstream variables but fstream variable can do both input and output and between the braces u see there is the name of the file (str) then c_str this one i.e(c_str) is used becuase u can not open a file with a string variable but with a char string u can and here this function (c_str) cast it to char string
This is essentially true, although you mean "char array" not "char string" because there is no "char string".
myBuff = new char[100]; //IS THIS HOW YOU DECLARE DYNAMIC ARRAYS GENERALLY?,what is "new char"?
No, this is a static array. The key point for an array to be "dynamic" is VARIABLE SIZE. If any array declares a size at initialization, it is NOT a dynamic array. Hope that helps :)
MyBuff = new char[100];
This is how the array is declared. It essentially says that MyBuff is a new array of 101 characters. This is because arrays are 0 indexed, so MyBuff[0] is the first element, going to MyBuff[100], so 101 elements in total.
itoa(intValue,myBuff,10); // Itoa, how does it work HERE?
So here intValue is converted into a string that is subsequently stored in MyBuff.
return(strRetVal); //strRetVal is a "what" at this stage :) ?
strRetVal is a string. In C, a string was a series of characters followed by a terminator '/0'. In C++, a string is a container which holds the same thing but has some extra functions to support the inherent nature of strings. you can read more about it here: http://www.cplusplus.com/reference/string/string/
In the specific case, strRetVal contains the string equivalent of intValue.
ifstream infile; //WHAT IS NEED OF THIS FILE HERE?
In C++, there are three ways to open files:
ifstream in; // input stream
ofstream out; // output stream
fstream inAndOut; // input and output
The declaration basically says that there will be an input file. In the function however, infile is NEVER used, so it is redundant.
infile.open(stri.c_str()); //WHY IS CASTING DONE HERE,ANY DETAILS?
stri is of type 'string', which as I mentioned before is a container. infile can't open if the file name is of type string. So, the function c_str() converts it to a C-style string so that infile can open it.
WHY R WE CONVERTING PROFILE NO. TO A STRING?
string stri=IntToString(profile_number)+".txt"; infile.open(stri.c_str());
Because the file is named by profile number (or I'm assuming based on the code), so the number must first be converted to a string so it can be "817.txt" or whatever the number is, and then c_str() makes it a C-style string so infile can open it.
As far as I can tell, there are no dynamic arrays in this program. Remember, an array is dynamic only if it has a variable size and random access, and there are no variable size arrays here.
Hope it helps!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.