-3

I may just be having a brain fart, upon doing the bubble sort algorithm, of course, I encountered a problem. Any help will be golden. The problem is to do with the "float numbers[n];" and the "std:string c;".

int n = 0;
cout << "Enter value of N: ";
cin >> n;

float numbers[n];

cout << "You will enter " << n << " numbers." << endl;
for (int i = 0; i < n; i++) {
std:string c;
    cin >> c;
    numbers[i] = atof(c.c_str());
}

1 Answers1

1

It is not exactly clear to me (or us) what your problem exactly is. But in any case, std:string has to be replaced by std::string. Double colon sign, indicating that string lies in the std namespace.

Besides, it is much more idiomatic, and more portable and less error-prone, if you replace the C-style array by a proper std::vector object.

This code compiles OK:

#include  <string>
#include  <iostream>
#include  <vector>

using  std::cout;
using  std::cin;
using  std::endl;


int main()
{
    int n = 0;
    cout << "Enter value of N: ";
    cin >> n;

    std::vector<float> numbers(n);

    cout << "You will enter " << n << " numbers." << endl;
    for (int i = 0; i < n; i++) {
        std::string c;
        cin >> c;
        numbers[i] = stof(c);
    }

    // etc...

    return EXIT_SUCCESS;
}
jpmarinier
  • 4,427
  • 1
  • 10
  • 23
  • 3
    Still using a VLA, which is a big no-no. Either a vector or dynamically allocated array (ew) is needed here. And why convert the `std::string` to a C-string to use `atof()` when `stof()`exists in ``? Reference on VLAs in C++: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – sweenish Oct 04 '19 at 19:05
  • I get you say it in words, but you shouldn't have it in the code you're providing as a solution. – sweenish Oct 04 '19 at 19:09
  • *This code compiles OK:* -- [No it doesn't](https://rextester.com/XNEY33323) – PaulMcKenzie Oct 04 '19 at 19:13
  • @Xander Powel VLA stands for Variable Length Array. The size of your array is not known at compile time. This is accepted by some compilers, primarily for backward compatibility reasons. Absolutely no reason to have VLAs in newly written code. – jpmarinier Oct 04 '19 at 19:14
  • @PaulMcKenzie it does compile with GCC v8 and default options. It even works (shame !). I agree this is not even an half-good excuse for not using an std::vector object instead. – jpmarinier Oct 04 '19 at 19:16
  • @jpmarinier Simply adding a comment to say that the code you've provided is not ideal doesn't really suffice. I've submitted an edit making the changes. – sweenish Oct 04 '19 at 19:19
  • 3
    The problem with posting answers like this using VLA's is that there are lot (maybe most) of the persons looking for answers are using Visual C++, not g++. If they now copied and pasted your answer into their compiler, they have a non-working solution. Might as well post what actually works -- it is just a few more keystrokes. – PaulMcKenzie Oct 04 '19 at 19:23
  • Besides, once a vector is used, there is no need to force the user to enter N/n beforehand. Counting numbers is the job of the computer. You just need a convention that en empty line means the end of the number list.
    But a drawback of all this is that a lot of improvements gets presented simultaneously to the OP, without a proper graduation. We could also write a Shell-Metzner sort algorithm, as this is much faster than Bubble Sort while we are at it.
    – jpmarinier Oct 04 '19 at 19:31
  • @jpmarinier Or use `std::sort()`, but I get where you're coming from. There could be an argument for still asking for a size, as the code reserves the heap space up front, which is more efficient than knowingly allowing the vector to reallocate x times. OP is obviously asking for help on a homework, but things like avoiding VLA and avoiding needless type conversions can all come together. They knew about `atof` and `std::string`, adding that `` can do anything `` can do is not an overload. My thing was that your code should reflect what the text was already saying. – sweenish Oct 04 '19 at 20:31