char search and suggest -c

how to catch each char of user typing and suggest a word from a list

Page 1 of 1

2 Replies - 1215 Views - Last Post: 29 October 2006 - 09:14 AM Rate Topic: -----

#1 gumstic  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-October 06

char search and suggest -c

Post icon  Posted 28 October 2006 - 02:17 PM

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.

#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();
}



Is This A Good Question/Topic? 0
  • +

Replies To: char search and suggest -c

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: char search and suggest -c

Posted 28 October 2006 - 02:28 PM

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:
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.
Was This Post Helpful? 0
  • +
  • -

#3 gumstic  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 28-October 06

Re: char search and suggest -c

Post icon  Posted 29 October 2006 - 09:14 AM

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


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...



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1