CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: New Guy Help

  1. #1
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Question New Guy Help

    # include <iostream>
    using namespace std;

    int main ()

    {
    char first[16];
    char last[16];
    char prefix[2];
    char married[2];

    cout << "What is your first and last name? \n";
    cin >> first >> last;
    cout << "Are you a man or a woman?" << endl << "Please answer with a 'm' for man or a 'w' for woman.";
    cin >> prefix;

    {
    if (prefix = 'w') //line 18
    cout << "Are you married?" << endl << "Please answer with a 'y' for yes or a 'n' for no.";
    cin >> married;
    if (married = 'y') //line 21
    cout << "Hello Mrs. " << last << ". \n";
    else if (prefix = 'n') //line 23
    cout << "Hello Ms. " << last << ". \n";
    }

    if (prefix = 'm') //line 27
    cout << "Hello Mr. " << last << ". \n";
    return 0;
    }
    ____________________________________________________________________________
    Above is a project im working on to get used to C++ (just started programming yesterday) and when i try to build it i get 4 errors saying "incompatible types of assignment of 'char' to 'char[2]' " in lines 18, 21, 23 and 27. Any help would be appreciated, thanks.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: New Guy Help

    You defined prefix as a char array. My guess is that you want to check the first character. So if (prefix[0] == 'w') ...

  3. #3
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: New Guy Help

    kk, thx, will try and let you know

  4. #4
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: New Guy Help

    huh, it worked but now is giving me warnings on the same lines saying "suggest parentheses around assignment used as truth value" and , if its not 2 much to ask, what exactly did assigning 0 to char in the if statements do?

  5. #5
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: New Guy Help

    nvrmd on the warnings, fixed them

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: New Guy Help

    It sounds like you're still doing assignment rather than comparison as ninja suggested.

  7. #7
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: New Guy Help

    Quote Originally Posted by Lindley View Post
    It sounds like you're still doing assignment rather than comparison as ninja suggested.

    ya, i figured that 1 out, but i still dont get what assigning 0 to char did to make this work

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: New Guy Help

    Quote Originally Posted by iiSoMeGuY 7x
    but i still dont get what assigning 0 to char did to make this work
    prefix[0] does not assign 0. It accesses the element of prefix at index 0.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: New Guy Help

    I don't see you assigning 0 anywhere.....please clarify your question.

  10. #10
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: New Guy Help

    Quote Originally Posted by laserlight View Post
    prefix[0] does not assign 0. It accesses the element of prefix at index 0.
    ok, thanks

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: New Guy Help

    Of course you could just define prefix and married as a single char instead of an array.

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

Featured