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

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




String Evaluation

 
Reply to this topicStart new topic

String Evaluation

Artaniz
post 8 Dec, 2005 - 11:13 AM
Post #1


D.I.C Head

**
Joined: 11 Apr, 2005
Posts: 127


My Contributions


i am have a function that outputs and seraches a string. but if the string doesn't match then it sould return a "-1" but it always retunr sthe "-1" and i can't find why

CODE

void stri()
{
    int x = 0;
   int a,b,n,m,found;
   n = strlen(str);
   m = strlen(par);      
   for (a=0; a<=n-m; a++)
   {
       found = 1;
       for (b=0; b<m; b++)
 {
          if (par[b] != str[a+b])
          {
              found = 0;
     x = a + 1;
          }
 }
 if (found)
 {
     cout << str <<", " << par <<" - " << x;out << str <<", " << par <<" - " << x;
 }
 if (!found)
 {
     cout << str <<", " << par <<" - " << "-1";out << str <<", " << par <<" - " << "-1";
 }
    }
}
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 8 Dec, 2005 - 11:21 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


First thing that comes to mind is the condition...you may want some brackets around
CODE

a<=n-m

like
CODE

a<=(n-m)

Secondly, I don't see any declarations for str and par...are they global?

Thirdly, have you printed out the value of the found variable after the loop, but before it is evaluated? does it ever come back as a 0?
User is offlineProfile CardPM

Go to the top of the page

Artaniz
post 8 Dec, 2005 - 11:29 AM
Post #3


D.I.C Head

**
Joined: 11 Apr, 2005
Posts: 127


My Contributions


i added the brackets around the n-m and the str and par are global
im still not sure what you mean by the 0, by the way my codes looks it should only be 0 if they dont match and if they do then found = 1

This post has been edited by Artaniz: 8 Dec, 2005 - 11:31 AM
User is offlineProfile CardPM

Go to the top of the page

Artaniz
post 8 Dec, 2005 - 11:52 AM
Post #4


D.I.C Head

**
Joined: 11 Apr, 2005
Posts: 127


My Contributions


ok i fixed it. i made found global and made if statement
thanks for the help
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 8 Dec, 2005 - 12:28 PM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


Well, I'm glad you got it going, but what I had meant by the 0 and one is that traditionally a zero is considered true, and anything ese is false. Since you were interpreting an integer as a boolean value, I was wondering if you'd checked to see the value before the comparison, or were relying solely on the output of the comparison.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 03:52AM

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