Results 1 to 4 of 4

Thread: String Stuff! Plz Help!!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Exclamation

    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.

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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;
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    New Member
    Join Date
    Mar 2001
    Posts
    1

    Thumbs up

    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Smile Thanks!

    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width