CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2013

    Getting the longest word from an Array

    I'm new, and a student who is oddly enough a little stumped. Not being doing C++ for long so I probably have a lot of Bad Code and it probably looks ugly to anyone who has been doing it for a few months. I've noticed around here that using namespace std etc isn't exactly good practice but if I take a code back to my tutor without it I'll get sent back to change it and put it back in. So just to explain why I'm using it, I'm keeping a note in my head that outside of my classroom it isn't a good idea. So now I'm done apologising for this, I'll get to why I've been up till 3am.

    For the life of me, I can't work out how to call the plant with the longest Latin name. I've been at it for a few hours.

    As I'm a student, and I'm very much pointing this out so no one accuses me down the line of getting other people to do my homework! I'm not expecting someone to turn up and give me the exact code to fix my problem. Being pointed in the right direct or given a bit of code I could work with would be awesome.

    Thank you for taking time out to read this, and double thanks for any help!

    // Session6_4.cpp : Defines the entry point for the console application.
    //For you to do...
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip> 
    #include <string>
    using namespace std;
    const int MAXPLANTS =6; //Always use this instead of magic numbers in code!
    struct Plants
    	string CommonName;
    	string LatinName;
    	double Height;
    	double Price;
    int main()
    	Plants ListPlants[MAXPLANTS] = 
    								{"Bluebell",				"Endymion Non Scripta",			0.3,	1.25},
    								{"Snowdrop",				"Galanthus Nivalis",			0.15,	1.55},
    								{"Tenby Daffodil",			"Narcissus Obvalis",			0.4,	3.25},
    								{"Winter Aconite",			"Eranthis Hyemalis",			0.1,	2.0},
    								{"Dogs Tooth Violet",		"Erythronium Dens Canis",		0.15,	5.95},
    								{"Summer Snowflake",		"Leucojum Aestivum",			0.3,	4.50}
    	int IndexMostExpensive;
    	double PriceMostExpensive;
    	PriceMostExpensive = ListPlants[IndexMostExpensive].Price; 
    	///Header Display///
    	cout<<setfill(' ')<<setw(20)<<"Common Name"<<(' ')<<setw(20)<<"Latin Name"<<(' ')<<setw(20)<<"Height"<<(' ')<<setw(10)<<"Price"<<endl<<endl;
    	for (int i=0;   i<MAXPLANTS;    i++)
    		cout<<setfill(' ')<<setw(20)<<ListPlants[i].CommonName;
    		cout<<setfill(' ')<<setw(30)<<ListPlants[i].LatinName;
    		cout<<setfill(' ')<<setw(10)<<ListPlants[i].Height;
    		cout<<setfill(' ')<<setw(10)<<ListPlants[i].Price;
    			if (ListPlants[i].Price>PriceMostExpensive)
    			IndexMostExpensive =i;			
    			PriceMostExpensive = ListPlants[i].Price; 
    	cout<<endl<<"Most expensive Plant is "<<ListPlants[IndexMostExpensive].CommonName<<" at a price of "<<ListPlants[IndexMostExpensive].Price<<endl;
    	cout<<"The Latin name being "<<ListPlants[IndexMostExpensive].LatinName<<" and a height of "<<ListPlants[IndexMostExpensive].Height<<endl;
    	return 0;

  2. #2
    Join Date
    Apr 1999

    Re: Getting the longest word from an Array

    Write a simple function that takes an array of Plants and returns the index of the longest name. All of that other stuff with output is not important, as it just clutters up everything. Not only that, you end up wasting time with writing unimportant I/O, when the real goal is never achieved (coming up with the longest name). Save your keystrokes for important work, not pretty input/output.

    First, the name "Plants" is misleading, as the struct holds information for just one plant.
    #include <string>
    struct Plant
    	std::string CommonName;
    	std::string LatinName;
    	double Height;
    	double Price;
    // assumes that there is at least 1 plant in the array
    int GetLongestName(Plant* plantArray, int numPlants)
        int longestIndex = plantArray[0].CommonName.size();
        for ( int i = 1; i < numPlants; ++i )
              // plantArray[i] name with the longest name
         return longestIndex;
    Plant allMyPlants[4];
    int main()
        // assume allMyPlants has info
        int testValue = GetLongestName(allMyPlants, 4);
    That is what you should start with. You write a function, test it with data that you can hard-code into the array. Then test the function to see if it works. I left the logic out of finding the longest name, since that's your job to complete it.


    Paul McKenzie
    Last edited by Paul McKenzie; February 15th, 2013 at 04:16 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

By using this site, you agree to the Privacy Policy