-
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
-
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.
-
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.
-
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?
-
>> both 'setprecision' and std::setprecision' don't work
setprecision was fine, you just forgot the header file that declares it.
-
-
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
-
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.
-
>> 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.
-
Yep it now works fine, just a few formatting errors with tabs to sort out, thanks once again people, this forum rocks!
-
post it - the line with variable declaration
and line containing error
and error message as it is
-
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;
}
-
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 :)
-
That sounds pretty cool, can you give me any links at all?
-
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.