Hi all,
I need help with an old C program, making it work in C++, and changing it a little bit. For now, can someone help me change this line of code to the new function in C++?
Thanks and God Bless.
Jonn.
cp = (char *) malloc(strlen(tbuff));
Hi all,
I need help with an old C program, making it work in C++, and changing it a little bit. For now, can someone help me change this line of code to the new function in C++?
Thanks and God Bless.
Jonn.
cp = (char *) malloc(strlen(tbuff));
Please don't tell me that's followed by strcpy( cp, tbuff );
If it is, it's a bug.
There is no space for the \0 at the end of the string.
1. In C++, it would be cp = new char[ strlen(tbuff) + 1];
2. If you're REALLY converting it to C++ (and not just making it compile with C++), then you should be using std::string
for all these strings rather than messing about with (and getting it wrong) your own memory allocation.
I don't follow the
std::string
conversion you are talking about. The code is to convert any entered dollar amount in numbers to letter form. I will post the rest of the code, I tried to work on it, and it only works to 99.99, anything higher it crashes.
#include <stdio.h>
#include <stdlib.h>
static char *LowAmounts[]= {
" ","One","Two","Three","Four","Five",
"Six","Seven","Eight","Nine","Ten",
"Eleven","Twelve","Thirteen","Fourteen","Fifteen",
"Sixteen","Seventeen","Eighteen","Nineteen"
};
static char *TenAmounts[]={
" "," ", "Twenty","Thirty","Forty",
"Fifty","Sixty","Seventy", "Eighty","Ninety"
};
struct account {
float amount;
char *value;
int day;
int month;
struct account *InternalPtr;
};
static struct account Payable,* AccountPtr;
struct account InitializePayable(void);
char *ConvertToString(float);
void main()
{
printf("%s %s\n",*(TenAmounts+5),*(LowAmounts + 2));
Payable = InitializePayable();
printf("%8.2f\n",Payable.amount);
return;
}
struct account InitializePayable()
{
struct account TempAccount;
char ibuff[12];
int i;
for (i = 0; i < 12; i++)
*(ibuff + i) = 0;
i = 0;
while ((*(ibuff + i) = getchar()) != '\n')
i++;
TempAccount.amount = (float) atof(ibuff);
printf("%6.2f - first shot \n",TempAccount.amount);
/* TempAccount.amount /= 100; */
TempAccount.value = ConvertToString(TempAccount.amount);
TempAccount.day = 17;
TempAccount.month= 11;
return TempAccount;
}
char *ConvertToString(float in_val)
{
char *cp,tbuff[68]="\0";
int tval,uval;
uval = (int) (in_val);
if (uval >= 20)
{
tval = (uval / 10);
uval-= (uval / 10) * 10;
}
else
tval = 0;
strcat(tbuff,*(TenAmounts+tval));
strcat(tbuff,*(LowAmounts+uval));
strcat(tbuff," AND ");
cp = (char *) malloc(strlen(tbuff));
strcpy(cp,tbuff);
printf("%d %d %s\n",tval, uval, cp);
return cp;
}
here is how to enter the characters of a number as a string and convert to float. If this code is too much for you then you should probably shelve your project for awhile and concentrate on learning c++.
#include <string>
#include <strstream>
#include <iostream>
using namespace std;
int main()
{
string input;
float num;
strstream str;
cout << "Enter a number...";
cin >> input;
str << input;
str >> num;
cout << "num = " << num << "\n";
return 0;
}
Hi Dragon,
I don't think you read or understood my question correctly. It's the exact opposite of what you code does. And I have shelved the project for the last month or so. This is my last project in C++. The issue is the professor does not teach half of the stuff he asssigns you, and thus I come to the only place that can aide me. I am sorry if I made it seem that I was lazy and wanted you to do my work for me, I just needed some help to go the right way. I have worked on the original code and it works to 99.99. Which was the original requirement, but he changed it to 1000. And that is what I am working on now.
That's what's nice about stringstream -- make conversion very simple in either direction.
if you want to stick with char*, then use sprintf() to make the conversion. But if you do this you are not converting the C program to C++.
char *ConvertToString(float in_val)
{
char *cp = new char[80];
sprintf(cp,"%f",in_val);
return cp;
}
Here is the c++ equalivant
struct account {
float amount;
std::string value;
int day;
int month;
struct account *InternalPtr;
};
string ConvertToString(float in_val)
{
string cp;
strstream str;
str << in_val;
str >> cp;
return cp;
}
Thank you very much. I will try this tomorrow and thank you again.
TIS' the season to be grateful, lol.
Merry Christmas all,
The code worked and I handed in my project. I thank all for the assistance, and again, I hope I can be of help in your time of need as you were to me. I will be taking the second class in C++ next semester, so look out for more posts. lol.
Thanks again, and God Bless,
Jonathan.
Oh, I forgot, we can close this thread.
Jonathan.
Oh, I forgot, we can close this thread.
Usually the best thing for the original poster to do when problem is fixed is to click the "Mark as solved" link at the top of the page.
cfront
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.