Program for School

P

PouBear

I'm a new programer and I have a program due Monday 12-11-06. My program
must take a URL from the user as input. The program must output the
protocol used and the top level domain.If the protocol is not included in
the URL or the program does not recognize the protocol, the program must
output messages to relay that information. Likewise it must do the same
for the domain. We are currently working with pointers and arrays. We have
to use strncmp and strncat.
sincerly PouBear
 
E

Evan

PouBear said:
I'm a new programer and I have a program due Monday 12-11-06. My program
must take a URL from the user as input. The program must output the
protocol used and the top level domain.If the protocol is not included in
the URL or the program does not recognize the protocol, the program must
output messages to relay that information. Likewise it must do the same
for the domain. We are currently working with pointers and arrays. We have
to use strncmp and strncat.
sincerly PouBear

That sounds like an interesting assignment. You should have fun working
on it.

(Leaving behind the glibness, you're not going to get someone here to
do your homework for you. If you have a specific problem with what
you're working on feel free to post and we'll try to help you out.)

Evan
 
P

PouBear

Evan I sorry you feel I'm trying to get someone to do my assignment for
me,that was and is not my intention. I need suggestions as to how to
create the function to compare the strings.

PouBear
 
G

Gianni Mariani

PouBear said:
I'm a new programer and I have a program due Monday 12-11-06. My program
must take a URL from the user as input. The program must output the
protocol used and the top level domain.If the protocol is not included in
the URL or the program does not recognize the protocol, the program must
output messages to relay that information. Likewise it must do the same
for the domain. We are currently working with pointers and arrays. We have
to use strncmp and strncat.
sincerly PouBear

Hint: look for implemenatations of a "URL" parser. Google is your friend.


I wrote this one...
http://austria.sourceforge.net/dox/html/classat_1_1UrlParser.html
 
I

IR

PouBear said:
Evan I sorry you feel I'm trying to get someone to do my
assignment for me,that was and is not my intention. I need
suggestions as to how to create the function to compare the
strings.

As Evan pointed it out, show us some code you wrote. We'll be glad to
correct it, or give you pointers to a better design.

But just keep in mind that most people around here already know how to
do that, and that they are not willing to give you any help as long as
you didn't show us that you really worked on it.

IOW, you have to make it yourself, even if we may have to help you. :)

Cheers,
 
P

PouBear

Having trouble getting this to compare the strings. What am I doing wrong
#include<iostream>
#include<cstring>
using std::cout;
using std::cin;

int main()
{
const int NUM_URL =15;
const int MAX_LENGTH = 5;
char *Pro [ NUM_URL]={"http","https",
"ftp","mailto","news", "gopher"};

cout << "\t***URL - an address on the World WideWeb!!!***\n";
cout << "Enter your favorite URL:\n\t"; //asks user forfavorite URL
char str1[7]; //gets URL from user
cin >> str1;
for ( int i =0; i < NUM_URL; ++i)
cout << str1 << "\n";

cout << "The protocol is: " ;
// if (strncmp(str1, http, 7) == 0) cout << "http\n";
// else if (strncmp(str1, https, 7) == 0) cout << "https";
// else if (strncmp(str1, ftp, 7) == 0) cout << "ftp\n";
// else if (strncmp(str1, gopher, 7) == 0) cout <<
"gopher\n";
{
cout << "Not included";

}

cout << "\nThe top level domain you entered is: ";

return 0;
}

PouBear
 
I

IR

PouBear said:
Having trouble getting this to compare the strings. What am I
doing wrong
#include<iostream>
#include<cstring>
using std::cout;
using std::cin;

int main()
{
const int NUM_URL =15;
const int MAX_LENGTH = 5;
char *Pro [ NUM_URL]={"http","https",
"ftp","mailto","news", "gopher"};

cout << "\t***URL - an address on the World WideWeb!!!***\n";
cout << "Enter your favorite URL:\n\t"; //asks user
forfavorite URL char str1[7]; //gets URL from user
cin >> str1;
for ( int i =0; i < NUM_URL; ++i)
cout << str1 << "\n";

cout << "The protocol is: " ;
// if (strncmp(str1, http, 7) == 0) cout <<
"http\n";
// else if (strncmp(str1, https, 7) == 0) cout <<
"https"; // else if (strncmp(str1, ftp, 7) == 0)
cout << "ftp\n"; // else if (strncmp(str1, gopher,
7) == 0) cout <<
"gopher\n";
{
cout << "Not included";

}

cout << "\nThe top level domain you entered is: ";

return 0;
}


I don't know if you are allowed to do that, but you really should
use std::string.

For now, if the user enters more than 6 characters (which is veeeery
likely), you'll run into big trouble (why 6? because your array is 7
chars and that you need to store a null terminator)...

Get back to us with either some code that uses std::string, or a
bigger buffer *if you don't really have the choice*.


BTW, you are trying to compare the 7 first chars of the input string
(which can be only 6 chars long) with "http" (which is only 4
chars...). Didn't you forget :// ?



Cheers,
 
E

Evan

IR said:
Get back to us with either some code that uses std::string, or a
bigger buffer *if you don't really have the choice*.

Well, the original message did say "We are currently working with
pointers and arrays. We have to use strncmp and strncat" which
(especially the last bit) tells me that std::string's out.
 
E

Evan

