Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a float from a char* in C++?
    text
    copied!<p>I am relatively new to C++, coming from the happy-go-lucky world of Python.</p> <p>I decided to write a set of functions to find the mean, median, mode, and range of a set of numbers. To aid in the calculation of the median, I decided to make l2g (least to greatest) and g2l (greatest to least). I understand I will only be using one of these, but want to make both just for the sake of completeness.</p> <p>I want my program to rely on as few external (even standard) libraries as possible. The reason for this is that I will most likely be using any functions that I write again. They will be used in my OS that I am developing based off of the tutorials at BrokenThorn. Therefore I want to keep the code as independent as possible, using functions that I write instead of functions in the standard libraries.</p> <p>I have two problems:</p> <ul> <li>The first is that I want my functions to be able to handle float values. With the user input function that I created earlier, I get a pointer to a char array. I need to know how to convert that into a float value. I was thinking that maybe I could create a union with two members, of char and float types, and just reference the float member when I need to use the value. I would just put user data into the char member. However, I don't know if this approach would work. The tutorial that I use doesn't go very in-depth about defining my own data types.</li> <li>My second dilemma is that I do not know how to create the l2g and g2l functions. I was planning on nesting two for loops. I was going to use the inside one to find the next number in the new array, and the outside one to repeat this process until it gets through all the numbers in the original array. Then the function would return a pointer to the new array of floats, after deleting the first array's dynamic memory.</li> </ul> <p>Another issue is that I think I need to return the length of an array when I return a pointer to it. The reason for this is that to use a for loop to go through the contents of that array, I need to have the highest position usable in the array, so I don't get garbage. I have a way to avoid this problem by making a struct that has two members, length and the array. It looks like this:</p> <pre><code>struct arrayLength { int l; float * p; } ; </code></pre> <p>I was hoping that the following set of calls:</p> <pre><code>float * myArrayPointer; // I will change this to user input once I get the rest of the program working myArrayPointer = new float[5]; myArrayPointer[0] = 65.97; myArrayPointer[1] = 21.06; myArrayPointer[2] = 21.06; myArrayPointer[3] = 509.69; myArrayPointer[4] = -41.73; // Can floats be negative? cout &lt;&lt; mean(myArrayPointer, 5); // Give function mean the pointer to array and number of elements cout &lt;&lt; "\n"; cout &lt;&lt; median(myArrayPointer, 5); cout &lt;&lt; "\n"; cout &lt;&lt; range(myArrayPointer, 5); cout &lt;&lt; "\n"; cout &lt;&lt; mode(myArrayPointer, 5); cout &lt;&lt; "\n\n\n"; arrayLength myArrayPointerLG = l2g(myArrayPointer, 5); float * myArrayLGitem; for(int i=0;i&lt;myArrayPointerLG.l;i++) { myArrayLGitem = myArrayPointerLG.p[i]; cout &lt;&lt; myArrayPointerLGitem &lt;&lt; "\n"; } </code></pre> <p>Would give the following output:</p> <pre><code>115.21 21.06 551.42 21.06 -41.73 21.06 21.06 65.97 509.69 </code></pre> <p>So far, I haven't gotten my code compiled. :/ I think maybe a for loop has its own scope... Could someone verify this as well? Here's a link to the code on pastebin:</p> <blockquote> <p><a href="http://pastebin.com/eisHt98A" rel="nofollow noreferrer">Pastebin</a></p> </blockquote> <p>And in case you were wondering about the <code>&lt;iostream&gt;</code> inclusion... I will remove this and use the printing functions I wrote if I end up adding it to my OS. OR I might just define std::cout. The errors that I'm getting are saying that currentPosition and listOfNumbers are undeclared (not defined) in l2g. I am running Windows 7 64-bit, using the MSVC++ 2008 Express Edition compiler. Thank you in advance to anyone who helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload