473,654 Members | 3,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accelerated C++ "overriding commands for target"

23 New Member
Gidday

Question : Write a program that will report the longest and shortest string in its input.

I am attempting EX 3.4(page 49) from Accelerated C++ by Koenig and Moo.

I keep getting the above mentioned compiler error and I'm not too sure what I'm doing wrong. Below is the source code. Please ignore the system("PAUSE") as I use this purely to pause the dos shell. Any pointers appreciated :

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <ios>
  6. #include <algorthim>
  7.  
  8. using std::cout;        using std::endl;        using std::cin;
  9. using std::vector;      using std::string;      using std::setprecision;
  10. using std::sort;        using std::streamsize;
  11.  
  12. int main()
  13. {
  14.   // ask for and read the users name  
  15.   cout << "Please enter your name :";
  16.   string name;
  17.   cin >> name;
  18.   cout << "Hello " << name << "!";
  19.  
  20.   // ask for and read names from the user
  21.   cout << "Please enter a list of names, "
  22.                   "followed by an end-of-file: ";
  23.   vector<string> names;
  24.   string x;
  25.  
  26.   // invariant: names contains all the names entered so far
  27.   while (cin >> x) 
  28.   names.push_back(x);
  29.  
  30.   // check that the user entered some names 
  31.   typedef vector<string>::size_type vec_sz;
  32.   vec_sz size = names.size();
  33.   if (size == 0) {
  34.       cout << endl << "You need to enter some names, "
  35.                       "Please try again: " << endl;
  36.       return 1;
  37.   }
  38.  
  39.   // sort the names
  40.   sort (names.begin(), names.end());
  41.  
  42.   // output the longest and shortest name
  43.   streamsize prec = cout.precision();
  44.   cout << "The shortest name is: " << setprecision(3)
  45.        << names.begin() << "The longest name is: " << names.end();
  46.        << setprecision(prec) << endl;
  47.  
  48.   system("PAUSE");    
  49.   return 0;
  50. }
  51.  
Aug 3 '07 #1
5 2393
JosAH
11,448 Recognized Expert MVP
I think you're overcomplicatin g things a bit; why not simply keep track of the longest
and shortest strings read sofar? When you read a new string, compare it with
those two strings and update them where needed.

If you haven't read any strings yet you simply assign the currently read string
to both the shortest and the longest strings.

You don't need to sort anything or I must've misunderstood your question.

kind regards,

Jos
Aug 3 '07 #2
Madmartigan
23 New Member
I think you're overcomplicatin g things a bit; why not simply keep track of the longest
and shortest strings read sofar? When you read a new string, compare it with
those two strings and update them where needed.

If you haven't read any strings yet you simply assign the currently read string
to both the shortest and the longest strings.

You don't need to sort anything or I must've misunderstood your question.

kind regards,

Jos
Thanks Jos

I will look at another way to do this. I think I was under the impression that I had to use a vector in this way. I'm still a bit confused as to how to compare two strings with a new string entry.

Lannon
Aug 5 '07 #3
JosAH
11,448 Recognized Expert MVP
Thanks Jos

I will look at another way to do this. I think I was under the impression that I had to use a vector in this way. I'm still a bit confused as to how to compare two strings with a new string entry.

Lannon
You don't have to compare strings; you just have to compare their lengths. There's
a member function for that purpose.

kind regards,

Jos
Aug 5 '07 #4
Madmartigan
23 New Member
Came up with this which works a treat. Thanks again


#include <iostream>
#include <algorithm>
#include <iomanip>
#include <ios>
#include <string>
#include <vector>

using std::cin; using std::sort;
using std::cout; using std::endl;
using std::string; using std::streamsize ;
using std::vector; using std::setprecisi on;