PouBear said:
Having trouble getting this to compare the strings. What am I doing wrong
#include<iostream>
#include<cstring>
using std::cout;
using std::cin;

int main()
{
const int NUM_URL =15;
const int MAX_LENGTH = 5;
char *Pro [ NUM_URL]={"http","https",
"ftp","mailto","news", "gopher"};

The array Pro is declared to be an array of 15 (NUM_URL) elements, but
there are only 6.
cout << "\t***URL - an address on the World WideWeb!!!***\n";
cout << "Enter your favorite URL:\n\t"; //asks user forfavorite URL
char str1[7]; //gets URL from user
cin >> str1;
for ( int i =0; i < NUM_URL; ++i)
cout << str1 << "\n";


This is going to overflow the input buffer. You're printing 15
(NUM_URL) characters from a string with only 7 characters. (This means
that the last 8 characters are going to be the same sort of garbage
that you'd get if you used an uninitialized variable.)
cout << "The protocol is: " ;
// if (strncmp(str1, http, 7) == 0) cout << "http\n";
// else if (strncmp(str1, https, 7) == 0) cout << "https";
// else if (strncmp(str1, ftp, 7) == 0) cout << "ftp\n";
// else if (strncmp(str1, gopher, 7) == 0) cout <<
"gopher\n";

If you uncomment those lines, I think your compiler should give you
errors about not being able to find strncmp. This is because of the
same reason that you have to include "using std::cout" at the beginning
of your program: strncmp is in the std namespace. So either prefix
those calls with std:: or include a using line for strncmp. (Note that
if you include <string.h> instead of <cstring> these would be in the
global namespace; however, the above advice is probably better.)

Now, as for the actual calls, they won't work as such. The reason is
that you're trying to pass a variable called (for instance) http into
strncmp, but there is no such variable. You probably mean to reference
the Pro array. You could also pass in a literal "http". (And, as the
other reply from IR said, don't forget ://.

Finally, note that the protocols are different lengths. For instance,
"ftp://" is only 6 characters, while "gopher://" is 9.
{
cout << "Not included";

}

cout << "\nThe top level domain you entered is: ";

return 0;
}

PouBear

I'd say a C newsgroup might be a better place to ask this question
(because the "C++ way" is to use std::string rather than character
arrays and the strblah functions) but they'll direct you back here
after going "WTF's cout?" ;-) If you happen to know printf and scanf
though, and don't mind changing your code to use those, you might try
that.

Evan
 
G

Gavin Deane

PouBear said:
Having trouble getting this to compare the strings. What am I doing wrong
#include<iostream>
#include<cstring>
using std::cout;
using std::cin;

cout << "The protocol is: " ;
// if (strncmp(str1, http, 7) == 0) cout << "http\n";
// else if (strncmp(str1, https, 7) == 0) cout << "https";
// else if (strncmp(str1, ftp, 7) == 0) cout << "ftp\n";
// else if (strncmp(str1, gopher, 7) == 0) cout <<

<snip>

Are you aware that <cstring> (like all <cxxx> vs <xxx.h> headers) puts
its names in the std namespace? So your calls to strncmp need to be
std::strncmp, or you need a using declaration like you have for
std::cout and std::cin.

Unfortunately, many compilers get this wrong and put the names from
<cxxx> headers in both the std and the global namespaces, so you may
well find your code compiles with no problem.

Gavin Deane
 
P

PouBear

I can't figure out how to get this to check for the domain after it has
checked the protocol.
Are ther any suggestions
Poubear
*#include <iostream>
#include <cstring>
using namespace std;

int main()
{
const char * protocol[6] = { "http:\\", "https:\\",
"ftp:\\",
"mailto:\\", ""gopher:\\" };
const char * domain[7] = { ".com/", ".gov/", ".org/",
".mil/", ".net/",
".us/", ".biz/" };

cout << "\t***URL - an address on the World Wide
Web!!!***\n";
cout << "Please enter your favorite URL:\n\t ";
char str1[7]; // Get input.
cin >> str1;

cout << "The protocol is: "; // Process input.
{
if (strncmp(str1, protocol[0], 5) == 0) cout <<
"http\n";
else if (strncmp(str1, protocol[1], 6) == 0) cout <<
"https\n";
else if (strncmp(str1, protocol[2], 4) == 0) cout <<
"ftp\n";
else if (strncmp(str1, protocol[3], 7) == 0) cout <<
"mailto\n";
else if (strncmp(str1, protocol[4], 5) == 0) cout <<
"news\n";
else if (strncmp(str1, protocol[5], 7) == 0) cout <<
"gopher\n";
else
cout << "Not included\n"; //if protocol is not found
it will output this line.
}

cout << "The top level domain is:";

{
if (strncmp(str1, domain[0], 4) == 0) cout << "com\n";
else if (strncmp(str1, domain[1], 4 ) == 0) cout <<
"gov\n";
else if (strncmp(str1, domain[2], 4) == 0) cout <<
"org\n";
else if (strncmp(str1, domain[3], 4) == 0) cout <<
"mil\n";
else if (strncmp(str1, domain[4], 4) == 0) cout <<
"net\n";
else if (strncmp(str1, domain[5], 3) == 0) cout <<
"us\n";
else if (strncmp(str1, domain[6], 4) == 0) cout <<
"biz\n";
else
cout << " Not included\n";
}

return 0;
}*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top