Convert a string into an array of integers - c++

I ve got a string which is white space seperated integer numbers and I want to convert it in array of vector of intergers. My string is like the:
6 9 17 5 3 4 10 12 7 3 5 10 6 5 0 10 10 10 13 3 6 10 2 11 33 9 14 7 0 8 7 6 38 2 23 8 4 52 7 3 19 12 2 22 3 6 3 1 2 1 5 17 13 5 1 6 0 12 6 9 15 2 22 0 27 2 3 4 7 2 8 2 8 6 11 22 4 9 4 1 1 2 2 16 8 3 1 8 0 4 4 2 1 10 24 15 8 2 6 9
How is it possible to convert it to array/vector??

Use std::istream_iterator. Example:
std::vector<int> vector(std::istream_iterator<int>(std::cin), std::istream_iterator<int>());
Or, with std::string:
std::string s = "6 9 17 5 3 4 10 12 7 3 5 10 6 5 0 10 10 10 13 3 6 10 2 11 33 9 14 7 0 8 7 6 38 2 23 8 4 52 7 3 19 12 2 22 3 6 3 1 2 1 5 17 13 5 1 6 0 12 6 9 15 2 22 0 27 2 3 4 7 2 8 2 8 6 11 22 4 9 4 1 1 2 2 16 8 3 1 8 0 4 4 2 1 10 24 15 8 2 6 9";
std::stringstream ss(s);
std::vector<int> vec((std::istream_iterator<int>(ss)), (std::istream_iterator<int>()));

This following code does what you need:
std::istringstream iss(my_string);
std::vector<int> v((std::istream_iterator<int>(iss)),
(std::istream_iterator<int>()));

