473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Program for School

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
Dec 10 '06 #1
10 1279

PouBear wrote:
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

Dec 10 '06 #2
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

Dec 10 '06 #3
PouBear wrote:
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/h...UrlParser.html
Dec 10 '06 #4
IR
PouBear wrote:
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,
--
IR
Dec 10 '06 #5
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[i] << "\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
Dec 10 '06 #6
IR
PouBear wrote:
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[i] << "\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,
--
IR
Dec 10 '06 #7

IR wrote:
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.
>

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,
--
IR
Dec 10 '06 #8

PouBear wrote:
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[i] << "\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.hinstead of <cstringthese 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

Dec 10 '06 #9

PouBear wrote:
Having trouble getting this to compare the strings. What am I doing wrong
#include<iostream>
#include<cstring>
using std::cout;
using std::cin;
<snip>
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 <cxxxvs <xxx.hheaders) 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
<cxxxheaders in both the std and the global namespaces, so you may
well find your code compiles with no problem.

Gavin Deane

Dec 10 '06 #10
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:\\", "news:\\", "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;
}*/

Dec 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ruth | last post by:
Alison, You are going to find this a real coincidence! To begin with, I am Roberta Laird's husband. I think you know Roberta. We also are friends with Laura McConnell. Anyway, I am in business...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
10
by: preethamkumark | last post by:
- The program first creates a shared memory buffer containing an array of 20 integers. - Each slot of the buffer can have either 0 or 1, where 0 represents an empty slot, and 1 represents an...
5
by: shanknbake | last post by:
Here is my code. I've noted where the program crashes. I'm doing this program as a project for school. //cqueue.h file //HEADER FILE http://rafb.net/paste/results/Nh0aLB77.html...
5
by: Lockwood | last post by:
someone check out this program and tell me what im doing wrong please...every time i fix some errors i get even more than before (right now theres 10). note that im a beginner/noob, and that this...
2
by: sallyk07 | last post by:
Modify the Student class so that each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the...
2
by: Richard Hollenbeck | last post by:
I originally wrote my grades program in MS-Access to help community college teachers in the California community colleges keep track of their students' grades and produce reports for the students...
5
by: zr3cool | last post by:
heres the program description.... There exist two text files that are not identical with a size of between 100 and 250 characters. Write a c++ program that will read in these two text files,...
2
by: bubbles19518 | last post by:
I have my c project which works fine and compiles fine on my home machine (MacBook Pro Mac OSX). When I upload it to my school server and compile it there it compiles but I get a bus error when I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.