This is topic Need more help in C++ involving strcpy() and strcmp() in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=045241

Posted by Blayne Bradley (Member # 8565) on :
 
Ok so I include the header #include "strings.h"

And next I try doing the string functions Im making a program that will take a pre-enmtered list of names and weights and sort the names with their corresponding weights by alphabetical order.

I completewly understand how the logic is supposed ot work buuuuuuuut.......


In the case of:

[quote]

if ( strcmp( A[MAX][Jin], A[MAX][Jin + 1] ) == 1 )
{
do whatever;
}
else
{
do else;
}

etc....

I get cannot convert from char to const char *'

[Confused]

This is inside the function "void Bubble" its the if of a Bubble sort algorthm.

void Bubble ( char A[LMAX][NMAX], int W[LMAX] )
{

etc

}

LMAX and NMAX are constands for the NMAX being 10 or max size of each string name and LMAX max number of string names in array.

I made MAX = LMAX;

Basically is A[][Jin] is greater then A[][Jin + 1] then contiue on in the loop.


Why am I getting erros that I was never taught to undertand [Frown]


Edit: Strcpy gives same error.
 
Posted by airmanfour (Member # 6111) on :
 
Go outside and build a treehouse!
 
Posted by Blayne Bradley (Member # 8565) on :
 
... dee gi hi zi ge eee errrrr WTF! [Confused]
 
Posted by ricree101 (Member # 7749) on :
 
It's kind of hard to see what is going on from the information you've provided, but it kind of looks like A is a char**. You are dereferencing A twice, which gives you a char rather than the char* you need.

I'm not entirely sure what MAX does in this situation. If you explain a little further, I might be able to help more.

Also, from what I've read about strcmp, the return value is:
<0 string1 is less than string2
0 string1 is the same as string2
>0 string1 is greater than string2

Therefore, testing to see if the return value is 1 is probably not what you want to do.
 
Posted by Nighthawk (Member # 4176) on :
 
First off, "strcmp" will not necessarily return a value of 1, but will return a POSITIVE value if greater and a NEGATIVE value if lesser.

Also, the second value in the array definition is the character length, and the first is the number of the string. So A[0][n] is the "n"th character in the first string.

So, if LMAX is the maximum amount of strings, and NMAX is the maximum length of each string, to compare string "m" and "n" ("m" and "n" being 0..m..LMAX-1 and 0..n..LMAX-1):

if ( strcmp( A[m], A[n] ) > 0 )
...
 
Posted by HollowEarth (Member # 2586) on :
 
Like ricree said, you dereferenced A twice, and strcmp takes a pointer so you actually want &A[MAX][Jin] .
 
Posted by Blayne Bradley (Member # 8565) on :
 
I'm not entirely sure myself A[][] being the Name array of

Name = { "Tom", "Dick", "Harry" }

except of course its past as "A"

LMAX = 100;
NMAX = 20;

Because A is a 2D array thus [][].

The Modifyers I had were "Jin" being a number that determines a postion in the array (I think) and the other one. Which I wasnt sure what it was so I assumed it was LMAX as MAX.
 
Posted by Blayne Bradley (Member # 8565) on :
 
www.vassili.zambinidirect.com/c++.txt

Is the tetxt file where I copied and pasted the majority of my code that I think is needed to undertsnd what im doing.
 
Posted by ricree101 (Member # 7749) on :
 
ok, if you have some array
Name = { "Tom", "Dick", "Harry" }

You only want to dereference Name once;

For example, Name[0] is the char* corresponding to "Tom"

Name[0][0] is 'T'
 
Posted by Blayne Bradley (Member # 8565) on :
 
What does dereferenced A twice mean? What did I do exactly to cause it Im not aware of why things dont work only when they do work.
 
Posted by Blayne Bradley (Member # 8565) on :
 
Aha.

Thank you.
 
Posted by HollowEarth (Member # 2586) on :
 
Right. In C an array is just a pointer the first element. So if you have a two dimensional array what you have is an array of arrays. So some examples:
code:
char foo[10][10]; // foo has type char**

When you use [] it dereferences the pointer. That means that it returns the value at that address rather than the address itself:
code:
foo[1]  // returns char*
foo[1][1] //returns char

strcmp takes a const char* since c strings are just character arrays with a null in the last slot. So when you wrote A[MAX][Jin] you're just passing strcmp a single character (the first one in the string) so you need to take the address of the that character. You do that by using the '&' in front. so:
code:
if ( strcmp( &A[MAX][Jin], &A[MAX][Jin + 1] ) == 1 )
{
do whatever;
}
else
{
do else;
}

Like was said, check the docs for strcmp and then figure out what you want that test to be, (probably == 0 or != 0) since it being exactly equal to 1 is probably implementation dependent.
 
Posted by Nighthawk (Member # 4176) on :
 
Typical how it takes five programmers explaining the same thing to be understood. We don't make things easy, do we? [Wink]
 
Posted by ricree101 (Member # 7749) on :
 
Are you using some string.h given to you by the class rather than the C standard library string.h?

Also, what's with the variable names? You might want to get in the habit of choosing names that make it's purpose more obvious.
 
Posted by Blayne Bradley (Member # 8565) on :
 
The names given where "J" or "P" you know 1 letter vairbales so I lengthened them for fun.

Its C standard string.h

Also its saying when I do strcpy to try strcpy_s cuz' its safer is there a reaosn for this? It makes some of my errors go away.
 
Posted by Nighthawk (Member # 4176) on :
 
quote:
Originally posted by ricree101:
Are you using some string.h given to you by the class rather than the C standard library string.h?

Also, what's with the variable names? You might want to get in the habit of choosing names that make it's purpose more obvious.

I've found that textbooks like using variable names that don't mean squat: "A", "B", "C", "x", "y"... It's a really bad practice they have, because it causes new programmers that hit the real world and start coding to continue this practice, making production code not as easy to decipher sometimes.

Real development shops sometimes enforce using the special prefix notation; I forget what it's called. "sz..." for strings, "i..." for integers, "b..." for booleans, etc...
 
Posted by King of Men (Member # 6684) on :
 
On another forum I frequent, one of the moderators has this in his sig:

quote:
If you are posting in 'For Beginners' and your post contains the word 'char', your code has a bug. std::string roxxors teh big one!!!
Is there any particular reason you can't use the standard library for this?
 
Posted by Nighthawk (Member # 4176) on :
 
quote:
Originally posted by King of Men:
On another forum I frequent, one of the moderators has this in his sig:

quote:
If you are posting in 'For Beginners' and your post contains the word 'char', your code has a bug. std::string roxxors teh big one!!!
Is there any particular reason you can't use the standard library for this?
That quote is stated in jest; no real programmer would be caught dead saying that.

Why? Because it's technically not "standard", at least in high end production environments. In real programmer shops, the "standard" is ANSI C or very, very basic C++, which does not include the Standard Template Library, Microsoft Foundation Class Library, etc... it's bare bone C/C++ programming without bells and whistles.

In some programmer shops (at least three that I've worked in), rather than use existing string classes ("std::string", "CString", etc...) they create their own from scratch.
 
Posted by Blayne Bradley (Member # 8565) on :
 
hmm? How am I not using the standard library?
 
Posted by HollowEarth (Member # 2586) on :
 
eh Nighthawk he is obviously learning, so I don't see why that specifically would be a problem. Though it seems more likely that its just a restriction of the class.

That prefix notation is called hungarian notation.

Look here and here for info on strcpy_s.
 
Posted by Nighthawk (Member # 4176) on :
 
quote:
hmm? How am I not using the standard library?
You're using C/C++, which is standard.

What he's referring to is "STL", or the "Standard Template Library", which is a collection of classes that are used to simplify certain functions. For example, they have a "string" class for strings, a "vector" class for arrays, etc...

quote:
eh Nighthawk he is obviously learning, so I don't see why that specifically would be a problem.
Don't get me wrong; I'm not directly criticizing it. I mention it solely so that he's aware, and I always advise that it's a good practice to start early. Hopefully, and it might not be the case, Hungarian notation will eventually be discussed and recommended for reasons I've stated. But, in the few computer classes I've taken (I've taken three whole classes in my lifetime), recommended practices for naming variables was never talked about.
 
Posted by ricree101 (Member # 7749) on :
 
If you look at his code, there is a line:
#include "string.h", so he may have to use some string library his class gave him.
 
Posted by King of Men (Member # 6684) on :
 
The STL is part of the ANSI C++ specification. If people refuse to use it and thereby impair their productivity, that is their problem. And C++ (not C) is certainly standard in game development; possibly you have worked in a different part of the industry.
 
Posted by Nighthawk (Member # 4176) on :
 
quote:
Originally posted by King of Men:
The STL is part of the ANSI C++ specification. If people refuse to use it and thereby impair their productivity, that is their problem. And C++ (not C) is certainly standard in game development; possibly you have worked in a different part of the industry.

Actually, I AM a game developer...

As for it being a standard, nowadays it is. Although I have to point out that Quake I and Quake II were written in ANSI C, not C++.

Maybe things have changed recently, I don't know. But I know that in the Canvas graphics software package or in the Half-Life and Half-Life 2 Software Developer's Kit, STL is *never* used.
 
Posted by Will B (Member # 7931) on :
 
The code linked to is kind of a mess. Hard to read. Names like "A" and "W" aren't very clear. Comments would also help. Seriously, if you can make the code clear, you can make it work.

It does seem that the string.h in question is not a system file, becuase it's got "" not pointy-brackets around it.

C++ STL strings are nicer, but you go with what you have...

And to answer the question of precisely what's wrong:


char Hold; //Temp Variable
char Hold2;
//Note that Hold and Hold2 are single characters and can't be used for holding char strings. They should be instead
//char Hold[NMAX];
//char Hold2[NMAX];

...
do
{
Hold2 = A[MAX][Jin];
//Here, Hold2 is going to be the Jin-th character of the MAX-th string. Whoa! First of all, there is no MAX-th string; if MAX==LMAX==10, then the strings are 0..9. Second, we don't want to deal with the MAX-th string but the Jin-th one. Third, we don't want to consider individual characters -- we should use strcmp to compare strings, and let it worry about individual characters.
if ( strcmp( Hold2, A[MAX][Jin + 1]) == 1 )
//We should compare Hold2 to A[Jin+1]

{
Chiang = 1;
Hold = A[MAX][Jin];
A[MAX][Jin] = A[MAX][Jin + 1];
A[MAX][Jin + 1] = Hold;
//You can't use = to copy char arrays; use strcpy
}
...

My suggestion is to start with the Wikipedia entry on bubble sort: http://en.wikipedia.org/wiki/Bubble_sort . It isn't in C++, but it's essential to know the algorithm, too.

And whoever commented that strcmp might not return 1 was spot-on. It's better to say if (strcmp (str1, str2) > 0) ...

Good luck!
 
Posted by fugu13 (Member # 2859) on :
 
NIghthawk: a lot of that is due to availability. Until recently STL implementations tended to be very buggy in ways dependent on whose STL lib one was using.

Additionally, the STL is far too bulky for embedded use, a big area for C/C++, and most C++ programmers out there have not been trained in its use and write bad STL code, neither of which speeds adoption.

However, many places are using not just the STL nowadays, but the excellent Boost C++ libs that layer on top of it (and are strongly supported by the C++ standards body).
 
Posted by Blayne Bradley (Member # 8565) on :
 
Okay strcmp(&A[Jin], &A[Jin+1]) doesnt work but it dosnt complain without the andparsands.

So ya I thinks Im finally making progress.
 
Posted by Blayne Bradley (Member # 8565) on :
 
oh and once again thanks.
 
Posted by Corwin (Member # 5705) on :
 
Either I don't really understand the variables here, or it's very simple.

If A is a set of strings, A[n] is the "nth-1" string in A (they start from 0) and A[n][i] is simply the "ith-1" character in the A[n] string.

&A[n] is the address of the "nth-1" string, meaning where it is in memory. It is NOT the string. You're comparing the addresses of two different variables, of course they are different.

Use "strcmp" to compare strings.
Use "==" to compare characters. That's why there was an error when you compared two A[x][y]-s with strcmp.

Think well what your variable means. Once you figure it out, it's easy to see what function to use each time.
 
Posted by Bokonon (Member # 480) on :
 
What I wonder is why the heck Blayne is being taught sorting algorithms and multi-dimensional arrays without a solid foundation in pointers and memory allocation (more the former than the latter)???

Blayne, if you really want to figure this out, I recommend you stop doing hunt-and-peck debugging (trust me, I've been there, done that), and find a decent tutorial on pointers, and what '&' vs. '*' really mean. If you want to do any serious performance programming, particularly in C/C++ you'll need to really grasp these concepts.

Here's a link to a pointer's intro (in pdf form). I don't know how good it is, but maybe it'll help you:

Pointers!

-Bok
 
Posted by Corwin (Member # 5705) on :
 
Good point, Bokonon... [Wink]

Oh, and Blayne, don't mix C and C++. It blows. The & can mean different things depending on which you use. Stick to C for now, it's enough to learn simple algorithms.

Can I ask in what context this is? That is, if you have a class in C, or you're just doing this on your own, etc. If it's a class and you haven't reached pointers and memory allocation it's simpler to stick with arrays of integers.
 
Posted by Nighthawk (Member # 4176) on :
 
quote:
Originally posted by Bokonon:
What I wonder is why the heck Blayne is being taught sorting algorithms and multi-dimensional arrays without a solid foundation in pointers and memory allocation (more the former than the latter)???

You know, you're right. It didn't really dawn on me. Since I've never taken a course in C++, I can't really be sure of the order of learning (when I took classes, they were in Pascal, which is much easier to deal with than C/C++ in this regard). But if you're going to be teaching pointers, arrays, etc... creating a bubble sort is a rather intense "trial by fire", compared to other examples that can be used.

Sorting and searching methods, if discussed at all, come much later in the course. The foundation for syntax is set first before anyone talks about theory.
 
Posted by Blayne Bradley (Member # 8565) on :
 
my last C++ course in regards to arrays and pointers was about a year ago since I failed a certain VB course i had to wait a year before doing my next C++ course and as such i forgot alot i learnt and its only slowly coming back.
 
Posted by Bokonon (Member # 480) on :
 
Well, sorting isn't so bad, if he were dealing with a basic array of integers. I think it's okay to go over the theory and try and implement a basic sort of various types, particularly if you are discussing performance concerns (which in my case, since the algorithm course was a second year course, involved some quick overview, and some handwaving, though with sorting algorithms, the derivation is fairly straightforward, so long as you stick to Big-O considerations).

But trying to deal with manipulating multi-dimensional arrays, on top of implementing a search, seems a tad much. Either the class is structured weirdly, IMO, or they have taught some basic ideas, and Blayne doesn't quite grasp them yet, or that "string.h" include is supposed to encapsulate most of these issues, and Blayne is over-engineering his assignment(?) by using strcpy() and strcmp().

-Bok
 
Posted by King of Men (Member # 6684) on :
 
Come now, a two-dimensional array is not so complicated as all that. I grant you that chars are a bad place to start with them, though.
 
Posted by King of Men (Member # 6684) on :
 
Speaking of C++, I'm having a spot of trouble with the preprocessor. What I want to do is to be able to turn on debugging statements with a single define, and have the debugs appear in my code as ordinary

someStream << "Something happened" << endl;

statements. I first tried this:

code:
#define DEBUG
#ifdef DEBUG
#define debugstream std::cout
#else
#define debugstream //
#endif

The plan was, then, that

code:
debugstream << "Something bad!" << std::endl;

should appear to the compiler as

code:
// << "Something bad!" << std::endl;

This does not happen, because the preprocessor removes the // before the define stage, and thus sees debugstream being defined as an empty string; hence I get

code:
 << "Something bad!" << std::endl;

which is not a valid statement.

What I eventually came up with was

code:
#define DEBUG
#ifdef DEBUG
#define debugstream(x) std::cout << x
#else
#define debugstream(x)
#endif

which works, but has the disadvantage of looking like a function call instead of a stream input. It's not very important, I just like things to be elegant. Can anyone think of a way to do this the way I want?
 
Posted by Bokonon (Member # 480) on :
 
I assume you tried escaping the "//" in case the preprocessor stripped them out during the comment phase?

I might also go with a

code:
/* cout << "Something Bad!" << std::endl; */

instead. Don't know if it would work though.

-Bok
 
Posted by King of Men (Member # 6684) on :
 
The preprocessor does not do string handling, as far as I know; escaping them would just give the compiler \/\/ << "Something Bad" << endl;, which it would not like. I do not understand how you want to implement your second suggestion; could you give the preprocessor code, please?

Edit : To answer your question with experiment instead of theory; no, the preprocessor does not do string handling. [Smile]
 
Posted by Bokonon (Member # 480) on :
 
Hehe. It's been a while since I did any C/C++ coding. Did you try the other suggestion? I'm guessing the preprocesser is going to strip comments regardless their format...

You could always use a log4* library (I think they have a C++ version). Overkill, but it would probably work [Smile]

-Bok
 
Posted by King of Men (Member # 6684) on :
 
No, Bok, I did not try your other suggestion, because I do not understand what it actually is. Did you read my post?
 
Posted by ricree101 (Member # 7749) on :
 
Is there any way to have a stream that doesn't really go anywhere? If so, you could always have that go in place of cout if debugging is turned off.
 
Posted by King of Men (Member # 6684) on :
 
Yes, but then the statement still gets executed. I want it removed at compile time. [Smile]
 
Posted by Corwin (Member # 5705) on :
 
Huh, I've always surrounded everything that was for debugging with #ifdef DEBUG - #endif blocks. Easiest way I guess. [Dont Know] Trying to mix debugging with non-debugging code kind of seems against my initial purpose.

I'd have:

#ifdef DEBUG
std::cout << "Something Bad!" << std::endl;
#endif

Also helps me see the debugging related parts right away.
 
Posted by Blayne Bradley (Member # 8565) on :
 
Whats odd is that strcpy doesnt work only strcpy_s does on my computer or at least with Visual Studios 2005.
 
Posted by King of Men (Member # 6684) on :
 
Yeah, that's what I did initially too, but that's a lotta damn typing...
 
Posted by Bokonon (Member # 480) on :
 
KoM, looking at it again, you probably can't do it... Like I said, it's been a while.

-Bok
 
Posted by HollowEarth (Member # 2586) on :
 
Blayne, does it actually not work, or does it just give a warning? Microsoft's site seems to indicate that it should just warn.
 
Posted by Blayne Bradley (Member # 8565) on :
 
not sur eif tis an error that doesnt let me do things or just a warn I dont remember.
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2