PDA

Click to See Complete Forum and Search --> : Crash Course! - 3 Questions


Nov 8th, 2000, 12:18 PM
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.

parksie
Nov 8th, 2000, 04:44 PM
1: If you use a string for your target, then you can check that way. However, you need to use the official iostream library ;):

#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]

Nov 9th, 2000, 12:33 PM
damn, can someone decypher whatever the heck parksie just said?

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

What is a namespace?

parksie
Nov 9th, 2000, 01:41 PM
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.

Nov 9th, 2000, 01:49 PM
Parksie,
everytime I try the code you have


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

parksie
Nov 9th, 2000, 02:00 PM
Works fine for me (that exact code) in VC++ 5.

HarryW
Nov 9th, 2000, 09:14 PM
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.