problem returning array from function(among others)

This is a discussion on problem returning array from function(among others) within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a program to take in the names of 5 hard drives and their transfer speeds ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    problem returning array from function(among others)

    Hi, I'm trying to write a program to take in the names of 5 hard drives and their transfer speeds as well as the size of a folder in gigabytes to be transferred at the speed of that drive. It should then store the time taken in an array after calculating it. Then finally print out all of the drives names, speeds, the size of the folder, and the time taken for the folder to be transferred, all in columns using tabs. Then do the the same again but only list the fastest drive. Finally, the time taken should be rounded to 1 decimal place.

    Before anyone asks, this is an exercise I am doing in school in truebasic, but I like to do them all in c++ so I can learn that too because c++ rocks.

    Now, as you can see below, I have made a function to carry out the calculation and return an array with the transfer times in it. However, g++ keeps throwing up this error that my array is not defined so I wonder if I am returning it properly.
    Code:
    #include<iostream>
    #include<string>
    #include<cmath>
    
    using namespace std;
    
    float transferTime ( int hddSpeeds[], float sizeOfDir );
    
    int main()
    {
    	string hddNames[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
    	float transferTime( long hddSpeeds[], float sizeOfDir );
    	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" << hddSpeeds[loop]"\t" << sizeOfDir"\t" << transferRate[loop]"\t\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" << hddSpeeds[fastest]"\t" << sizeOfDir"\t" << std::fixed << std::setprecision(1) << topspeed <<;
    }
    
    float transferTime ( int hddSpeeds[], float sizeOfDir )
    {
    	float transferRate[5];
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		int dummy = hddSpeeds[loop];
    		transferRate[loop] = sizeOfDir * 1024 * 8/dummy;
    	}//close loop
    	return transferRate[];
    }
    Here is the output from g++ just in case anyone is interested:

    Code:
    hdd.cpp: In function ‘int main()’:
    hdd.cpp:28: error: ‘transferRate’ was not declared in this scope
    hdd.cpp:34: error: ‘transferRate’ was not declared in this scope
    hdd.cpp:38: error: ‘hddname’ was not declared in this scope
    hdd.cpp:38: error: expected `;' before string constant
    hdd.cpp:43: error: ‘hddname’ was not declared in this scope
    hdd.cpp:43: error: expected `;' before string constant
    hdd.cpp: In function ‘float transferTime(int*, float)’:
    hdd.cpp:54: error: expected primary-expression before ‘]’ token
    The last line of the error output is the one which really has me confused. Should I be doing something at the start of the function?

    Thanks in advance,

    Calef13

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You cannot return an array from a function. The easiest solution would be to declare the array in main, and then pass it to the function where it is updated.

    Note that you also need to fix the place where you attempt to call the function, you are declaring another function instead of calling it.

    If you really want to learn C++, consider learning to use vector for this instead of a C style array.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Thanks, I was told I could return an array from a function. I am still learning so thanks for the tip about vector arrays. Can anyone tell me how I would got about printing a string array with cout?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> I was told I could return an array from a function.
    You can return a pointer that points to an array, but that leads to issues of memory management and deciding who owns the memory and when and how it gets deleted.

    >> how I would got about printing a string array with cout
    There are more advanced ways, but a simple loop outputting each value would work. It's the same as with an array of floats.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you would print it the same way you print other strings.

    'hddNames' is not a string but an array of strings (with elements 0-4 in your case). 'hddNames[0]' IS a string. if you can get that one printed, you should be able to figure out how to get the others in the array to be displayed.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Yeah I have printed out arrays using loops before, but this won't work for some reason.

    The compiler doesn't like this:

    Code:
    	for (int loop = 0; loop < 5; loop++ )
    	{//open loop
    		cout<< hddname[loop]"\t" << hddSpeeds[loop]"\t" << sizeOfDir"\t" << transferRate[loop]"\t\n";
    	}//close loop
    I am getting these errors:

    Code:
    hdd.cpp:39: error: ‘hddname’ was not declared in this scope
    hdd.cpp:39: error: expected `;' before string constant
    hdd.cpp:44: error: ‘hddname’ was not declared in this scope
    hdd.cpp:44: error: expected `;' before string constant
    I have declared this array, I don't understand why this isn't working.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    hdd.cpp:39: error: ‘hddname’ was not declared in this scope
    the compilers not lying, you dont have a variable named _EXACTLY_ 'hddname'... what must you do?

    hdd.cpp:39: error: expected `;' before string constant
    when you concatenate (join) strings with cout use a format like this, not like how you have: cout << "hello" << "\t";

    hope that helps

  8. #8
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Sorry I don't understand, what kind of format should I use. Something like this:

    cout<< hddNames[]\t;
    cout<< hddSpeeds[]\t;
    ...

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    anytime you have more than one variable or string together you need to seperate them with <<'s
    Code:
    cout<< hddname[loop] << "\t";
    etc

  10. #10
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Hm.... I understand the last part, however the compiler is still complaining that hddNames is undeclared, I had mistyped hddNames as hddnames ( this naming convention is new to me :P)
    What am I doing wrong?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    post the exact error message and updated code if necessary. you shouldnt get the error if you typed 'hddNames[#]'

  12. #12
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Yeah I have changed it a bit.

    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
    	transferTime( long hddSpeeds[], float sizeOfDir, float 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 << std::setprecision(1) << topspeed << "\n";
    }
    
    float 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 transferRate[];
    }
    Code:
    hdd.cpp: In function ‘int main()’:
    hdd.cpp:26: error: expected primary-expression before ‘long’
    hdd.cpp:26: error: expected primary-expression before ‘float’
    hdd.cpp:26: error: expected primary-expression before ‘float’
    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’ is not a member of ‘std’
    hdd.cpp: In function ‘float transferTime(int*, float, float)’:
    hdd.cpp:58: error: invalid types ‘float[int]’ for array subscript
    hdd.cpp:60: error: expected primary-expression before ‘]’ token
    I have forgotten how to call functions apparently

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
    transferTime( long hddSpeeds[], float sizeOfDir, float transferRate[] );
    you dont specify the datatypes (long, float, etc) when your using the function, only when the function is defined (such as at the top of your file with the function 'prototype' and at the bottom with your fuction 'implementation').

    hdd.cpp:39: error: ‘hddName’ was not declared in this scope
    hdd.cpp:47: error: ‘hddName’ was not declared in this scope
    are you sure you have variable called 'hddName'? triple check..

    hdd.cpp:50: error: ‘setprecision’ is not a member of ‘std’
    dont put 'std::' in front of setprecision.

    Code:
    float 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 transferRate[];
    }
    transferRate is a 'float' but you try and access it with [loop] as if it were an array. this function is said to return 'float' but you try and return a 'float[]'. which is i guess back to your initial problem of wanting to return an array from a function. there was a previous post in this thread regarding that, check it out. after you get other errors fixed we can work on that, if you dont solve it by reading a previous post.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    David said that I could declare my float array in main and then update it in the function, that is what I am trying to do, also this line:

    Code:
    transferTime( hddSpeeds[], sizeOfDir, transferRate[] );
    throws up the error:

    Code:
    hdd.cpp:26: error: expected primary-expression before ‘]’ token
    hdd.cpp:26: error: expected primary-expression before ‘]’ token

  15. #15
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    I get it now, I can't return an updated float array can I? Can I make a float array at all?

    I might just leave the idea of a function, it seems like it would make more sense to just do it all in int main().
    Last edited by Calef13; 10-30-2006 at 12:58 PM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-09-2006, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21