L.W
L.W

Reputation: 11

How to allow user to input array index along with the values of the array?

#include <iostream>
#include <algorithm>
using namespace std;

int main(){

string m;
int n;

cout << "How many elements would you like to sort?" << endl;

cin >> n;  //index for the array



cout << "Enter " << n << " numbers seperated by a comma to be sorted" <<   endl;

cin >> m;

string unsortedNumbers[n]={m};  // values inputed stored as array values
cout << m << endl;

std::sort(m.begin() , m.end());  //sorting string m
std::cout<< m << endl;

}

I understand that the code is wrong, if you run the code the 'string' works properly until sorting. When sorting numbers above 10 it breaks them down as 1 and 0. Also all the commas get sorted and say your input was 2,3,4,5 your output will look something like ,,,,2345. So my question is how can I let the user select the index size while then allowing whatever number they input to determine the number of variables to then be sorted?

Upvotes: 1

Views: 2070

Answers (3)

David Pulse
David Pulse

Reputation: 90

FunctionOne(int &arr[]) {}

void main() {
    int n;
    cout << "How many elements would you like to sort?" << endl;

    cin >> n;  //index for the array



    cout << "Enter " << n << " numbers. Separate by <enter> to be sorted" << endl;
#PRAGMA NUMBER
    int arr[n];
    for (int m:arr)
        cin >> m;
    FunctionOne(&arr);
}

So you don't have what we call spaghetti code, you can put sort in a function. It's good practice.

Upvotes: 0

MORTAL
MORTAL

Reputation: 383

the simplest way to let the user enter the size of array and then sort and print the result on screen is by using STD approach like this

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::cout << "How many elements would you like to sort?" << std::endl;
    std::size_t n;
    std::cin >> n;  //index for the array

    std::cout << "Enter " << n << " numbers seperated by a comma to be sorted" << std::endl;

    std::vector<char> vec;
    vec.reserve(n + n - 1); // array size + commas

    // get array values from user
    std::copy_n(std::istream_iterator<char>(std::cin), vec.capacity(), std::back_inserter(vec));

    // remove commas
    vec.erase(std::remove(vec.begin(), vec.end(), ','), vec.end());

    // sort array
    std::sort(vec.begin(), vec.end());

    // print it 
    std::copy(vec.begin(), vec.end(), std::ostream_iterator<char>(std::cout, " "));
    // input: 2, 3, 1
    // output: 1 2 3
}

Upvotes: 1

龚成玥
龚成玥

Reputation: 111

I can only do this in a quiet fussy way since I turn string to char* and finally turn char* to string again. In my view,The string classes of the C++ standard library enable you to use strings as normal types that cause no problems for the user. Thus, you can copy, assign, and compare strings as fundamental types without worrying or bothering about whether there is enough memory or for how long the internal memory is valid. However,that also means we can only use interfaces and can't do whatever we want to do.You know,string based on the basic template class basic_string<> and looks like container more than char*.

#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    string m;
    int n;
    cout << "How many elements would you like to sort?" << endl;
    cin >> n;  //index for the array
    cout << "Enter " << n << " numbers seperated by a comma to be sorted" <<   endl;
    cin >> m;


    //string c[n]={m};
    vector<int> unsortedNumbers;
    const char* p=m.c_str();
    int i=0;
    while(i<n){//or *p!='\0'
        i++;
        unsortedNumbers.push_back(atoi(p));
        while(*p>='0'&&*p<='9') p++;
        if(i<n)p++;
    }
    //std::sort(m.begin() , m.end());  //sorting string m
    sort(unsortedNumbers.begin(), unsortedNumbers.end());
    for_each(unsortedNumbers.begin(), unsortedNumbers.end(), [](int x){cout<<x<<" ";});
    //now we have a sorted array and its type is double[]
    char arr[n][m.length()];
    for(int i=0;i!=n;i++) {
        if(i!=n-1) sprintf(arr[i],"%d,",unsortedNumbers[i]);
        else sprintf(arr[i],"%d",unsortedNumbers[i]);
    }
    m.clear();
    for(auto i:arr)  m+=i;
    cout<<m;
}

Upvotes: 2

Related Questions