Here is a ready to use example with all required headers
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::string s = "6 9 17 5 3 4 10 12 7 3 5 10 6 5 0 10 10 10 13 3 6 10 2 11 33 9 14 7 0 8 7 6 38 2 23 8 4 52 ";
std::istringstream is( s );
std::vector<int> v;
std::transform( std::istream_iterator<std::string>( is ),
std::istream_iterator<std::string>(),
std::back_inserter( v ),
[]( const std::string &s ) { return ( std::stoi( s ) ); } );
for ( int x : v ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
Or indeed instead of algorithm std::transform you can use simply the constructor of class std::vector that accepts two iterators as for example
std::vector<int> v( ( std::istream_iterator<int>( is ) ),
std::istream_iterator<int>() );
Or
std::vector<int> v( { std::istream_iterator<int>( is ),
std::istream_iterator<int>() } );

I see some older and smarter way of doing that:
#include <stdlib.h> /* strtol */
char *numbers = "6 9 17 5 3 4 10 12 7 3 5 10 6 5 0 10 10 10 13 3 6 10 2 11 33 9 14 7 0 8 7 6 38 2 23 8 4 52 7 3 19 12 2 22 3 6 3 1 2 1 5 17 13 5 1 6 0 12 6 9 15 2 22 0 27 2 3 4 7 2 8 2 8 6 11 22 4 9 4 1 1 2 2 16 8 3 1 8 0 4 4 2 1 10 24 15 8 2 6 9"
long table[512];
char *pt;
int i=0;
pt = numbers;
while(pt != NULL)
table[i++] = strtol (pt,&pt,10);

Related

Solve two equations of Linear Inequality involving three different variables through code

I have two linear inequality equations with me, I am trying to understand how can I solve them using the code.
2x + z <= A
3y + z <= B
I know the values of A and B. I am trying to find all the possible positive values of x , y and z that will satisfy the given equation.
I want to solve the above equation using code but I haven't been able to come up with anything.
Any pointers are helpful. Thanks.
Linear inequalities can be represented as boolean expressions. Either the inequality is true, or false. We can use a function in C++ just like a function in mathematics here.
bool equation_one(double x, double z, double A)
{
return ((2 * x) + z <= A);
}
bool equation_two(double y, double z, double A)
{
return ((3 * y) + z <= A);
}
Then, if we wanted to see if any values satisfies these equations, we can just try values.
NOTE: This is horribly inefficient. We are looking at cubic time bounds. But, this is much more straight forward then using a linear algebra technique.
for(double x = 0; x <= A/2; x++)
for(double y = 0; y <= B/3; y++)
for(double z = 0; z <= A && z <= B; z++)
if(equation_one(x, z, A) && equation_two(y, z, B))
//These are the values we want!
EDIT: My bad, you said positive values only. Fixed.
EDIT 2: Thought I'd provide some output. For A = 20 and B = 20, these are all possible positive integer values that satisfy the equations with x, y, and z respectively.
0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
0 0 5
0 0 6
0 0 7
0 0 8
0 0 9
0 0 10
0 0 11
0 0 12
0 0 13
0 0 14
0 0 15
0 0 16
0 0 17
0 0 18
0 0 19
0 0 20
0 1 0
0 1 1
0 1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 2
7 4 3
7 4 4
7 4 5
7 4 6
7 5 0
7 5 1
7 5 2
7 5 3
7 5 4
7 5 5
7 6 0
7 6 1
7 6 2
8 0 0
8 0 1
8 0 2
8 0 3
8 0 4
8 1 0
8 1 1
8 1 2
8 1 3
8 1 4
8 2 0
8 2 1
8 2 2
8 2 3
8 2 4
8 3 0
8 3 1
8 3 2
8 3 3
8 3 4
8 4 0
8 4 1
8 4 2
8 4 3
8 4 4
8 5 0
8 5 1
8 5 2
8 5 3
8 5 4
8 6 0
8 6 1
8 6 2
9 0 0
9 0 1
9 0 2
9 1 0
9 1 1
9 1 2
9 2 0
9 2 1
9 2 2
9 3 0
9 3 1
9 3 2
9 4 0
9 4 1
9 4 2
9 5 0
9 5 1
9 5 2
9 6 0
9 6 1
9 6 2
10 0 0
10 1 0
10 2 0
10 3 0
10 4 0
10 5 0
10 6 0

Duplication of data entries by id if they meet a certain condition

In the original choice data set, individuals (id) are captured making purchases (choice) among all the product options possible (assortchoice is a product code). Every individual always faces the same set of products to choose from; as a result the value of choice is always either 0 or 1 ("was the product chosen or not?").
clear
input
id assortchoice choice sumchoice
2 12 1 2
2 13 0 2
2 14 0 2
2 15 0 2
2 16 0 2
2 17 0 2
2 18 0 2
2 19 0 2
2 20 0 2
2 21 0 2
2 22 0 2
2 23 1 2
3 12 1 1
3 13 0 1
3 14 0 1
3 15 0 1
3 16 0 1
3 17 0 1
3 18 0 1
3 19 0 1
3 20 0 1
3 21 0 1
3 22 0 1
3 23 0 1
4 12 1 3
4 13 0 3
4 14 1 3
4 15 1 3
4 16 0 3
4 17 0 3
4 18 0 3
4 19 0 3
4 20 0 3
4 21 0 3
4 22 0 3
4 23 0 3
end
I created the following code to understand how many choices were made by each individual:
egen sumchoice=total(choice), by(id)
In this example, an individual 3 (id=3) only chose one product (since sumchoice=1), but individual 2 made two choices (sumchoice=2). Finally, individual 4 made three choices (sumchoice=3).
Since this is a choice data, I need to transform all the instances of multiple choices into sets of single choices.
What I mean by that: if an individual made two purchases, I need to duplicate the choice set for that individual twice; for an individual who made 3 purchases, I need to replicate the choice set three times, so the final structure looks like the data set below.
clear
input
id transaction assortchoice choice
2 1 12 1
2 1 13 0
2 1 14 0
2 1 15 0
2 1 16 0
2 1 17 0
2 1 18 0
2 1 19 0
2 1 20 0
2 1 21 0
2 1 22 0
2 1 23 0
2 2 12 0
2 2 13 0
2 2 14 0
2 2 15 0
2 2 16 0
2 2 17 0
2 2 18 0
2 2 19 0
2 2 20 0
2 2 21 0
2 2 22 0
2 2 23 1
3 1 12 1
3 1 13 0
3 1 14 0
3 1 15 0
3 1 16 0
3 1 17 0
3 1 18 0
3 1 19 0
3 1 20 0
3 1 21 0
3 1 22 0
3 1 23 0
4 1 12 1
4 1 13 0
4 1 14 0
4 1 15 0
4 1 16 0
4 1 17 0
4 1 18 0
4 1 19 0
4 1 20 0
4 1 21 0
4 1 22 0
4 1 23 0
4 2 12 0
4 2 13 0
4 2 14 1
4 2 15 0
4 2 16 0
4 2 17 0
4 2 18 0
4 2 19 0
4 2 20 0
4 2 21 0
4 2 22 0
4 2 23 0
4 3 12 0
4 3 13 0
4 3 14 0
4 3 15 1
4 3 16 0
4 3 17 0
4 3 18 0
4 3 19 0
4 3 20 0
4 3 21 0
4 3 22 0
4 3 23 0
end
***update:
transaction indicates which transaction order this is:
bysort id assortchoice (choice): gen transaction=_n
Hence, choice=1 should appear only once per each transaction.
The answer isn't quite "use expand" as there is a twist that you don't want exact replicates.
expand sumchoice
bysort id assortchoice (choice) : replace choice = 0 if _n != _N & choice == 1
list if id == 2 , sepby(assortchoice)
+-----------------------------------+
| id assort~e choice sumcho~e |
|-----------------------------------|
1. | 2 12 0 2 |
2. | 2 12 1 2 |
|-----------------------------------|
3. | 2 13 0 2 |
4. | 2 13 0 2 |
|-----------------------------------|
5. | 2 14 0 2 |
6. | 2 14 0 2 |
|-----------------------------------|
7. | 2 15 0 2 |
8. | 2 15 0 2 |
|-----------------------------------|
9. | 2 16 0 2 |
10. | 2 16 0 2 |
|-----------------------------------|
11. | 2 17 0 2 |
12. | 2 17 0 2 |
|-----------------------------------|
13. | 2 18 0 2 |
14. | 2 18 0 2 |
|-----------------------------------|
15. | 2 19 0 2 |
16. | 2 19 0 2 |
|-----------------------------------|
17. | 2 20 0 2 |
18. | 2 20 0 2 |
|-----------------------------------|
19. | 2 21 0 2 |
20. | 2 21 0 2 |
|-----------------------------------|
21. | 2 22 0 2 |
22. | 2 22 0 2 |
|-----------------------------------|
23. | 2 23 0 2 |
24. | 2 23 1 2 |
+-----------------------------------+

fortran read and write from file(reading from .msh and writing to dat)

I am trying to read a .msh file and want to generate .dat file in rearranged manner (node number, x1 ,y1 , z1, x2, y2, z2)
$MeshFormatv
2.2 0 8
$EndMeshFormat
$PhysicalNames
4
1 1 "inlet"
1 2 "top"
1 3 "exit"
1 4 "bottom"
$EndPhysicalNames
$Nodes
45
1 -2 -2 0
2 2 -2 0
3 2 2 0
4 -2 2 0
5 -1.666666666666667 -2 0
6 -1.333333333333333 -2 0
7 -1 -2 0
8 -0.6666666666666665 -2 0
9 -0.3333333333333335 -2 0
10 0 -2 0
11 0.3333333333333335 -2 0
12 0.666666666666667 -2 0
13 1 -2 0
14 1.333333333333333 -2 0
15 1.666666666666667 -2 0
16 2 -1.666666666666667 0
17 2 -1.333333333333333 0
18 2 -1 0
19 2 -0.6666666666666665 0
20 2 -0.3333333333333335 0
21 2 0 0
22 2 0.3333333333333335 0
23 2 0.666666666666667 0
24 2 1 0
25 2 1.333333333333333 0
26 2 1.666666666666667 0
27 1.666666666666667 2 0
28 1.333333333333333 2 0
29 1 2 0
30 0.6666666666666665 2 0
31 0.3333333333333335 2 0
32 0 2 0
33 -0.3333333333333335 2 0
34 -0.666666666666667 2 0
35 -1 2 0
36 -1.333333333333333 2 0
37 -1.666666666666667 2 0
38 -2 1.555555555555556 0
39 -2 1.111111111111111 0
40 -2 0.6666666666666667 0
41 -2 0.2222222222222223 0
42 -2 -0.2222222222222223 0
43 -2 -0.6666666666666665 0
44 -2 -1.111111111111111 0
45 -2 -1.555555555555555 0
$EndNodes
$Elements
45
1 1 2 4 1 1 5
2 1 2 4 1 5 6
3 1 2 4 1 6 7
4 1 2 4 1 7 8
5 1 2 4 1 8 9
6 1 2 4 1 9 10
7 1 2 4 1 10 11
8 1 2 4 1 11 12
9 1 2 4 1 12 13
10 1 2 4 1 13 14
11 1 2 4 1 14 15
12 1 2 4 1 15 2
13 1 2 3 2 2 16
14 1 2 3 2 16 17
15 1 2 3 2 17 18
16 1 2 3 2 18 19
17 1 2 3 2 19 20
18 1 2 3 2 20 21
19 1 2 3 2 21 22
20 1 2 3 2 22 23
21 1 2 3 2 23 24
22 1 2 3 2 24 25
23 1 2 3 2 25 26
24 1 2 3 2 26 3
25 1 2 2 3 3 27
26 1 2 2 3 27 28
27 1 2 2 3 28 29
28 1 2 2 3 29 30
29 1 2 2 3 30 31
30 1 2 2 3 31 32
31 1 2 2 3 32 33
32 1 2 2 3 33 34
33 1 2 2 3 34 35
34 1 2 2 3 35 36
35 1 2 2 3 36 37
36 1 2 2 3 37 4
37 1 2 1 4 4 38
38 1 2 1 4 38 39
39 1 2 1 4 39 40
40 1 2 1 4 40 41
41 1 2 1 4 41 42
42 1 2 1 4 42 43
43 1 2 1 4 43 44
44 1 2 1 4 44 45
45 1 2 1 4 45 1
$EndElements
I have tried with allocatable, I want to skip the lines till character '$Nodes' appear and and one more line then read it in a array and then skip the three lines of character. Read the next in another array and then rearrange the no as mentioned above.
program coordinates
implicit none
INTEGER:: ierror, nodeno, elementno, i, j, k , t, p, l=0, n
CHARACTER:: command
real::data(2,100)
!CHARACTER (len=5)::N!odes
!CHARACTER (len=8)::EndN!odes
!CHARACTER (len=8)::E!lements
!CHARACTER (len=11)::EndE!lements
!CHARACTER :: No*5, EndN*8, E*8, EndE*11
! CHARACTER*5 :: Nod
! CHARACTER*8 :: Ele
! real, allocatable, dimension(:,4)::node
! real, allocatable, dimension(:,7)::element
! real, allocatable, dimension(:)::n,x,y,z,a,b,c,d,g,h
!call system(l='grep -n '$Nodes' /home/user/Nitesh/Fortran/rect.msh|tail -LineNumberToStartWith|grep regEX')
! 'l = 'grep -n '$Nodes' /home/user/Nitesh/Fortran/rect.msh
command = 'grep -n $Nodes /home/user/Nitesh/Fortran/rect.msh|cut -f1 -d:'
! call system('command')
call system('grep -n '$Nodes' /home/user/Nitesh/Fortran/rect.msh|cut -f1 -d:')
! call system('l')
! print*, "enter the no. of nodes"
! read*,t
! print*, "enter the no. of elements"
! read*,p
! print*, "enter the line no. nodes data(array) starting from"
! read*,l
!allocate(node(t),element(j))
print*, "opening file"
! allocate(n(t),x(t),y(t),z(t),a(t),b(t),c(t),d(t),g(t),h(t))
OPEN (FILE='/home/user/Nitesh/Fortran/my.dat',UNIT=8, STATUS='OLD', ACTION='READ', &
IOSTAT=ierror)
if(ierror/=0)then
print*,"File rect.msh cannot be open"
stop
end if
do i=1,n
read(8,'(/)')
end do
do i=1,t+l
read(8,*) data(:,i)
!read(8,*) j,k
end do
! 8 format('',F10.6,F10.6,F10.6,F10.6)
! if(Nod == 'Nodes') then
! read*,(n(i),x(i),y(i),z(i),i=1,t)
! end if
! if(Ele == 'Elements') then
! read*,(n(j),a(j),b(j),c(j),d(j),g(j),h(j),k=1,p)
! end if
! OPEN (UNIT=10, FILE='/home/Nitesh/rect_new.msh', STATUS='NEW', ACTION='WRITE', &
! IOSTAT=ierror)
! write(*,10)
! 10 format (' ',n())
! CLOSE (UNIT=10)
CLOSE (UNIT=8)
print*, "file read"
! do i=1,n
! n(t)=g(j)
! x(t),y(t),z(t)
open(file='/home/user/Nitesh/Fortran/rect.dat', unit=24, status='replace', action='write', &
IOSTAT=ierror)
if(ierror/=0)then
print*,"File rect.msh cannot be open"
stop
end if
print*,"writing data"
do i=1,l+t
write(24,*) data(:,i)
end do
print*,"data written"
! OPEN (UNIT=10, FILE='rect_new.msh', STATUS='NEW', ACTION='WRITE', &
! IOSTAT=ierror)
! write(*,10)
! 10 format (' ','The coordinates of elements')
! CLOSE (UNIT=10)
end program coordinates

Replace middle row elements of nested list with new list elements Q kdb

Hi so I have created the nested list/matrix:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m
1 2 3
4 5 6
7 8 9
I have also identified the middle column in the list:
q)a:m[0;1],m[1;1],m[2;1]
I now want to replace the middle row (4 5 6) with a to finish with m looking like:
q)m
1 2 3
2 5 8
7 8 9
You've already seen you can index into the matrix with syntax like m[0;1], where 0 refers to the first level of nesting and 1 refers to the second level
KDB also allows you to assign to an index of a list in a similar way e.g.
q)l:1 2 3 4
q)l[1]:20
q)l
1 20 3 4
So you can use something similar in this example:
q)m[1]:a
q)m
1 2 3
2 5 8
7 8 9
As an aside, KDB also allows you to leave out an index, in which case it will take all items from the corresponding level of nesting, e.g.
q)m[0] /first level of nesting i.e. first row
1 2 3
q)m[;0] /second level of nesting i.e. first column
1 4 7
Hope that helps
Jonathon McMurray
AquaQ Analytics
You want to generalise for larger matrices (which must also be square) so your answer needs two parts:
how to construct a
how to insert it
for row/col x where x<count m.
The general expression you want is simply m[x;]:m[;x], because m[x;] denotes row x and m[;x] denotes column x.
See Q for Mortals 3.11.3 Two- and Three-Dimensional Matrices
You can make this a function of the index and the matrix:
q)show m:5 5#1_til 26
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
q){y[x;]:y[;x];:y}[3;m]
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
4 9 14 19 24
21 22 23 24 25
Just adding another approach for you.
q)m:8 cut til 64
q)0 0+\:til 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
q)(m)./:flip 0 0+\:til 8
0 9 18 27 36 45 54 63
q)#[m;4;:;(m)./:flip 0 0+\:til 8]
0 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
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q)
For fun, here it is in a function which takes the length&width of the matrix and replaces the 'middle' row with the diagonal values
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}8
0 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
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}5
0 1 2 3 4
5 6 7 8 9
0 6 12 18 24
15 16 17 18 19
20 21 22 23 24
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}4
0 1 2 3
4 5 6 7
0 5 10 15
12 13 14 15
q)
q)#[((1 2 3);(4 5 6);(7 8 9));1;:;(2;5;8)]
1 2 3
2 5 8
7 8 9
Indexing in q can be straight forward and I believe a intermediate can be omitted:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m[1]:m[;1]
q)m
1 2 3
2 5 8
7 8 9

