Thread: problem returning array from function(among others)

  1. #16
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    ok here is my new source code:

    Code:
    #include<iostream>
    #include<string>
    #include<cmath>
    
    using namespace std;
    
    float transferTime ( int hddSpeeds[], float sizeOfDir );
    
    int main()
    {
    	string hddNames[5];
    	float transferRate[5];
    	int hddSpeeds[5];
    	float sizeOfDir;
    	float counter = 0;
    	int fastest;
    	for ( int loop = 0; loop < 5; loop++ )
    	{//open loop
    		cout<<"Please enter a harddrive name to be compared: ";
    		cin>> hddNames[loop];
    		cout<<"Please enter the transfer speed of that drive in megabits per second: ";
    		cin>> hddSpeeds[loop];
    		cout<<"Please enter the size of the directory being transferred in Gigabytes: ";
    		cin>> sizeOfDir;
    	}//end loop
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		int dummy = hddSpeeds[loop];
    		transferRate[loop] = sizeOfDir * 1024 * 8/dummy;
    	}//close loop
    	for ( int loop = 0; loop < 5; loop++ )
    	{//open loop
    		if ( counter < transferRate[loop] )
    		{//open if
    			 fastest = loop;
    		}//close if
    	}//close loop
    	float topspeed;
    	topspeed = transferRate[fastest];
    	cout<<"Hard Drive\tTransfer rate Mbps\tDirectory Size\tTransfer time\n";
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		cout<< hddName[loop] << "\t";
    		cout<< hddSpeeds[loop] << "\t";
    		cout<< sizeOfDir << "\t";
    		cout<< transferRate[loop] << "\n";
    	}//close loop
    	cout<<"\n";
    	cout<<"The fastest of the given Hard Drives was: ";
    	cout<<"Hard Drive\tTransfer rate Mbps\tDirectory Size\tTransfer time\n";
    	cout<< hddName[fastest] << "\t";
    	cout<< hddSpeeds[fastest] << "\t"; 
    	cout<< sizeOfDir << "\t"; 
    	cout<< std::fixed << setprecision(1) << topspeed << "\n";
    }
    and the errors I am getting have reduced:

    Code:
    hddnofunc.cpp: In function ‘int main()’:
    hddnofunc.cpp:43: error: ‘hddName’ was not declared in this scope
    hddnofunc.cpp:51: error: ‘hddName’ was not declared in this scope
    hddnofunc.cpp:54: error: ‘setprecision’ was not declared in this scope

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you are calling a function and passing an array to it, you just put the name of the array without the brackets.

    You already passed hddSpeeds as an array to the function. You would do transferRate exactly the same way as you do hddSpeeds (assuming you had hddSpeeds working correctly).

    >> I might just leave the idea of a function
    If your hope is to just get it working, ok, but otherwise using a function is good practice and much closer to better programming techniques.

    >> error: ‘setprecision’ was not declared in this scope
    std::setprecision was fine (although not needed because of the using namespace std directive), you just forgot the header file that declares it.

  3. #18
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    transferTime( hddSpeeds[], sizeOfDir, transferRate[] );
    when calling the function you dont use the '[]' if you use these then the function expects an array index, such as: hddSpeeds[2], but you want to pass the whole array, so dont use []s when calling.

    i think you should change the function to:
    Code:
    void transferTime ( int hddSpeeds[], float sizeOfDir, float transferRate[])
    {
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		int dummy = hddSpeeds[loop];
    		transferRate[loop] = sizeOfDir * 1024 * 8/dummy;
    	}//close loop
    	return;
    }
    any changes made to the hddSpeeds and transferRate arrays will also be made to the variables in main, where it was called. dont forget to change the funciton prototype (at the top) to reflect the change made here.

  4. #19
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    OK I have corrected those errors with hddnames, stupid human mistakes :P

    I only have this error:

    Code:
    hddnofunc.cpp:54: error: ‘setprecision’ was not declared in this scope
    both 'setprecision' and std::setprecision' don't work, does anyone know any other ways to round to one decimal place?

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> both 'setprecision' and std::setprecision' don't work
    setprecision was fine, you just forgot the header file that declares it.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243

  7. #22
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    ok, I'll keep the function, here is the new updated source code and errors:

    Code:
    #include<iostream>
    #include<string>
    #include<cmath>
    
    using namespace std;
    
    void transferTime ( int hddSpeeds[], float sizeOfDir, float transferRate[] );
    
    int main()
    {
    	string hddNames[5];
    	float transferRate[5];
    	int hddSpeeds[5];
    	float sizeOfDir;
    	float counter = 0;
    	int fastest;
    	for ( int loop = 0; loop < 5; loop++ )
    	{//open loop
    		cout<<"Please enter a harddrive name to be compared: ";
    		cin>> hddNames[loop];
    		cout<<"Please enter the transfer speed of that drive in megabits per second: ";
    		cin>> hddSpeeds[loop];
    		cout<<"Please enter the size of the directory being transferred in Gigabytes: ";
    		cin>> sizeOfDir;
    	}//end loop
    	transferTime( hddSpeeds, sizeOfDir, transferRate );
    	for ( int loop = 0; loop < 5; loop++ )
    	{//open loop
    		if ( counter < transferRate[loop] )
    		{//open if
    			 fastest = loop;
    		}//close if
    	}//close loop
    	float topspeed;
    	topspeed = transferRate[fastest];
    	cout<<"Hard Drive\tTransfer rate Mbps\tDirectory Size\tTransfer time\n";
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		cout<< hddName[loop] << "\t";
    		cout<< hddSpeeds[loop] << "\t";
    		cout<< sizeOfDir << "\t";
    		cout<< transferRate[loop] << "\n";
    	}//close loop
    	cout<<"\n";
    	cout<<"The fastest of the given Hard Drives was: ";
    	cout<<"Hard Drive\tTransfer rate Mbps\tDirectory Size\tTransfer time\n";
    	cout<< hddName[fastest] << "\t";
    	cout<< hddSpeeds[fastest] << "\t"; 
    	cout<< sizeOfDir << "\t"; 
    	cout<< std::fixed << setprecision(1) << topspeed << "\n";
    }
    
    void transferTime ( int hddSpeeds[], float sizeOfDir, float transferRate[] )
    {
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		int dummy = hddSpeeds[loop];
    		transferRate[loop] = sizeOfDir * 1024 * 8/dummy;
    	}//close loop
    	return;
    }
    Code:
    hdd.cpp: In function ‘int main()’:
    hdd.cpp:39: error: ‘hddName’ was not declared in this scope
    hdd.cpp:47: error: ‘hddName’ was not declared in this scope
    hdd.cpp:50: error: ‘setprecision’ was not declared in this scope

  8. #23
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    ok fixed the setprecision problem, thanks a lot. Those pesky hddNames stuff still remains, even though I'm sure I've typed them correctly.

    Sorry for the waste of a post, fixed everything, It's compiled! I'll go and do a practice run.

    Thanks for all of your help, I really appreciate it guys and gals.
    Last edited by Calef13; 10-30-2006 at 02:19 PM.

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm sure I've typed them correctly.
    The error gives you a line number, so check that exact line and make sure you are sure.

  10. #25
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Yep it now works fine, just a few formatting errors with tabs to sort out, thanks once again people, this forum rocks!

  11. #26
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    post it - the line with variable declaration
    and line containing error

    and error message as it is
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #27
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    ok here is a sample run:

    Code:
    Please enter a harddrive name to be compared: maxtor
    Please enter the transfer speed of that drive in megabits per second: 100
    Please enter a harddrive name to be compared: western
    Please enter the transfer speed of that drive in megabits per second: 150
    Please enter a harddrive name to be compared: ibm
    Please enter the transfer speed of that drive in megabits per second: 250
    Please enter a harddrive name to be compared: samsung
    Please enter the transfer speed of that drive in megabits per second: 300
    Please enter a harddrive name to be compared: samsung
    Please enter the transfer speed of that drive in megabits per second: 130
    Please enter the size of the directory being transferred in Gigabytes: 5.0
    Hard Drive      Transfer rate Mbps      Directory Size  Transfer time
    maxtor          100             5               315.1
    western         150             5.0             315.1
    ibm             250             5.0             315.1
    samsung         300             5.0             315.1
    samsung         130             5.0             315.1
    
    The fastest of the given Hard Drives was: Hard Drive    Transfer rate Mbps     Directory Size   Transfer time
    samsung 130     5.0     315.1
    and here is the relevant source code:

    Code:
    #include<iostream>
    #include<string>
    #include<cmath>
    #include <iomanip>
    
    using namespace std;
    
    void transferTime ( int hddSpeeds[], float sizeOfDir, float transferRate[] );
    
    int main()
    {
    	string hddNames[5];
    	float transferRate[5];
    	int hddSpeeds[5];
    	float sizeOfDir;
    	float counter = 0;
    	int fastest;
    	for ( int loop = 0; loop < 5; loop++ )
    	{//open loop
    		cout<<"Please enter a harddrive name to be compared: ";
    		cin>> hddNames[loop];
    		cout<<"Please enter the transfer speed of that drive in megabits per second: ";
    		cin>> hddSpeeds[loop];
    	}//end loop
    	cout<<"Please enter the size of the directory being transferred in Gigabytes: ";
    	cin>> sizeOfDir;
    	transferTime( hddSpeeds, sizeOfDir, transferRate );
    	for ( int loop = 0; loop < 5; loop++ )
    	{//open loop
    		if ( counter < transferRate[loop] )
    		{//open if
    			 fastest = loop;
    		}//close if
    	}//close loop
    	float topspeed;
    	topspeed = transferRate[fastest];
    	cout<<"Hard Drive\tTransfer rate Mbps\tDirectory Size\tTransfer time\n";
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		cout<< hddNames[loop] << "\t\t";
    		cout<< hddSpeeds[loop] << "\t\t";
    		cout<< sizeOfDir << "\t\t";
    		cout<< std::fixed << setprecision(1) << topspeed << "\n";
    	}//close loop
    	cout<<"\n";
    	cout<<"The fastest of the given Hard Drives was: \n";
    	cout<<"Hard Drive\tTransfer rate Mbps\tDirectory Size\tTransfer time\n";
    	cout<< hddNames[fastest] << "\t";
    	cout<< hddSpeeds[fastest] << "\t"; 
    	cout<< sizeOfDir << "\t"; 
    	cout<< std::fixed << setprecision(1) << topspeed << "\n";
    }
    
    void transferTime ( int hddSpeeds[], float sizeOfDir, float transferRate[] )
    {
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		int dummy = hddSpeeds[loop];
    		transferRate[loop] = sizeOfDir * 1024 * 8/dummy;
    	}//close loop
    	return;
    }

  13. #28
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    looks like everything is working good job
    maybe fix up the 2nd last line of output so it looks more like a chart like you were meaning.

    if you want to continue on this, maybe take in a command-line argument specifying the number of harddrives to read in, rather than the constant of 5? maybe i dont have 5 drives to compare. you could check the arguments and if there arent any then use the default of 5 (or whatever you decide).

    ie, if i ran this program from a console as: hdd 3
    it would only ask for 3, etc, etc.

    if your up for another challenge

  14. #29
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    That sounds pretty cool, can you give me any links at all?

  15. #30
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    heres a quick example, you should be able to figure it out.
    Code:
    #include<iostream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	for (int i = 0; i < argc; i++)
    		cout << argv[i] << endl;
    }
    compile and run it from your command line, specifying a number of arguments, serperated by spaces.

    argc = number of parameters passed
    argv = array of character arrays (c-strings) containing the parameters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  2. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  3. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM