PDA

Click to See Complete Forum and Search --> : String Stuff! Plz Help!!


Smithy006
Apr 1st, 2001, 06:45 PM
Hi All,

I'm currently working on a really basic program but i Keep getting these stupid little problems. I'm having trouble with giving a char a value the compiler keeps giving me the error.

error C2440: '=' : cannot convert from 'char [19]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Here's some sample source of what I'm trying to do...

#include <iostream.h>

//declare variables

char namea [30];
char nameb [30];
char namec [30];
char named [30];

char statusa;
char statusb;
char statusc;
char statusd;

char ainfoa; // These are the ones giving me the problem as showed below...
char ainfob;
char ainfoc;
char ainfod;

double payhour;
double timework;

double totala;
double totalb;
double totalc;
double totald;


int main()
{
cout << "$$$$$$$$$$ Welcome to the Pay Calculation Program Ver 2.0 $$$$$$$$$$\n" << endl;
cout << " Program By Ben Smith\n" << endl;

// first person
cout << "Enter the first persons name: ";
cin >> namea;
cout << "Enter the first persons amount per hour received: ";
cin >> payhour;
cout << "Enter the number of hours the person works: ";
cin >> timework;
cout << "Enter their pay status..." << endl;
cout << "[Enter S for Salaried, P for Permanant or T for Temporary]: ";
cin >> statusa;

// calculate and save in total a. Add additional Info
totala = payhour * timework;
if ((totala >= 25) && (statusa == 's' || statusa == 'S'))
ainfoa = "Standard- Salaried";
else
if (totala >= 25)
ainfoa = "Plusrate";

Any Help would be Greatly appreciated!!!!

Thanks.

HarryW
Apr 1st, 2001, 07:21 PM
You need to allocate a proper buffer to hold a string, a single character (1 byte) isn't enough to hold the whole string. Also, you can't just allocate a string to an array of characters, since a string literal is of type const char *. You will get a type error if you try to, which is a good thing because it wouldn't make sense :)

You should modify your code so it looks more like:

char ainfoa[50];


and to assingn a value to the variable at run-time (not at the same time as declaring it), you should use the strcpy() function (I think it's either strcpy() or strcopy(), I don't remember right now) to copy the value across.

If you are using C++ (not C) you could use the STL string class which allows you to work with strings just like in VB. You'll need to add this line:

#include <cstring>

then declare your strings like this:

string ainfoa;

Adventure3
Apr 1st, 2001, 07:38 PM
Hey,

you're trying to assign a char a string.

ex. ainfoa = "Standard- Salaried"; //this is wrong

You have to declare ainfoa as char ainfoa[25], just like you did with namea, nameb, etc.

Then you can use strcpy() function to set the ainfoa string with "Standard-salaried" as so:

ex. strcpy(ainfoa, "Standard-salaried");


Toma

Smithy006
Apr 1st, 2001, 08:01 PM
Hi Ad and Harry,

Thanks for all of your help. That was exactly what I was after.:)

I just gota remeber strcopy rather than doing it more in a VB fashion (I'm a lot more experienced in VB).