How make to the put numbers randons in array in c++

I wonder how is to ask the user the random number of arguments. Because if u wanted eg only three numbers, so u put

int a, b, c;

cout << "";
cin >> a >> b >> c; // Put three variables, since only are 3 elements, but if n number of elements there would be an arrangement!

But I would like to n number of elements like this: [num 1] [num 2] [num n], see the examples of execution below;

Example run 1:

69 69 69 69 44 123

printing arrangement

69 69 69 69 44 123


Example run 2:

666 666 69 69 96 69 666 777 123

printing arrangement

666 666 69 69 96 69 666 777 123
How about reading as a string, and parsing it later to see how many integers there are?
Use a vector. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>

int main()
{
    const int sentinel = -999999 ;

    std::vector<int> numbers ;

    std::cout << "enter integers one by one, enter " << sentinel << " to end input:\n" ;
    int v ;
    while( std::cin >> v && v != sentinel ) numbers.push_back(v) ;

    // use numbers in the vector
}
I can't put vectors, i wanted put middling thereby:

#include <iostream>


using namespace std;

int main()
{
double x;

cout <<" ";
while( cin >> x ){
cout << x << endl;
}

cout << "\nEND" <<endl;

return 0;
}


but i don't know what criterion for stop the loop and appear of message "END", help me
Last edited on
Because if i puted of the number 69 how criterion for exit of loop

#include <iostream>

using namespace std;

int main()
{
double x;

cout <<" ";

while( cin >> x && x != 69 ){
cout << x << endl;
}

cout << "\nEND" <<endl;

return 0;
}

Exit of loop when the number 69 were entered, but i want change of criterion for when the key enter is entered
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    std::cout << "enter integers separated by spaces on a single line\n"
                 "  an enter immediately after the last integer ends the input:\n" ;

    int v ;
    // http://en.cppreference.com/w/cpp/io/basic_istream/peek
    while( std::cin >> v && std::cin.peek() != '\n' ) std::cout << v << ' ' ;

    std::cout << v << "\nEND\n" ;
}


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

int main()
{
    std::cout << "enter integers separated by spaces on a single line\n"
                 "  an enter ends the input:\n" ;

    std::string line ;
    // http://www.cplusplus.com/reference/string/string/getline/
    std::getline( std::cin, line ) ; // read an entire line into the string

    // http://en.cppreference.com/w/cpp/io/basic_istringstream
    std::istringstream stm(line) ; // construct an input string stream to read from the string

    int v ;
    // read in integers, one by one, from the string stream and print them out
    while( stm >> v ) std::cout << v << ' ' ;

    std::cout << "\nEND\n" ;
}
Thanks, but in firts code, when you use the cin.peek() don't print the last number, how in example:

1 2 3 4 5 6

1 2 3 4 5

END

Why?
I want that appear the number 6 too
Last edited on
> when you use the cin.peek() don't print the last number,

The last number is printed on line 12: std::cout << v << "\nEND\n" ; (here, v is the last number.)

This assumes that at least one number was entered, and there was no invalid input.
To remove that assumption, modify line 12 to:
1
2
3
// std::cout << v << "\nEND\n" ;
if(std::cin) /* if cin is not in a failed state */ std::cout << v ;
std::cout << "\nEND\n" ;
Topic archived. No new replies allowed.