![]() |
|
#1
|
|||
|
|||
Strings, Pointers, and Printing AlphabeticallyCould someone please look at the code below and tell me what I'm doing wrong. I'm supposed to input a string of seven words and then print them on the scree. After that I am supposed to use strcmp() to help sort them and them print them alphabetically on the screen. Since I really have no Idea what the hell I'm doing I'd appreciate any help I could get. Thanks.
CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Strings, Pointers, and Printing AlphabeticallyIf you wrote that you must have SOME idea of what's going on...
You need to tell us some things so we can get a sense of where you're 'at' like... What happens? Do you get an error on compiling? post that (in code tags please) Do you get un expected output? post that (in code tags please) Did you write all that and then try to compile/run? You should 'build' in pieces, compiling and running to test as you go. That will help to narrow down a problem's causes. Like do a single scanf() printf(),,, if it works move to next,,, if not find out why and fix. repeatrepeatrepeatrepeatrepeat... |
|
#3
|
|||
|
|||
Re: Strings, Pointers, and Printing AlphabeticallyOk. Sorry about that. I have no errors when compiling or running the program. The program prompts me for the seven words. I then enter the words and I get them printed back in list form and then get the list again where they are supposed to be alphabetized. See below:
Code:
The strcmp() is supposed to help in sorting them but for the life of me I can't figure it out. Thank for the help. |
|
#4
|
|||
|
|||
Re: Strings, Pointers, and Printing AlphabeticallySorry, this has gotten long.... so I started with:
gcc -Wall -W -pedantic nh10.c -o nh10.exe nh10.c: In function `main': nh10.c:34: warning: char format, different type arg (arg 2) nh10.c:38: warning: char format, different type arg (arg 2) nh10.c:43: warning: char format, different type arg (arg 2) Ok it compiles with some warnings and a run looks like this: nh11.exe Code:
First print looks ok. So what happens to the hello on second print? First fix those warnings... CPP / C++ / C Code:
While it 'worked' in this case, it is not the proper way to do this. The '&' is used to furnish the address of an object. I guess you got the idea from a scanf() example?? That example was probably for a single data like: CPP / C++ / C Code:
This is important: The identifier name you give an array is a pointer to the space set aside in memory for the array data. In the this case: CPP / C++ / C Code:
to the starting address of 105 contiguous bytes in memory. AKA: the array's 'base' address. Try sticking this in place of the first printf() loop: CPP / C++ / C Code:
Obviously something's wrong with the bubble() because the first string gets messed up in the second output so I comment that out for and the output is now: Code:
Note a and a[0] are the same address AND that the address of each of the elements are exactly 'CHARS' bytes (or 15) apart. Get the picture? I hope so because you're going to need to understand these concepts to figure out what's wrong with bubble(). I have some stuff to get to and I won't be able to address the rest of this right now except to say that I need to review this too as I am having trouble passing a pointer to bubble()... (void* ???) It can be tricky to get the correct information to a function for it to work. I will leave you with this link to an EXCELLENT tutor on pointers which I will be re-reading for help. It has an example of what you are trying to do : http://pw1.netcom.com/~tjensen/ptr/pointers.htm In addition to that there are several great threads on this forum re: this. Go to the top right of the forum listings page. Search 'pass, dimensional, array' or 'pass array strings' etc... Merry Whatever!,, Howard(); Last edited by Howard_L : 24-Dec-2007 at 11:20.
|
|
#5
|
|||
|
|||
Re: Strings, Pointers, and Printing AlphabeticallyQuote:
Ok. I have no idea what any of the above means. Looks like jibberish to me. Anyway, thanks for the link it has alot of good information. Unfortunately, I don't really understand it nor do I really care too. I'm just trying to fight through this course. If I can get a C I'll be more than happy. Thanks for all the pointers. I'll be banging my head on my desk until I get this thing to work. |
|
#6
|
|||
|
|||
Re: Strings, Pointers, and Printing Alphabetically...I guess so,, sorry
|
|
#7
|
|||
|
|||
Re: Strings, Pointers, and Printing AlphabeticallyPerhaps this will help without giving too much away...
CPP / C++ / C Code:
It's close to what was originally posted.: Code:
|
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 09:25 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The