WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

You've mostly got it.

Each C 'string' ends in a character value of 0. That's what your "out of bounds" is really checking, really it's "end of string.

Commented (and formatted properly)

int find_ substr( char *s1, char *s2)
{
    register int t;
    char *p, *p2;
    for( t= 0; s1[t]; t++)  // Move character by character thru the source string
                            // start at the first character t=0 and s1[t] 
                            // until the current character s1[t] is 0 (FALSE)
    {
        p = &s1[t];         // p becomes the address of the current character
        p2 = s2;            // p2 becomes the search string

// Following tests to see if the current position in the source string and the seach string
// matches...
        while (*p2  &&      // Continue while *p2 is not 0 (TRUE) -- end of string not found
               *p2 == *p)   //            and source and search characters are identical
        {
            p++;            // skip to the next character
            p2++;           //    in both strings
        }
        if (!*p2) return t; // out of loop -- if the current character in the seach string
                            //    is 0, we have a match.  Return the substript of the start
                            //    of the sub-string
    }
    return -1;  // substring not found 
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hi VinC,

system("pause");

Use instead.

No, don't. Learn your tools. Using scanf() to read characters is even worse than using gets() because not only does it allow you to blow your buffer boundaries but as you can see it also leaves your input stream dirty. The \n is left in the stream for your getchar() to read.

Use a combination of fgets() and sscanf() for safety and to process the input stream cleanly.

fgets(tmpstr, 80, stdin);
    sscanf(tmpstr,"%d %[abcdefg] %s", &i, str, str2);

[edit]Since you already learned this from GID, you could check out the scanf() series there. [/edit]

Grunt commented: Rightly Said - [Grunt] +1
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

For now, I'm going to continue using DM only because it's what my instructor has advised us to use....

I really hate hearing this type of comment. An instructor that can't update his tools seems IMO to not care about his students. The first paragraph of the Digital Mars License Agreement states:

The Software is not generally available software. It has not undergone testing and may contain errors. The Software was not designed to operate after December 31, 1999. It may be incomplete and it may not function properly. No support or maintenance is provided with this Software. Do not install or distribute the Software if you are not accustomed to using or distributing experimental software. Do not use this software for life critical applications.

I question the validity using it in any way simply because of this official statement. Oh well. At least you'll learn something about C/C++ and hopefully can get to a real compiler ASAP.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

i am using a program that gets run through a windows command prompt and outputs the result to the command line screen by default. i was trying to get it to output the results to a .csv file injstead but i have no idea how to do this.

You have to change the program to make the output come out in CSV format. Ancient Dragon's advice will get the data into a file for you, but it won't be in CSV format. It'll still look like the screen.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I am a novice programmer and I didn't think of that. Is it better or more common to do it that way?

I just skipped the zeroeth subscript so the first workday would be 1

Yes, it's more common to actually use the 0th element. If you need 12 elements, it makes less sense to define 13 of them. Especially in the case of a structure that might be -- say -- 1000 total bytes. A big waste of space.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Use std::string::substr()...

to break the string into 4 strings of 4 characters. Store them in a string array (or 4 variables if you don't know arrays). Then work with each of the 4 strings.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I don't normally post complete homework solutions, but since everyone else is, here's my 2 cents worth

I just gave the solution with a different approach, but well the more the merrier.

Sad... So sad...

Let's all give him his code. He can then just pick the one he likes and turn it in. Nothing solved, nothing gained... :confused:

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I haven't read any of your posts to see what the problem is but it seems to me that if you are

1) Posting your code properly formatted
2) Using code tags
3) Explaining what is going wrong in understandable detail
4) Explaining what the code should do in understandable detail
5) Not making us guess what you are talking about
6) Not expecting to be taught by us (I believe we are here to help, not teach)

you should be having no problems.

[edit]
I just looked at the last two questions you posted and I see a lot of help you've been given. Useful help.

Then you said

NOPE NOT BROKEN I DONT CARE IF I USE CORRECT GRAMER ONLINE AS LONG AS IT GETS THERE AND THE PEOPLE CAN READ IT AND AS FOR ...

which is basically saying you refuse to communicate better and don't really care if half of us can't read your posts. I'm just going to assume you were frustrated and didn't mean to say that. Is that a correct assumption to make?

WolfPack commented: Good Advice +3
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

In this thread WolfPack provide me a link to the online reference manual for the free Digital Mars Compiler that I'm using - which I've found easy to use.

I have never actually used the Digital Mars compiler, but I have never heard anything good about it. I think you'd be better off with DEV-C++ or Borland 5.5 for free. DM seems to have major trouble when you wish to learn Standard C/C++ -- I don't think it conforms to any standard.

http://www.borland.com/downloads/download_cbuilder.html for Borland 5.5
http://www.bloodshed.net for Bloodshed DevC

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Since word is defined as 5 characters, and you enter "hello", the \0 at the end overwrites memory outside your string. Try increasing the size of word for starters.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

As for syntax changes, I tried the Professional Version for some time, and the major irritation was that the use of functions like strncpy has been deprecated in favour of microsoft specific functions like strncpy_s. So you may get a lot of compiler warnings saying this and that is deprecated, blah blah blah. Of course you can turn if off with a specific preprocessor switch

Leave it to M$ to depricate standard functions in lieu of their own functions. Whether their reasoning is sound is immaterial. They should not ignore the standards because they don't like them.

I personally do not like this compiler specific code...

I despise this "compiler specific code" because of the arrogance. They can add the functions -- no problem there -- but to deprecate the Standard takes it too far.

but since I don't know the internals of the C/C++ standard implementations, I am not in a position to say where the old strncpy functions have gone wrong.

