C++ program

I am trying to make a piece of code that calculates the actual size of a hard drive storage for example if a person types 40 the program should display 37.25. I've got to the point where I ask the user the size of hard drive he/she wants I just can't figure out how to do that math that will get me to that.

#include <iostream>
using namespace std;

int main()

{
double size;
cout << " Enter the size of the Hard drive you would like:" << endl;
cin >> size;


return 0;

}
[/code]
Last edited on
I am trying to make a piece of code that calculates the actual size of a hard drive storage

What do you think is the "actual size"?
Are you simply looking to convert gigabytes to gibibytes?

Edit:
That seems like the only reasonable option:

1
2
3
4
5
# include <iostream>
int main() {
  constexpr double gib_per_gb = (1 << 30) / 1.e9; 
  std::cout << 40 / gib_per_gb;
}
Last edited on
I'm trying to stay with gigabytes like for example you purchase a 40gb hard drive but in reality that 40gb hard drive is 37.25GB not 40.
No, it's not. The size of a 40GB drive is 40 billion bytes, by definition and as advertised.

There is a common misconception regarding the definition of these units.

SI defines a set of uniform prefixes which can be applied to units. The most relevant ones here are "kilo-", "mega-", "giga-", and "tera-", but there are enough prefixes that the system is sufficient to quantify most natural phenomena.

By definition, "kilo-" means 1000. A "kilobyte" is exactly one thousand bytes; a "kilometer" is exactly one thousand meters. Further, "mega-" means 1000000 (or 1e6); a megabyte is exactly one million bytes; a megavolt is exactly one million volts. In a unit abbreviation, the lower-case letter 'k' means "kilo-", and the upper case M and G means "mega-" and "giga-" respecitvely.

There is also a system of binary prefixes that make natural sense for computers. In this system, we use prefixes that name 2^10, 2^20, 2^30, and so on. Those prefixes are named "kibi-", "mebi-", and "gibi-" (...and "tebi-", etc.).

It also turns out that the quantity 2^10 = 1024 is only 2.4% off from 1000, so colloquially this power of 2 is sometimes (incorrectly) referred to using the prefix "kilo". The most correct abbreviation is "kibi". Similar inaccuracies occur when referring to megabytes where mebibytes are correct, and gigabytes where gibibytes are correct.

When you buy a drive at 40GB with the misconception that the size is actually 40GiB, you're actually getting about 3 fewer gigabytes than expected, but the sizes are as advertised (in the U.S., at least).

(A similar deception occurs when purchasing internet service where I live. Rates are advertised in mebibits per second - that is, in Mib/s: people who don't know any better might be surprised to find speeds 8 times slower than expected!)
Last edited on
Different MFG's use different amounts of disk space for different things.
Some of your drive has sectors set aside for bad blocks.
The drive really has more space than advertised but may not be mapped as usable.
Basically I'm saying there is more space on a disk than what your buying.
Drive mfg's use decimal to calculate disk space, Windows on the other hand measures using binary and thus reports a different size.

http://knowledge.seagate.com/articles/en_US/FAQ/172191en

Yes your right. and that's what I'm trying to make a program that is above to be able to do that.
Yes your right. and that's what I'm trying to make a program that is above to be able to do that.


In other words, you're looking to convert from GB to GiB. Or do I not understand the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# include <iostream>
# include <string>
# include <sstream>

void usage(char** argv) { 
    std::cout << "usage: *argv size\n\t"
              << "where size is a number in units of gigabytes\n"; 
}

bool to_double(std::string s, double& d) {
    std::istringstream str{s}; 
    return !(str >> d).fail();
}

int main(int argc, char** argv) {
  constexpr double gib_per_gb = 1.e9 / (1 << 30);
  
  double d;
  if (argc == 2 && to_double(argv[1], d)) {
    std::cout << d * gib_per_gb << '\n';
    return 0;
  } else {
    usage(argv);
    return 1;
  }
}

http://coliru.stacked-crooked.com/a/79d504c87e93b092
Last edited on
mbozzi, I don't think that is what he is talking about. The MiB, GiB, etc prefixes are recent. There is certainly a push to use them now, but until recently MB, GB, etc were used incorrectly.

Available size on a drive is usually smaller than the drives reported size even within the OS (though the newer OSs seem to not show all that info), of which I suspect, some of the space is used for file indexing etc before being set aside as usable space, thus leading to an empty drive with less usable space than reported. The issue with MB vs MiB, I suspect simply makes it more confusing as more different numbers get thrown around.

Though I admit this is suspicion on my part, I find it weird for windows to report a drive as size A (which is different from packaging), then report size B empty space when nothing is on the drive yet.
Last edited on
I don't think that is what he is talking about.

The reason I suspect this is because 40 GB is 37.2529 GiB, and OP wrote:
If a person types 40 the program should display 37.25

I'm not certain how to query the kernel for this information (or if that's even the right approach), so if you're right I can't help, at least not until I do more research.

I expect that the available size of a volume depends on such things as file-system type, which is easy to obtain, but if @SamuelAdams is correct and this matters to the OP, the absolute physical storage size of the drive will differ with the design of the hardware.

I find it weird for windows to report a drive as size A (which is different from packaging), then report size B empty space when nothing is on the drive yet.

Empty space is smaller than the drive size at least because of the space required for file-system's data structures.
Last edited on
The program I'm trying to make is to ask the user what size hard drive he wants only in GB and after They enter the amount the program prints out the accurate storage size in a hard drive in GB
As has been pointed, there is GB vs GiB, which is constant, but there is also the file system, partitioning (a boot sector might have been added or similar as well), unusable sectors, etc, and these are not constant and you'd have to find that info first then account for it.

For just the GB to GiB part though, you simply multiply by 1000^3 then divide by 1024^3. Reverse to convert the other way. The still leaves the file system and other stuff though.



Topic archived. No new replies allowed.