Basically my question is regarding the function in red, rest are to provide a ba
ID: 3626479 • Letter: B
Question
Basically my question is regarding the function in red, rest are to provide a background to you ( they are from same program) , Can u achieve what is done in the red fuction by using purely static arrays(below is not a static one!). And how does itoa convert say 1032 to a string , does it convert into "1032" , in inverted commas, THE INTEGER CHANGES INTO WHAT FORM? Because i have used strcmp (to compare the modified integer AND true integer read from the file )later on and it works, how does an integer work as one of the arguments of strcmp?? ! And by the way why do we even need to convert the Password passed to the function to a string anyway, cant we read the integer(password) from a
file and compare it to the password passed to the function , Comparing int to int, it should work eh??
**************************************************************************
string IntToString(int intValue) {
char *myBuff;
string strRetVal;
// Create a new char array
myBuff = new char[100];
// 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);
}
************************
void making_profile(int profile_counter,int password, string dest,int row,int column)
{ofstream outfile;
string str=IntToString(profile_counter)+".txt";
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;
char line[20];
string stri=IntToString(profile_number)+".txt";
infile.open(stri.c_str());
string str=IntToString(password);
char choise;
if(!infile)
cout<<"profile not found"<<endl;
else{
infile>>line;
if(strcmp(line,str.c_str())==0){
cout<<"Do you want to print your profile information"<<endl;
cin>>choise;
if(choise =='y' || choise =='Y'){
while (!infile.eof()){
infile>>line;
cout<<line<<" ";
}}}
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);
char line[20];
if(!infile)
cout<<"profile nou found"<<endl;
else{
infile>>line;
if(strcmp(line,str.c_str())==0)
{
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
look i will give u another tow ways to cast from string to int but i hope u will rate life saver this time coz i think this is a real life saver solution (1) #include #include using namespace std; void IntToString(double i, string & s) { s = ""; if (i == 0) { s = "0"; return; } if (i < 0) { s += '-'; i = -i; } int count = log10(i); while (count >= 0) { s += ('0' + i/pow(10.0, count)); i -= static_cast(i/pow(10.0,count)) * static_cast(pow(10.0,count)); count--; } } int main() { int i = -12; string s = ""; IntToString(i,s); coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.