The problem with the string functions is they make it easy to overflow your string buffer. In some ways they are as dangerous as gets() and scanf("%s"...), but they are under control of the programmer, not the user. Any programmer worth his salt can handle this without difficulty.

Grunt commented: Nicely Said +1
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Hi. I can't a small part of this program to work correctly; the square root function either returns a bunch of funny symbols OR it returns 0.00

Split the statement into two. Print the value that goes into the sqrt() function to see it's value, then call the function. If you need to, break the huge statement into smaller and smaller pieces until you nail down the problem. And verify the values going into comp are valid.

1) If you have a structure nesting two other data structure types (like in my program where the structure contains an enumerated type as well as a union type) and you send this structure to a function, how do you write values directly into each of the fields via scanf?

First of all, I wouldn't.... ;) I don't like scanf.
But it looks like you're using it OK.

2) Did I use the scanf and switch statements for the enumerated types correctly? I think I may have gotten that part to work on accident -.-

Happy accident. If you print the values after reading them, you'll know for sure.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Firstly, I want it to be known clearly that you are NOT using Visual Basic.... the used the same name, but it's actually .NET or express, but not legacy VB.

I'm curious how you can tell with the information nedwards gave. I see nothing mentioning any version of anything...

I'm also curious why in a VB application someone would want to use VBScript? Why not just use VB?

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

if you don't know where to start, here's a hint

You forgot the return:

int main()
{
    // put your code here

    return 0;
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

ok thanks problem solved, i declare a global variable char b[8]; and make char *bin=b point to b, it works. but i am just wondering making bin points to b does nothing but it works can someone explain why ??

Pointing bin to b does nothing? I though it points bin to real space...

A pointer needs to point somewhere. But when you declare a pointer using char *bin; where is it pointing to? All it is is a holder of an address but no address has been loaded.

Then when you use bin = b; the address of b is now loaded, and bin points to the first byte of b. And life is good.


Now for your specific program, an easier solution is available. If you change char *bin; to char bin[9]; your problem is solved. In the first instance, bin is a pointer and must point somewhere. In the second, bin points directly to the array, so it is used as a direct pointer without changing any of your other code. And it must be 9, not 8. Remember, a C string must end in a 0, and a byte contains 8 bits -- therefore 9 values are required.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

The first problem I see is you defined a pointer bin but you never created space for it. A point simply points to space, yours points nowhere so your binary characters are going somewhere you really don't want them to.

Second problem is strrev() is not a standard function. You might be better off simply printing from the end to the beginning.

Dave Sinkula commented: Yup. +7
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Wow how old are you ? I'd expect to find you at slashdot rather than daniweb.:cheesy: I've read about those days, you submitted your code and didn't get output until that afternoon or maybe the next day!

Old.... But we were on a PDP-10 Timeshare machine. Looked like the command line today -- compile, run, fix, repeat. It was the batch people that had to wait -- but only a few minutes on this system. Batch means those punch cards -- even older than me... My record collection used to be on punch cards... :eek:


We're obviously talking about the same thing from different angles. I thought you were talking about the tool -- Dev-C, MSVC, etc., not the knowledge about how to get the job done with the language.

Hmmm you still havn't convinced me yet I still think you're attaching too much importance to the language, isn't it more about knowing how it all works. Essentially languages expose the same things variables, loops and decisions. I mean my own experience has been the language is easy, what I have found hardest *exactly is* getting from code to program.

If you don't know the language, you can't get the job done, no matter how much you know about "how it all works". But then again, you have to know how it all works to know what to code...

The real craft for me has been learning how to organise my header and class files, ...

hollystyles commented: Worthy posts +2
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

This all started over WaltP dismissing dilips' pre-processor macros as "undefined behavour" and not a real problem.

My arguement is it could be a real problem, and very possible in a large project. And the behaviour we have tested is clearly documented for the Gcc compiler and in the standard (courtesy Wolfpack)

I concede that it's is no longer undefined behaviour -- thanks to Wolfpack too. I didn't actually look up the standard to see. I made an erroneous assumption. (but it was a logical assumption :))

And I never said one should use only ONE compiler, but I stand by the notion you should take the time to know it well...

Still disagree. Irregardless of the compiler, you need to know the language. All you need to know about the compiler is how to start the compile process. You don't need to know how it parses, how it preprocesses, how to debug (although it can help admittedly), nor any of the esoteric stuff that's packaged in.

Now if you mean by 'compiler' the IDE editor (which is not part of the compiler but a surrounding package containing the compiler) I still disagree. A bare bones basic editor (Notepad) is certainly enough. I still remember editing on teletypes -- not even a screen existed -- and we did all right. But the help an IDE gives you is a big benefit -- but not required to be a top-knotch programmer. It all comes down to knowing the language …

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I would put this under "undefined behavior" and assume defining a to be b then b to be a to be a bad practice. Therefore I would abandon the concept of understanding what the compiler would do and move on to a different problem -- one that makes sense...

Just my 2 cents.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
Salem commented: Nice link to the CLC FAQ - Salem +1
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

I am novice of c++.
How to set co-ordinates of console using c++?
e.g. what to do if i wanna print pyramid of astericks at the center of the screen.
should i use gotoxy(x,y) function to go to the center of the console?

No. You should print the correct number of spaces and newlines to center your 'image'. gotoxy() is very non-standard and does not exist in most compilers.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

If you wish, your program can assume that if there was nothing on the command line, redirection is being used. In that case, instead of using the fopen(), simply assign stdin to your stream:

FILE *stream;

if (argc < 2)
{
    stream = stdin;
}
else
{
    stream = fopen(argv[i],"r");
    if (stream==NULL) 
   ...
}

This allows your program to take a file
> prog file.name

redirected file
>prog <file.name

or from the keyboard
>prog