Write here if you have problems with your C++ source code
Moderator: Board moderators
by Kire Sopov » Tue Dec 12, 2006 3:16 am
Why am I getting a compile error?
- Code: Select all
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
void ConvertNum(std::string &str, int nFrom, int nTo)
{
static const char szDigits[] = "0123456789ABCDEF";
int nValue10(0), nProduct(1);
// Convert from base nFrom to base 10:
for (std::string::const_reverse_iterator it = str.rbegin(); it != str.rend(); ++it)
{
int nTmp = ((*it >= '0') && (*it <= '9')) ? int(*it - '0') : 10 + (*it - 'A');
nValue10 += nTmp * nProduct;
nProduct *= nFrom;
}
// Convert from base 10 to base nTo:
str.clear();
do
{
str.push_back(szDigits[nValue10 % nTo]);
nValue10 /= nTo;
}
while ((nValue10 > 0) && (str.length() < 8));
if (str.length() > 7)
str = "ERROR";
else
std::reverse(str.begin(), str.end());
}
int main()
{
std::string strNum;
int a, b;
while (NULL != (std::cin >> strNum >> a >> b))
{
ConvertNum(strNum, a, b);
std::cout << std::setw(7) << strNum << std::endl;
}
}
-
Kire Sopov
- New poster
-
- Posts: 7
- Joined: Wed Sep 15, 2004 2:01 am
by lnr » Mon Oct 06, 2008 4:41 pm
I ran your code and got accepted.
-

lnr
- Experienced poster
-
- Posts: 134
- Joined: Sat Jun 30, 2007 2:52 pm
- Location: (DU,CSE)Dhaka,Bangladesh
by Angeh » Thu Feb 04, 2010 11:31 pm
use strcasecmp();
>>>>>>>>> A2
Beliefs are not facts, believe what you need to believe;)
-
Angeh
- Experienced poster
-
- Posts: 108
- Joined: Sat Aug 08, 2009 2:53 pm
Return to C++
Who is online
Users browsing this forum: No registered users and 1 guest