Corrupted "cout" library?

OK, so here is what i got going on. I use the latest version of Clion by JetBrains. I run the MinGW GUI as a compiler and use the bundled CMake that comes with the GUI.
So here is my code.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 30; i++) {
for (int j = 1; j <= i; j++) {
cout << j << " ";
}
cout << " LINE: " << i;
cout << endl;
}
return 0;
}
Very simple and this is what it should output
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
And it continues up until 30.
I know that the actual code is correct because when I run it on a different compiler (On an Ubuntu VM) it comes out perfectly. But when I run it on Clion this is the output:
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
1 2 3 4 5 LINE: 5
1 2 3 4 5 6 LINE: 6
1 2 3 4 5 6 7 LINE: 7
1 2 3 4 5 6 7 8 LINE: 8
1 2 3 4 5 6 7 8 9 LINE: 9
1 2 3 4 5 6 7 8 9 10 LINE: 10
1 2 3 4 5 6 7 8 9 10 11 LINE: 11
1 2 3 4 5 6 7 8 9 10 11 12 LINE: 12
1 2 3 4 5 6 7 8 9 10 11 12 13 LINE: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 LINE: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 LINE: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LINE: 16
1 2 3 4 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LINE: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LINE: 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 LINE: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LINE: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 LINE: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LINE: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 LINE: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 LINE: 24
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 LINE: 25
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 LINE: 26
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 LINE: 27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 181 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 LINE: 28
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 LINE: 29
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 LINE: 30
Ya, so that clearly doesn't work. And and to solidify the fact that its not my code, when I run it again...
1 LINE: 1
1 2 LINE: 2
1 2 3 LINE: 3
1 2 3 4 LINE: 4
1 2 3 4 5 LINE: 5
1 2 3 4 51 2 3 4 5 6 LINE: 6
1 2 3 4 5 6 7 LINE: 7
1 2 3 4 5 6 7 8 LINE: 8
1 2 3 4 5 6 7 8 9 LINE: 9
1 2 3 4 5 6 7 8 9 10 LINE: 10
1 2 3 4 5 6 7 8 9 10 11 LINE: 11
1 2 3 4 5 6 7 8 9 10 11 12 LINE: 12
1 2 3 4 5 6 7 8 9 10 11 12 13 LINE: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 LINE: 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 LINE: 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LINE: 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LINE: 17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 LINE: 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 LINE: 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 LINE: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 LINE: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LINE: 22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 LINE: 23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 LINE: 24
1 2 3 4 5 6 71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LINE: 25
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 LINE: 26
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 LINE: 27
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 LINE: 28
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 LINE: 29
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 LINE: 30
And if ran it again it would once again be different. It never the same and is always doing the same thing more or less.
This issue only seems to exist when I run programs that spit out long cout's. For example the HelloWorld runs fine. And as the last kick in the nuts, I ran it on my other computer (same setup with Clion and MinGW) and the exact same thing was happening.