Get array of integer in one line - C++

Hello,

How can I get an array of integers in one line? In the below code I should enter each integer in a new line.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<cmath>
using namespace std;



int main()
{
	int length;
	cin >> length;
	int* myArray = new int[length];

	for (int i = 0; i < length; i++)
	{
		cin >> myArray[i];  //1112321332
	}





	return 0;
}
separate the integers on input by a white-space char - space, tab or newline

1 1 1 2 3 2 1 3 3 2
Hello Shervan360,

I have been working with your program and come up with this. Just an example, but something to think about or keep for the future.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <limits>
//#include<cmath>  // <--- Not needed right now with the code that you have.

using namespace std;

int main()
{
    int length{};

    do
    {
        std::cout << "\n How many numbers would you like to use?: ";  // <--- Added.
        cin >> length;
        
        if (!length || !std::cin)
        {
            if (!std::cin)
            {
                std::cerr << "\n     Invalid Input! Must be a number\n";

                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
            }
            else if (!length)  // <--- Or (length < 1). You do not want a (0) or a negative number.
            {
                std::cerr << "\n     Invalid length! Must be greater than (0).\n";
            }
        }
    } while (!length);

    int* myArray = new int[length];

    std::cout << "\n Enter " << length << " numbers as ( 1 2 3 4 ...): ";  // <--- Added.

    for (int idx = 0; idx < length; idx++)
    {
        cin >> myArray[idx];  //1112321332
    }

    std::cout << "\n Numbers entered are: "; // <--- Used for testing. Comment or remove when finished.

    for (int idx = 0; idx < length; idx++)   // <--- Used for testing. Comment or remove when finished.
    {
        std::cout << myArray[idx] << ' ';
    }

    delete[] myArray;  // <--- Must have a delete when using "new".

    return 0;
}

Your use of blank lines is nice, but you have to many where they are not needed and not enough where they are needed. The first goal, first to your-self and then to others, is to make the code as easy to read as possible. Another advantage to this is that problems or errors tend to stick out and are easier to find.

Something I have a problem with is a "cin" without a prompt. I hate to stare at a blank screen wondering what to do and what should be entered. So unless you intend to provide the source code or written instructions on what to do and how to enter what is needed a good prompt helps.

Andy
#include <iostream>
#include <string>

using namespace std;

int main(){

string input;
string temp;
cin >> input;

int* myArray = new int[input.length()];

for(unsigned i =0; i < input.length(); i++){
myArray[i] = stoi(temp = input[i]);
}

for(unsigned i = 0;i < input.length(); i++)
cout << myArray[i]* 3 << " "; // <= just to prove you have an array of single digit ints'

delete[] myArray;
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <Windows.h> //<~not sure if this is needed or not.
#include <vector>
using namespace std;
int main() {

    string s{};
    cin>>s;

    vector<int> i{};

    for (auto m : s) {
        i.push_back(m - 48);
    }

    for (auto m : i) { //for testing
        cout << m;
    }
}


Last edited on
Topic archived. No new replies allowed.