Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




String searching

 
Reply to this topicStart new topic

String searching

chuck981996
post 4 Oct, 2008 - 06:39 PM
Post #1


New D.I.C Head

*
Joined: 3 Jul, 2008
Posts: 9


My Contributions


Hi all, I am experimenting with this program where if you ask the time it tells you the time. I found a function in the string.h header file which searchs for a sub-string inside a string and returns a pointer to the sub-string if it is found.

At the moment it only works if I type "time" and nothing else however if I type "what is the time" it doesn't work. It also never returns "Invalid Question!".

If there is something I am doing wrong, or if there is a better way to do this, please tell me.

Thanks in advance biggrin.gif

CODE

void time ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  string time = (asctime (timeinfo));
  cout << "The current time is: " << time <<endl;
}


int main ()
{
    char question[500];
    char * locate;
    cout << "Welcome" << endl;
    system("pause");
    cout << "Ask a Question:" << endl;
    cin >> question;
    locate = strstr (question,"time");
    //cout << locate;
    if (locate != "") time();
    else cout << "Invlaid question!";
    return 0;
}


User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 4 Oct, 2008 - 07:36 PM
Post #2


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 501



Thanked 44 times

Dream Kudos: 25
My Contributions


cin stops reading at the first whitespace, so question will only hold "what" when you test. In order to get the whole line, you need to use getline(). Also, you should be testing against NULL, not "":
cpp
int main ()
{
char question[500];
char *locate = NULL;
cout << "Welcome" << endl;
cout << "Ask a Question:" << endl;
cin.getline(question, sizeof(question));
locate = strstr (question,"time");
//cout << locate;
if (locate != NULL)
time();
else
cout << "Invalid question!" << endl;
return 0;
}
User is offlineProfile CardPM

Go to the top of the page

chuck981996
post 4 Oct, 2008 - 08:15 PM
Post #3


New D.I.C Head

*
Joined: 3 Jul, 2008
Posts: 9


My Contributions


QUOTE(JackOfAllTrades @ 4 Oct, 2008 - 08:36 PM) *

cin stops reading at the first whitespace, so question will only hold "what" when you test. In order to get the whole line, you need to use getline(). Also, you should be testing against NULL, not "":
cpp
int main ()
{
char question[500];
char *locate = NULL;
cout << "Welcome" << endl;
cout << "Ask a Question:" << endl;
cin.getline(question, sizeof(question));
locate = strstr (question,"time");
//cout << locate;
if (locate != NULL)
time();
else
cout << "Invalid question!" << endl;
return 0;
}



OK thanks! I didn't realise about cin.......I did, however try testing against NULL but it didn't work (probably because of my input problem). Thanks for the help.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 04:05PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month