473,395 Members | 1,797 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,395 software developers and data experts.

Accelerated C++ "overriding commands for target"

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 2384
JosAH
11,448 Expert 8TB
I think you're overcomplicating 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
I think you're overcomplicating 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 Expert 8TB
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
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::setprecision;

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:family contains all family names read so far
while (cin >> x)
family.push_back(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.begin(), 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 Expert 8TB
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
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...
2
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...
12
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 :...
19
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...
6
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...
8
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
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...
0
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...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
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...

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.