Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,132 people online right now. Registration is fast and FREE... Join Now!




char search and suggest -c

 
Reply to this topicStart new topic

char search and suggest -c, how to catch each char of user typing and suggest a word from a list

gumstic
28 Oct, 2006 - 01:17 PM
Post #1

New D.I.C Head
Group Icon

Joined: 28 Oct, 2006
Posts: 25


Dream Kudos: 25
My Contributions
The idea here is to "catch" characters while a user types and suggest him/her a couple of "matching" words saved, from a "dictionary" file.

I just need a kick-start to get my head around this... even though there's a lot more to add to it.

CODE

#include <stdio.h>
main()
{
char ch[20];

printf("What do you do each morning?\n");
scanf("%s",&ch);

if (ch=="w")
{
    printf("Select an option:\n");
    printf("1. war\n");
    printf("2. work\n");
}

scanf("%s",&ch);

if (ch=="1")
{
    printf("war");

    if (ch=="2")
        printf("work");
}

else
{
     printf("w");
}

getchar();
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Char Search And Suggest -c
28 Oct, 2006 - 01:28 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
I'm not sure what the question is here, but there are a few things you may wish to look at. First, you have declared a char array, and then asked for user input. You are then comparing that input to another value. If you are comparing the entire char array to the value, you will need to use the strcmp() function, not the equality sign. If you plan simply to compare one character to the value, the you'll need to specify the index and use character literals, like so:
CODE

if(char[0]=='1')

You are asking the user to enter a choice...are they entering the number, or the word? If the number, you can use an integer variable type.

Your code to check if th value is '2' is inside an if statement that has laready checked if the value is '1'...it will not be run. I assume you wish to check if it's '2' as a separate check.
User is offlineProfile CardPM
+Quote Post

gumstic
RE: Char Search And Suggest -c
29 Oct, 2006 - 08:14 AM
Post #3

New D.I.C Head
Group Icon

Joined: 28 Oct, 2006
Posts: 25


Dream Kudos: 25
My Contributions
Probably what you said with strcmp but how can you do that with char? Here is a more clear PseudoCode of the idea:

CODE


printf for user to type;
scanf for user input;

if program detects e.g. first letter typed "w" : then
   program printf "menu": selection1, selection2;
  
   scanf for user input : selection1 or selection2;
        else
    
   if no match of word : then
        user continue typing

   program loops until next char detection...


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month