int main()
{
// ask for and read the user's name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;

// ask for and read family member's names
cout << "Please enter all your family members names, "
"followed by end-of-file: ";
vector<string> family;
string x;
// invariant:famil y contains all family names read so far
while (cin >> x)
family.push_bac k(x);

// check that the user entered some names
typedef vector<string>: :size_type vec_sz;
vec_sz size = family.size();
if (size == 0) {
cout << endl << "You must enter two or more family names. "
"Please try again." << endl;
system("PAUSE") ;
return 1;
}
// sort the names
sort(family.beg in(), family.end());
cout << endl;

// declare variable of type iterator to read through all elements
// declare min and max variables setting min to 100
int max = 0;
int min = 100;
for (vector<string> ::iterator i = family.begin(); i != family.end(); ++i)
{
if ((*i).length() > max) { // work out the longest name by testing
max = (*i).length(); //the length of each input against the last
}
if ((*i).length() < min) { //work out the shortest name by testing the
min = (*i).length(); //the length of each input against the last
}
}
cout << "Shortest family member name: " << min << endl;
cout << "Longest family member name: " << max << endl;


system("PAUSE") ;
return 0;
}
Aug 5 '07 #5
JosAH
11,448 Recognized Expert MVP
Much better; well done.

A few minor nitpicks so that you can make this 'industrial strengh':

1) What if the user just enters one single name?
2) What if all the names are at least > 101 characters long?
3) Why are you still sorting that list?

kind regards,

Jos (<--- nitpicker, even in his spare time ;-)
Aug 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
20916
by: Rui Pestana | last post by:
Hello all, I want to use the POST method to submit the form and then grab the parameters in the asp file with request.form("parm"). The problem is that I am using the _search target to open the asp page. When I use _blank target there is no problem, either I use GET or POST method. But when I use _search target, only GET method works.
2
2634
by: Rob McLennan - ZETLAND | last post by:
Hi, I'm relatively clueless when it comes to correct ASP syntax. I'm testing out a search form for my company's website which is done in ASP. The results are displayed as per the code shown at the very bottom of this message. All I want to do is add a target="top" to the URL which is displayed as a link at the top of the results. I know enough about html to know that it probably goes somewhere into the follwing section: <a href=""" &...
12
3289
by: Romain | last post by:
Hi, I've used an excellent way to make target="_blank" links that still validate while using HTML 4.01 Transitional doctypes in the past. I learned the trick from : http://www.sitepoint.com/article/1041 Basically, instead of typing : <a href="yourlink.html" target="_blank" alt="">The link</a>
19
11183
by: Arthur Connor | last post by:
Currently I specified a TARGET attribute inside an <A> tag which is not allowed under strict XHTML. How do I change the line to meet the requirements of strict HTML? <A HREF="http://www.targetedomain.com/" TARGET="_top">...</A> Arty
6
77500
by: Tony Marston | last post by:
The code <a href="..." target="_blank">...</a> will not validate as XHTML STRICT because of the 'target' tag, so how do I achieve the same result by moving it to a CSS file? I cannot find anything which allows me to specify 'target=' on an anchor tag. -- Tony Marston http://www.tonymarston.net
8
10891
by: pocm | last post by:
Hi, What's the property/value I need to set in css for A to mimic the results of <a href="..." target="_new">...</a>? Cheers, Paulo Matos
2
2835
by: Matt | last post by:
In the following code, page1.asp is inside the frame of main.html. When the user click submit button in page1.asp, it will submit the form and open a new window called page2.asp. When the user clicks submit button on page2.asp, I expected to open a new window called page3.asp. Unfortunately, it just open page3.asp in the same window as page2.asp, and now page2.asp is gone. However, if I just open page1.asp, and begins from there, it is...
0
1151
by: daechulsohn | last post by:
Just thought I would contribute some knowledge for a change. After hours tracking down a problem with obfuscated code, where we were getting the exception : System.TypeLoadException: Method a on type b from assembly is overriding a method impl. We traced it to a class that implements multiple interfaces, two of which defined the same method, with same name and same signature. That is,
0
5448
by: oopaevah | last post by:
Hi, I have a set of valid xhtml pages that a third party web site is integrating with via an <iframein his own page. This retains his own branding template in the header and footer, with the iframe containing my pages in the middle of the screen. When it is time to exit my pages, to take the user back to the third party web site, I need to direct the browser out of the iframe and to one of his pages. I leave it at that and his site...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8494
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7309
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.