Correction

Pages: 12
#include <iostream.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>

void main()
{
char a[40];

cout <<"Enter a word: ";
cin >>a;
gets(a);

for (i=0; i <= a; i++)
{
if (strlen(a), a[i] == "e" && a[i+1] == "b" && a[i+2] == "a" && a[i+3] == "y")
{
cout <<"Accepted";
}
else
{
cout <<"Not Accepted";
}


}


getch();

}



I'm trying to make a program that accepts only a string containing the words "ebay" and "web", can somebody correct this code I created because it still has errors.

Thank you.
Please use [code][/code] tags.

You are using old-style headers:
1
2
3
4
5
6
#include <iostream.h> // should be <iostream>
#include <string.h> // should be <string>
#include <conio.h> // don't use it
#include <stdio.h> // don't use it, if you must, include <cstdio>
// new headers require the std:: namespace so add the next line
using namespace std;
( If your compiler supports old style headers, it may be quite old and you should get a new one )

void main() is non standard, use int main()

char a[40]; use C++ strings: string a;

1
2
cin >>a;
gets(a);
Use getline(cin,a); instead of these

1
2
for (i=0; i <= a; i++)
//etc 
std::strings allow you to look for a substring: http://www.cplusplus.com/reference/string/string/find/
eg:
1
2
3
4
if ( a.find ( "ebay" ) != string::npos )
    cout <<"Accepted";
else
    cout << "Not Accepted";


getch(); don't use it, see http://www.cplusplus.com/forum/articles/7312/

And return 0; from main
Last edited on
Thank you Bazzy for the correction.

Ahmm regarding to this code:

if ( a.find ( "ebay" ) != string::npos )
cout <<"Accepted";
else
cout << "Not Accepted";



this reads the entire word "ebay", what I need is a letter by letter (e, b, a, y) not an entire word (ebay).

somewhat like this,

b]a[i] == "e" && a[i+1] == "b" && a[i+2] == "a" && a[i+3] == "y"
[/b]

Can you help me out with that? Pls?
if you have a character sequence and you search for contiguous characters, you will find the entire word
eg:
a = "hello world ebay!!!" | i |i+1|i+2|i+3| h | e | l | l | o | | w | o | r | l | d | | e | b | a | y | ! | ! | !
Anyway, you can do that by using single quotes: a[i] == 'e'
so, how will I code it?

I really need to finish this now.. huhu
Just so that I can understand would this input be accepted? "efoobary"
no, like this dsfsadgsadgebayfdsagds54534


My point is I need to use the letter by letter (e, b, a, y) , not the entire word (ebay).
1
2
3
4
5
6
7
8
9
10
11
12
13
bool found = false;
for ( int i = 0; i < a.length()-3; i++ ) // a.length()-3 so that a[i+3] won't overflow
{
    if ( a[ i ] == 'e' && a[i+1] == 'b' && a[i+2] == 'a' && a[i+3] == 'y' ) // check all digits
    {
        cout << "Accepted";
        found = true; // remember that this was wound
        break; // exit from 'for'
    }
    // if isn't a match continue, it may be found later
}
if ( !found ) // if wasn't found
    cout << "Not Accepted";
Last edited on
can I ask for the entire code? please?

because I tried to run the program, no output is generated, maybe there is still something wrong:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string a;

cout <<"Enter a word: ";
getline (cin, a);

bool found = false;
for ( int i = 0; i < a.length()-3; i++ ) // a.length() -3 so that a[ i+3 ] won't overflow
{
if ( a[i] == 'e' && a[i+1] == 'b' && a[i+2] == 'a' && a[i+3] == 'y' ) // check all digits
{
cout << "Accepted";
found = true; // remember that this was wound
break; // exit from 'for'
}
// if isn't a match continue, it may be found later
}
if ( !found ) // if wasn't found
cout << "Not Accepted";

return 0;
}
oops! My mistake!


tNx yOu so much Bazzy!!

God bless you...
One last thing Bazzy,

what if "ebay" and "web" can be accepted, how can you code that??
Change the condition: if ( ( a[i]=='e' && a[i+1]=='b' ... ) || ( a[i]=='w' && a[i+1]=='e' ... ) )
waaaaaaaaaaaaaa!!
Last edited on
One problem, when I enter "dsagdsa web fsadgf395879" it is still not accepted, how can you solve that? pls?
What is your code now?
this the code that I used:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string a;

cout <<"Enter a word: ";
cin >>a;

bool found = false;
for ( int i = 0; i < a.length()-2; i++ )
{
if ((a[i] == 'e' && a[i+1] == 'b' && a[i+2] == 'a' && a[i+3] == 'y') || (a[i] == 'w' && a[i+1] == 'e' && a[i+2] == 'b') )
{
cout << "Accepted";
found = true;
}


}

if ( !found )
cout << "Not Accepted";


return 0;
}

If you want to get more then 1 word, replace cin >> a; with getline(cin,a);
And use [code][/code] tags, not [b[b][/b]][/b] tags
getline(cin,a); doesn't work, the program ran but it doesn't give any output.. :-(
hmmm, it has to work:
1
2
string a;
getline(cin, a);


maybe you forgot to stop the program before exit :-)?
it woRks now.. thaNk yOu so mUch for the heLp, i reaLLy apprEciate it..

ahmm..

now, cAn soMeone heLp me to cOde this in JAVA Language??
Pages: 12