Results 1 to 7 of 7

Thread: Crash Course! - 3 Questions

  1. #1
    Guest
    Hi, i need efficient code examples of the following scenarios...

    1. How to check the length of a string entered by the user (using the CIN method of the IOSTREAM.H file) and how to truncate a string if it is too long for the target char variable [8].

    2. Read and write to a file (sequential access preferably, if that is applicable to C++)

    3. Drawing shapes on the screen through code.

    Thanks.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    1: If you use a string for your target, then you can check that way. However, you need to use the official iostream library :
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void main() {
        string sInput;
    
        cin >> sInput; // or sInput << cin...can't remember right now
    
        // length: sInput.length()
    }
    2: Use the fstreams classes for higher level stuff (header fstream). If you want to play around with binary files, I would suggest a bit of the ol' low-level stuff . It's not too hard, and I posted a class called DirectFile somewhere on the forum .

    3: Console or windowed? If it's console then check out http://www.vesa.org/vbe3.pdf for more info.

    ...one edit later: You'll love this one - http://www.scitechsoft.com/dp_mgl.html

    [Edited by parksie on 11-08-2000 at 05:50 PM]
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Guest
    damn, can someone decypher whatever the heck parksie just said?

    I'm using iostream.h and declaring variables as CHAR ... []

    What is a namespace?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    damn, can someone decypher whatever the heck parksie just said?
    It was a bit bad, wasn't it...

    I'm using iostream.h and declaring variables as CHAR ... []
    That's the point. You're not supposed to use that one. That one's a Microsoft version. The official, real one is iostream. If you use a string, then you can check the length after the text has been entered. Then, use the c_str() member function and strncpy to copy the first eight to your array, to prevent access violations.

    What is a namespace?
    It's just a mechanism for making sure that there are no name collisions between different things. All the official parts of the Standard C++ Library are under namespace std.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Guest
    Parksie,
    everytime I try the code you have

    Code:
    #include <string>
    using namespace std;
    int main()
    {
    string mystr;
    return(0);
    }
    I get lots of errors, most are in <string>, and it say that
    using namespace std;

    is wrong syntax

    I have tried MSVC++, BC++B, BTC++.... all say that.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Works fine for me (that exact code) in VC++ 5.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    In the build menu, there's an option 'clean', try that, then recompile. If it still doesn't work paste the code into a new project.
    Harry.

    "From one thing, know ten thousand things."

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