![]() |
|
#1
|
||||
|
||||
Menu of character stringsHello.
I am trying to write a simple program which accepts input string from the user and compares the scanned string to each string in an array of character strings. For the time being, I assume that the input string,just as the strings in the defined array, has just single spaces between the words so that I can leave that checking to a later part. So the scanned string is compared to each of those strings, a message is sent to the user telling him what he wrote ( ..though the syntax seems to be right i suppose).Heres my program: CPP / C++ / C Code:
I doubt if the size_t type has got something to do with it but still...my program is just too simple at this stage but it still wont work Ive tried entering the string exactly as the strings in the array above(each once at a time ofcourse) but it still wont print the statement which it should. I wonder why the comparison is never showing the equality between the strings. Has it got to do something with the locale (I hardly know what a locale is so I would be pleased if someone would also throw some light on it.. Looking to hear from you, Best Regards, Aijaz Baig. Last edited by aijazbaig1 : 15-Jul-2006 at 13:41.
Reason: change the title
|
|
#2
|
||||
|
||||
Re: Menu of character stringsIf you print your string right after your input, you'll notice what was actually read. Whitespace stops a scanf() read. Also Check this out and follow the gets() link for a solution.
You also need a return at the end of main() __________________
Age is unimportant -- except in cheese |
|
#3
|
||||
|
||||
Re: Menu of character stringsHi.
Thanks for letting me know that I cannot read a white space with a scanf. I did allot a lot of space but i didn't knew that I could instead use the fgets which reads a specified number of characters or till the 1st newline character(whatever comes first) and appends a NULL after that isnt it? And this time I am printing the string using the array *idiom as it shouldn't really matter because the comparison would yield an output of zero if and only if both the arguments of the _strnicmp function have the same characters, though the case may be different. So heres the modified code and it works just fine but I think there is a problem with regards to memory initialization and hence after displaying the output it becomes non responsive and I see a pop-up window displayed by the OS I guess,informing me that the 'filename'.exe file has become non-responsive and asking me if i'd like to report this to microsoft..you know what I mean here, don't u? heres whats written in the title bar of that window: "array_menu.exe has encountered a problem and needs to close. We are sorry for the inconvenience." and heres the C code that caused windows to encounter that problem CPP / C++ / C Code:
I hope you might have understood the pop up windows that I was referring to. After all, we've all had our days with Windows, haven't we? ![]() |
|
#4
|
|||
|
|||
Re: Menu of character stringsQuote:
I don't understand how it you could could say that it "works just fine", given the behavior you describe. Maybe you could look at the conditions for exiting the do{}while() loop. Or, you could make the program tell you what it is doing: CPP / C++ / C Code:
Regards, Dave |
|
#5
|
|||
|
|||
Re: Menu of character stringsMr. aijazbaig1 maybe this is wat you are looking for :
CPP / C++ / C Code:
Hope this helps. In case of doubts do post again. Regards, Orochimaru... |
|
#6
|
||||
|
||||
Re: Menu of character stringsHello there.
I finally got the problem. My test condition in the do while loop was wrong. It should instead have been !flag as correctly pointed out by orochimaruSanin. Additionally, the bit-wise and operator which you have used is incorrect as I want to exit the loop as soon as a match is found or after all the elements have been compared implying that there was no match. Furthermore, one should never forget to replace the newline character appended to the string by fgets by the NULL character Heres the modified program. This time I have used a choice variable to check if the user wants to continue but it does 3 things wrong. 1: It doesn't actually respond to what the user is entering as his choice. Even if I enter a n or a N or even if I just press the enter key it just keeps looping and continues to search for a match. Seems to be stuck in an infinite loop. 2: For all strings entered except the 1st i.e. "create array", it cannot find a match. 3: When a match is not found it doesn't display whats it supposed to be displaying i.e. "The input string didnt match any of the entries !". Heres the modified program: CPP / C++ / C Code:
Code:
What do you thing am I overlooking? Please do let me know. Would someone explain the use of the the fflush function here?. Do we need to have such a function everytime we make a call to fgets if we want to read the characters right from the beginning of the stream for every new string entered and not from the last time where fgets stopped? So does it serve that purpose by flushing the input stream so that a new set of characters could be entered? Please do elaborate. Best Regards, Aijaz Baig. |
|
#7
|
|||
|
|||
Re: Menu of character stringsLooks like you didnt copy paste my code and then run it coz its working fine on my system.
Still i evaluated your program and found some problems : Quote:
Well actually no, its not wrong its correct. This is coz if you use the condition CPP / C++ / C Code:
and FALSE || TRUE = TRUE and hence the loop keeps on going on and on. where as if you use AND operator FALSE && TRUE = FALSE which is wat we want. Quote:
Correct me if i am mistaken but i think that fgets () automatically appends a NULL termination at the end of the received string so you need not do it. Quote:
The fflush () function flushes the stream which is provided as an input to it. So by doing fflush (stdin) i flush the input stream so that the stray input left over from the prev attempts does not interrupt with the current input session. And atlast the code snippet which caused your program to not work properly is CPP / C++ / C Code:
which should be CPP / C++ / C Code:
since in the prev stmt u end up adding 1 to the string array which causes malfinction. Also this code piece is wrong : CPP / C++ / C Code:
it should be CPP / C++ / C Code:
Hope this explanation helped. If this caused your program to work please post your feedback so thta other ppl can benefit from this. Bye. Regards, Orochimaru... |
|
#8
|
||||||||
|
||||||||
Re: Menu of character stringsYou took suggestions from a new programmer that gave you very bad information.
CPP / C++ / C Code:
You might want to define a value for the number of idiom entries. What you have is called a hard coded value. If you need to change things, you have to find all the 3's and change them, and you might miss one, or change a wrong one by mistake. So define the value with something like CPP / C++ / C Code:
CPP / C++ / C Code:
Quote:
CPP / C++ / C Code:
One way to check this type of if yourself is just before the test add: CPP / C++ / C Code:
Quote:
Quote:
CPP / C++ / C Code:
Quote:
Quote:
Quote:
CPP / C++ / C Code:
Quote:
Quote:
Also, please take another look at the Guidelines, #2F. __________________
Age is unimportant -- except in cheese |
|
#9
|
|||
|
|||
Re: Menu of character stringsThanks Mr. WaltP for pointing out such silly mistakes to me and my friend. I would be careful while using scanf () and fflush () in the near future and i have read the links posted by you. Keep up the good work. Thanks.
Regards, Orochimaru... |
|
#10
|
||||
|
||||
Re: Menu of character stringsHello there.
Thanks for the wonderful feedback Walt. I was suprised how sometimes somethings just won't behave the way you want or expect them to, making u scratch your head looking for logical reasons. This time I scanned and rescanned my program looking for some goofs which I thought may have caused the program to malfunction. I made quite a few corrections and I was able to make the program work to some extent. I still have some problems though which I have tried my best to put forth in a simple language. The program works fine during when the outer do-while loop is being executed the first time. It then asks me if I'd like to continue the program. If I enter anything other than a 'n' or a 'N' ,what happens is that it asks me for the string but it doesn't wait for me to enter it again and it keeps on asking the same question again and again whether Id like to continue or not until I enter a 'N' or a 'n'. If you haven't followed what I am talking about here, I am soon going to follow this with the modified program and the various outputs it is generating for various cases. CPP / C++ / C Code:
Say if I enter 'check for palindrome' and I indicate that I would like to continue then heres what I see on my console: Code:
Heres the sample output if I enter anything to generate a mismatch. Code:
But we cannot directly jump to the conclusion that it is remembering the contents of string1 because it is behaving differently when a match is found. As can be seen for the case when I entered 'check for palindrome' for the first time and I indicated that I wished to continue, it didn't repeat the message "you opted for entry number %d, %s\n........." This is in contrast with the case when a match wasn't found wherein it kept on repeating the message "The input string didnt match any of the entries!" after the 1st execution of the outer do-while loop. Has it got to do anything with the stream here? I hope that I have been simple and clear enough to put my problem so that you may have understood what I meant here. In addition i would like to inform you that I am using VC++ version 6, just in case if that might help you in diagnosing the problem. Hope to hear from you soon, Best Regards, Aijaz. |
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Read a .html file, check that file for links | salemite | C Programming Language | 10 | 17-Jan-2008 07:56 |
| Shapes Functions Version 2 - Arrays! | Cecil | C Programming Language | 1 | 09-Jul-2006 20:39 |
| Need help with strings | sasukekun | C++ Forum | 4 | 24-Apr-2006 10:51 |
| variables return to previous value after i try to set them | nasaiya | MS Visual C++ / MFC Forum | 2 | 14-Jun-2005 00:43 |
| Help wit my source code compiler errors | Krandygrl00 | C++ Forum | 1 | 06-Jun-2005 08:14 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The