C++ people.. I need your help.

Discussion in 'Off Topic Area' started by Tika, Aug 5, 2004.

  1. Tika

    Tika New Member

    I absolutely hate c++. When I took this job I made them promise Id never need to use it.

    THEY LIED... and currently Im supposed to be learning as much as I can about pointers (the bane of my existance) and strings (not the string class mind you, but cstrings/character arrays).

    Its been a long time, and I dont remember how to do anything. I have no problem with pointers to simple variables like ints and doubles.... but im lost and the errors im getting are not helping me....

    any help would be greatly appreciated. Ive tried quite a bit of fenagling...but im getting no where..

    CODE: (problem area in bold)

    // stringNpointers.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <cstring>
    #include <iostream>
    using namespace std;

    char* combineStrings(char* cstrOne[], char* cstrTwo[]);
    int* theBigger(int* ptrInt1, int* ptrInt2);


    int main(int argc, char* argv[])
    {

    int regInt1 =5;
    int regInt2 = 10;

    int* ptrInt1 = &regInt1;
    int* ptrInt2 = &regInt2;
    int* ptrReturn = NULL;

    ptrReturn = theBigger(ptrInt1,ptrInt2);

    cout << *ptrReturn << endl;

    //char cString1[] = "I Hope ";
    //char cString2[] = "This Works";

    //char* ptrFuncResult[] = combineStrings(&cString1, &cString2);

    //printf(*ptrFuncResult);

    return 0;


    }

    int* theBigger(int* ptrInt1,int* ptrInt2)
    {

    if (*ptrInt1 > *ptrInt2)
    return ptrInt1;
    else
    return ptrInt2;

    }


    char* combineString(char* cstrOne[], char* cstrTwo[])
    {

    char tmpString[100];
    strcpy(tmpString, const cstrOne);
    strcat(tmpString, cstrTwo);
    return tmpString;

    }



    THE ERRORS:

    --------------------Configuration: stringNpointers - Win32 Debug--------------------
    Compiling...
    stringNpointers.cpp
    D:\JT_Local\stringNpointers\stringNpointers.cpp(54) : error C2059: syntax error : 'const'
    D:\JT_Local\stringNpointers\stringNpointers.cpp(55) : error C2664: 'strcat' : cannot convert parameter 2 from 'char *[]' to 'const char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    D:\JT_Local\stringNpointers\stringNpointers.cpp(56) : warning C4172: returning address of local variable or temporary
    Error executing cl.exe.

    stringNpointers.exe - 2 error(s), 1 warning(s)

    I really hate c++...pointers SUCK.
     
    Last edited: Aug 5, 2004
  2. JohnnyX

    JohnnyX Map Addict

    Does it matter that there isn't a space between the = and the 5 in "int regInt1 =5;"

    Other than that, I don't know. :(

    Cheers. :)
     
  3. Kwajman

    Kwajman Penguin in paradise....

    Tika you lost me when you said C + +

    We've got some killer computer guys here. Check w/Yoda and WW.
     
  4. Tika

    Tika New Member

    well gee thanks guys ;) hehehe.... Im hoping that one of the gurus see this. There HAS to be a good c++ person on here. Whoever helps gets a nice big special tackle hehe....
     
  5. HearWa

    HearWa Ow, that hurt...

    YES! I finally have a chance to flagrantly display my random nerdiness on MAP! You hating C++ almost made me click the back button, but the tackle sounds like a good incentive. I've numbered the changes, and listed what they're for below. There may be some small changes, but that's just because I'm a masochist with my conventions. :)

    // stringNpointers.cpp : Defines the entry point for the console application.
    //

    // #include "stdafx.h" // you obviously need this header....
    #include <cstring>
    #include <iostream>
    using namespace std;

    char* combineStrings(char* cstrOne[], char* cstrTwo[]);
    int* theBigger(int* ptrInt1, int* ptrInt2);


    int main(int argc, char* argv[])
    {

    int regInt1 =5;
    int regInt2 = 10;

    int* ptrInt1 = &regInt1;
    int* ptrInt2 = &regInt2;
    int* ptrReturn = 0;

    ptrReturn = theBigger(ptrInt1,ptrInt2);

    cout << *ptrReturn << endl;

    //char cString1[] = "I Hope ";
    //char cString2[] = "This Works";

    //char* ptrFuncResult[] = combineStrings(&cString1, &cString2);

    //printf(*ptrFuncResult);

    return 0;


    }

    int* theBigger(int* ptrInt1,int* ptrInt2) // I'm sure I saw a function for this once...
    {

    if (*ptrInt1 > *ptrInt2)
    return ptrInt1;
    else
    return ptrInt2;

    }


    char* combineString(char** cstrOne, char** cstrTwo)
    {

    char *tmpString = new char[100]; // 1.
    strcpy(tmpString, (const char*)cstrOne); // 2.
    strcat(tmpString, (const char*)cstrTwo); // 3.
    return tmpString;

    }

    1. You were storing the string on the stack (somebody's been working too much with Java!), which may or may not be there as you intended once the function returns (stack pointer goes up). I put it on the heap instead. BE SURE TO DELETE THE CHARACTER ARRAY OFF THE HEAP AS WELL, OR YOU'LL GET A MEMORY LEAK. (delete [] (character array identifier).

    2. I believe you were trying to type cast the char pointer datatype? The declerator doesn't do this, sorry. Try perl or something, LOL! :D (inside joke)

    3. Basically same change. I type casted it to a const char pointer as well. Note the identifier is a pointer to a const pointer of the first array, but will decay into a pointer to every character in the array during the type cast. This may or may not be what you intended, but that information was not provided so I made an assumption.

    Please note I just got back from work, so I'm very tired. I didn't check it for errors, but it did compile and work. If you need any more help just PM me. :)
     
  6. YODA

    YODA The Woofing Admin Supporter

    Yeah - what he said ^^^^ :eek:
     
  7. HearWa

    HearWa Ow, that hurt...

    I may not have 13,000 posts, but I do know a bit of C++! :D
     
    Last edited: Aug 5, 2004
  8. WhiteWizard

    WhiteWizard Arctic Assasain

    You sick sick man :D
     
  9. HearWa

    HearWa Ow, that hurt...

    Actually, now that I looked at the code... why do you need two two-dimensional arrays to be passed into combineString()? It looks like you only need an array pointer to be passed into the function.

    Also, why not dynamically allocate the size of tmpString in combineString()? You could use strlen(char*) (a function in the standard C library), as literal strings are null-terminated by your compiler.

    Most of the functions you are defining have all ready been created, and are in the standard C library. Check it out. They're time-proven functions that have been through alot fo testing and debugging.
     
    Last edited: Aug 5, 2004
  10. Tika

    Tika New Member

    EDIT: just so you know, there is no point to this code. Im just trying to familiarize myself with pointers and strings...and passing them through functions... stuff like that.


    Thank you very much...right at the moment that made no sense to me whatsoever...but I will look again tomorrow morning since my brain is now fuzzy and hurting from trying to relocate my memory from c++ classes. I think I blocked it all out.

    Sorry bout the hating c++ comment...hehe...bad memories. Pointers are the reason I said Id never become a programmer. But then I got hired to be one and had to figure it out. Unfortunately we have the choice of hiring someone to write what we need in c++ and wait three months for it, or learn ourselves in a month and do it on our own.........C# is just so much easier if you ask me.
     
  11. WhiteWizard

    WhiteWizard Arctic Assasain

    its not so bad once you start writing it again i had to write a multi threaded network simulator in C a couple of years ago and could never have dreamt about doing it at the start of that year so just keep going despite me also hating it the chances are you'll become a better programmer at the end of it
     
  12. HearWa

    HearWa Ow, that hurt...

    In a month!? It took me a year to get the basics down! And I'm still learning about C++, a year later. I don't doubt I'll always be learning something new. That's what's great about C++. That, and POINTERS! *tee he*

    Good luck to you, I say!
     
  13. Tika

    Tika New Member

    Well not a month to master the language. I have a decent backround in programming... It took me 3 weeks to get a good enough grasp on vb6 to write programs, then once we realized that woudlnt do it, I learned C#.NET in about a month. C++ is deffinitely more difficult, but we dont even need to write programs. Just get a good enough grasp of it to write a wrapper. So we can program in c# to a c++ back end.
     
  14. WhiteWizard

    WhiteWizard Arctic Assasain

    C# and VB learning times are significantly lower than C/C++ so good luck
     
  15. hongkongfuey

    hongkongfuey Kung Fu Geek

    C++ and pointers are God. You really learn how the computer does things in the background. Assembler is even better for the same reasons. Not like these newfangled c#, vb.net, java languages. Automatic memory / stack management. Pah. RAD development. Pah. Visual tools. Pah.

    I remember programming 6502 assembler on a BBC micro. Hmmm takes me back, I can tell you. Don't make em like that anymore. Ran programs on my 2Mhz PC with 32Kb Ram that ran as fast as stuff you young un's write on your 2Ghz PC with 512Mb RAM. Or something like that.

    Nurse, where's my bed bath? :eek:
     
  16. WhiteWizard

    WhiteWizard Arctic Assasain

    Someone cart that man off he needs a time machine :D
     
  17. Tika

    Tika New Member

    oh I almost forgot.... *TACKLES HEARWA*.... thanks man...

    WW: Thanks... I know its not going to be easy, luckily my brother (the other person on our develepment staff hehehe) is a disgustingly fast learner, and has aleady blown through three books on the subject. He also spends about 10 hours a day on it (something Im not willing to do )......plus, like Isaid... we dont really have to write anything remotely complicated.

    HKF: whatever you say man :)... Im just glad that once we do this one project, we will never *crosses fingers* have to do c++ stuff again.
     
  18. NRees

    NRees Taekwon-Do II Degree

    Pah, I'm no good at any language except VB - mainly cos i cant be bothered to learn anything else at the mo ^_^. But I did sorta understand some of that code.

    Tika, what do you work as??

    No fair, you lot got the jobs I want :(

    *pouts* I want a programming related job dammit :p
     
  19. HearWa

    HearWa Ow, that hurt...

    Heh, I'm still in high school unfortunately. My job, you ask? Throwing boxes of peas/pies/french fries/potatoes on pallettes!

    I'm all self-taught. C++ is one of my favorite subjects. I'm starting to teach myself discrete mathematics, also ("Discrete Mathematics with Applications" is the tutorial I'm learning from). It really helps out my coding.
     
  20. bcullen

    bcullen They are all perfect.

    Woo hoo! Another old school geek.

    Heh, young folks. If you can't write in assembly, how are you going to fix the bugs you've created in C++? ;)
     

Share This Page