Thread: What am I doing wrong here?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    35

    What am I doing wrong here?

    I've just started C++ and I don't fully understand it yet. I made the following application (in Borland) and my teacher called it dangerous coding and said that coding like this might give me problems in the future.
    Code:
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "ten.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
            AnsiString s, t;
            s = this->A->Text;
            t = this->B->Text;
            this->AdivB->Text = s/t;
            this->AmodB->Text = s%t;
    }
    //---------------------------------------------------------------------------






    He told me to use something like:
    Code:
    int number1
    number1 = s.ToInt();

    So I tried this:
    Code:
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "ten.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
            : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
            int A
            A = s.ToInt();
            int B
            B = t.ToInt();
            this->AdivB->Text = s/t;
            this->AmodB->Text = s%t;
    }
    //---------------------------------------------------------------------------


    Shamefully this didn't work. Could someone tell me what I'm doing wrong and explain why?

    Thanks in advance,

    Sam Granger

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Presumably you are working with Borlands GUI and have created a form with two edit boxes called A and B. Your goal appears to be taking the information entered by user in the edit boxes, dividing them and using the modulus operator on them and then displaying the results in two different edit boxes called AdivB and AmodB. However, edit boxes in your version of Borland apparently take objects of the AnsiString class, not ints. Therefore you have to convert AnsiStrings to ints or doubles or whatever, do the math, and then convert the results back into AnsiStrings strings so they can be displayed. The ToInt() AnsiString method apparently converts AnsiStrings to ints. How you plan to convert the results back to AnsiStrings is unclear. You may be able to do it directly via a constructor, or there may be a built in AnsiString method, or you may want to try the non-standard function itoa() that Borland used to put in the non-standard header file called conio.h, or you might want to change the int into a string using sprintf() or a stringstream (this is a commonly discussed problem on the board so you could do a search to find examples) and then convert that string to an AnsiString.

    Step 1: Convert the AnsiStrings to ints
    int s = A->Text.ToInt();
    int t = B->Text.ToInt();

    STep 2: do the math
    int r1 = s/t;
    int r2 = s % t;

    Step 3: convert the results to AnsiString somehow

    Step 4: assign the resultant AnsiStrings to the respective result Text boxes Text attribute
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    If you just started with C++ why are you using all sorts of non-standard modifiers? Why not actually learn C++ first? What's with all the #pragma's? Where did you learn this?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I agree, this isn't C++, but a load of vendor specific stuff on top of C++.
    It won't make all that much sense until you've learnt the core C++ first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    35
    Quote Originally Posted by orbitz
    If you just started with C++ why are you using all sorts of non-standard modifiers? Why not actually learn C++ first? What's with all the #pragma's? Where did you learn this?
    Well, this is how my software engineering course at my college is teaching me to learn C++

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're learning the wrong thing, from a bunch of clueless boneheads.
    You'll find it hard to transport that 'c++' to another OS / Compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You guys having a rough day at the office? Give the guy a break. He's doing the best he can with what he's got. Slamming his instructors may make you feel good, but it won't help him. It's fine to point out the short falls, but at least try to help him overcome them until he has the opportunity to seek an alternative. It sounds like he has no choice in the matter for now.
    You're only born perfect.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    35
    Quote Originally Posted by orbitz
    If you just started with C++ why are you using all sorts of non-standard modifiers? Why not actually learn C++ first? What's with all the #pragma's? Where did you learn this?
    The pragma's were there automatically (application in borland C++ builder 6 enterprise). Should i remove this?

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    IMO, no. You might have gotten a better reception by posting this to the Windows forum rather than the C++ forum, though. In this forum, writing standard code from the console is the holy grail. Writing standard compliant, fully portable code is (almost) impossible when using popular proprietary libraries such as MS's MFC or Borland's VCL, though.

    As you've experienced, though, most people here feel you should learn standard coding practices using the console first, and then move on to GUI programming. However, your instructor/shool has decided to start you out using a GUI from the get go. You will have to learn non-standard approaches first and adjust later if required by employers, etc.. So be it.
    You're only born perfect.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Hell, it's his money and 4 years he's wasting, not mine!
    I think he's learning the wrong thing - what do you think he's learning?

    There's simply no need to be obfuscating C++ behind a bunch of proprietary API's. It's perfectly possible to learn the whole language from simple command line programs.

    If (and I stress IF) your chosen field is windows GUI programming, then you can learn MS's MFC or Borland's VCL or whatever. But without the solid foundation backing it up you'll be stuck with the proprietary stuff.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I agree, trying to learn good, solid C++ at the same time you're trying to master GUI programming makes the process overly complicated. I suspect the school views starting out with a GUI to be catering to "public demand" and thereby increasint student interest, and, in a way, I see their point. After all, the most obvious human/computer interface these days is a GUI. Besides, getting people "hooked" on the final product and then allowing them/assisting them to dig down deeper into the theory/basics of the product as they find necessary has a long and glorius tradition.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM