tanx dear mats, in this case i would appreciate a proper and yet sample demosntrating how to use malloc() just for learning purposes, im not gona use it anymore.
and the rest :
ive disabled all of the C string variables so far , and used their equivalent C++ counter parts ,(str::string) .
i even managed to replace the atoi() with the new string stream based Atoi() function i modified :
Code:
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;
int Atoi(string input)
{
int temp;
stringstream ss( input);
ss>>temp;
return temp;
}
it was primarily accepting a char * as an argumnet , but i changed it into string ! i dont know whether this makes an error or not !, so please tell me about this too.
and now, im having a slight big problem using stringstream in converting octal to decimal. the problem is i dont know , how im supposed to make the string stream understand that , it should treat the number a octal not decimal as default!
well conversion from decimal to octal is fine , because it treats the number as decimal at first ( as the number itself is decimal indeed!)
but when i have an octal number, how can i do this ? i mean simply doing :
Code:
#include <cstdlib>
#include <iostream>
#include <sstream>
using namespace std;
int main(int argc, char *argv[])
{
int num = 12;
std::stringstream stream;
stream <<dec<<num;
std::cout << stream.str();
system("PAUSE");
return EXIT_SUCCESS;
}
wont give me a correct answer though!
but this program is just fine with stream<<oct<<num, meaning the conversion from decimal to octal.! so im stuck in it !
and by the way , is not a function for converting octal to binary just like the above ! ?
im asking these, because im revising my whole project ! so i do change the functions , and im trying to use standard stuff at first.
( i cant download boost library, im using Dialup now, so , please help me with these stuff. tanx .
-------------------------
logs:
replaced itoa() with a better equivalent by string stream
replace atoi() with a stringstream based function . (Atoi(). i)
replaced all of C style string variables with their std::string counterparts.
DectoOctal() is removed and instead the new function :
Code:
string DectoOct(int dec)
{
std::stringstream stream;
stream <<std::oct<<dec;
return stream.str();
}
is used.
meanwhile im seeking a better function for Octal to bin
the current function i have for cotal to binary conversion is :
Code:
string OcttoBin(int n)
{
int a[6],i=0,t=0,num;
string str,str1,str2,message;
char buffer[9];
string str_array[9];
const size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]>7)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
if (!str.empty())
str.append("000");
else
str="000";
break;
case 1:
if (!str.empty())
str.append("001");
else
str="001";
break;
case 2:
if (!str.empty())
str.append("010");
else
str="010";
break;
case 3:
if (!str.empty())
str.append("011");
else
str="011";
break;
case 4:
if (!str.empty())
str.append("100");
else
str="100";
break;
case 5:
if (!str.empty())
str.append("101");
else
str="101";
break;
case 6:
if (!str.empty())
str.append("110");
else
str="110";
break;
case 7:
if (!str.empty())
str.append("111");
else
str="111";
break;
}
}
str1="000000";
str2="000";
if (str.length()==3)
{
str1.append(str);
str=str1;
}
if (str.length()==6)
{
str2.append(str);
str=str2;
}
if(t==1)
{
message="no";
// cout<<"Not a Octal number\n";
return s;
}
return str;
}
and the last one is to find a way to get stringstream to work properly as i said above.