![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
I am reviewing Arrays and need help converting some strings to arraysI am reviewing Arrays and need help converting some strings to arrays in this script. All the places in red are places where I want to create Array functions instead of calling a string. For example, When you enter the word you want to use I want to make that a scanf and place the word into an array. I know it is not the most effective way to write this but my professor does not want us tosing use strings yet and I am not sure how to do this without using strings. Any hints?
#include <stdio.h> #include <stdlib.h> #define MAXLEN 26 #define MAX 10 #define YES 1 #define NO 0 #define STARS "********************" int main (void) { char word [MAXLEN + 1], solution [MAXLEN + 1], ch, *w_ptr, *s_ptr; int num_guess = 0, len = 0, found = 0; printf("\nINSTRUCTIONS: "); do { num_guess = 0; printf("\nEnter the word for the player to guess and end with * > "); fgets(word, MAXLEN, stdin); for( w_ptr = word ; *w_ptr ; w_ptr++) *w_ptr = toupper(*w_ptr); len = strlen(word); strncpy(solution, STARS, len); solution[len] = '\0'; while( num_guess < MAX ) { printf("\n\nWord so far is %s, Guesses so far %d" ,solution ,num_guess++); printf("\nEnter a letter > "); ch = toupper(getche()); s_ptr = solution; w_ptr = word; found = NO; while ( *w_ptr ) { if ( ch == *w_ptr ) { *s_ptr = *w_ptr; found = YES; } w_ptr++; s_ptr++; } if ( strcmp(solution , word) == 0 ) break; if ( found ) printf("\nThe letter \"%c\" was in the answer",ch); } if ( num_guess < MAX ) printf("\nYou got it! The word was %s.\nIt took you %d tries \n",word , num_guess); else printf("\nYou had %d tries, the word was %s\n", MAX ,word); printf("\nPlay again [Y/N] - "); }while( (toupper(getche())) == 'Y' ) ; printf("\nGoodbye\n"); return EXIT_SUCCESS; } |
|||
|
#2
|
||||
|
||||
|
Hi jenmaz. I will not harp
too much on you for not using code tags, as you wanted to highlight some lines. However, it would be easier to read your code if you put it in code tags and then highlighted the lines in question with a comment like:CPP / C++ / C Code:
Anyway, the basis of your question confuses me. In C (not C++) the term string is relative. C does not provide a type definition for string. So a "string" in C is really only an array of chars. Whether it is defined dynamically or statically, it is still the same thing. Is it possible that your professor is saying that you can not use the C++ string type? I am not sure of any efficient and non complicated way to do your program with out using a char* or char[]. Maybe if you could show an example of what your storage type should be? __________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
|
Hi,
When some types in a word like house. That should get stored in an array. Each letter would become an element of the array. Then when people quess a letter and it is not h o u s or e the letter they typed would go into another array and counted as WRONG. (only get 6 wrong quesses) If, for example, the user types in h that is stored in Array that is CORRECT. So for the outer loop I am asking the first user to enter a word CPP / C++ / C Code:
Jennifer Quote:
Last edited by dsmith : 12-Nov-2004 at 07:15.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#4
|
||||
|
||||
|
Hi Jennifer. Can you please start using [c] and [/c] when you post your code? It really helps to view your code.
My recommendation: do not use scanf (unless your professor requires it). Your fgets is loading every char that is typed into an array. That is the definition of a string in C. Just because you use scanf, your string will still be stored in the exact same manner, which is exactly what you want (an array of chars) I believe what your prof. is getting at is don't use the C string library (ie strcpy, etc.), but use char arrays instead to do this. So for a copy you could use: CPP / C++ / C Code:
I apologize if this doesn't seem to answer your question, but the only way to store a string in C is to use an array (either dynamic or static). You could (if you were crazy) store it in a linked list I guess, but that would be pretty involved. __________________
The best damn Sports Blog period. |
|
#5
|
|||
|
|||
Thanks this helpsHi there,
This is exactly what the prof wants us to do. But I am not in understanding of what you place as copy code loop. What would source[i] and dest [i] represent in relation to my code that is existing? Quote:
|
|
#6
|
||||
|
||||
|
Okay Jen. At least I know what i am trying to do now
![]() Can you (or do you want to) use functions? If not, take this line for instance: CPP / C++ / C Code:
First there is no need to use strncpy, because the string literal is null terminated. So in this case, solution would be the destination & STARS would be the source. CPP / C++ / C Code:
The reason, I ask about functions is that this would make a nice little function (your own strcpy function if you will) CPP / C++ / C Code:
Just a thought... __________________
The best damn Sports Blog period. |
|
#7
|
|||
|
|||
|
Thanks I am going to try this and see what happens. We are suppose to create functions but my thoughts were to understand how it works then clean up the code by creating functions.
Quote:
|
|
#8
|
|||
|
|||
|
ok, I understand the copy code. I have not used the function yet. If you take this CODE below and execute it you will see that it is not doing what I need it to do.
CPP / C++ / C Code:
Quote:
|
|
#9
|
||||
|
||||
|
Hi Jen.
Use a printf statement right after the string copy code and you should see that it works (it does for me at least ).As for the rest of the program, I haven't looked to closely. (mostly because of the use of the non-standard getche). Are you satisfied that the string copy is working and just asking about the rest or are you struggling with it still? __________________
The best damn Sports Blog period. |
|
#10
|
|||
|
|||
|
Yes still strugling withthis I placed printf("%c", solution[i]); after the new code and it did not work.
I am so new and a little over whelmed. I may be thinking too much on this. This is actually due at 4PM today I need to upload this. I need to know this for an exam in 2 weeks. HELP!!! jen Quote:
|
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The