0

I'm trying to test my homework (knapsack problem) and it's getting repetitive entering all these inputs every time I recompile.

Here's what I've got:

will@will-mint ~/code/byun-sp15 $ g++ knapsack.cpp
will@will-mint ~/code/byun-sp15 $ ./a.out
Please enter number of items: 3
Please enter Knapsack Capacity: 10
Enter weights and values of 3 items: 
    Item 1: 3 40
    Item 2: 2 10
    Item 3: 5 50
0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 0 * 
0 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 
0 * 50 * 50 * 99 * 50 * 50 * 60 * 60 * 60 * 60 * 

Clearly my table is not correct, please do not help me there. NO SPOILERS! I put 3 10 3 40 2 10 5 50 in test.txt and tried the following:

will@will-mint ~/code/byun-sp15 $ vim test.txt
will@will-mint ~/code/byun-sp15 $ test.txt > ./a.out
test.txt: command not found
will@will-mint ~/code/byun-sp15 $ cat test.txt | ./a.out
will@will-mint ~/code/byun-sp15 $ cat test.txt | ./a.out >> output.log
will@will-mint ~/code/byun-sp15 $ vim output.log
will@will-mint ~/code/byun-sp15 $ cat test.txt | ./a.out 2> output.log
will@will-mint ~/code/byun-sp15 $ vim output.log
will@will-mint ~/code/byun-sp15 $ ./a.out < test.txt
will@will-mint ~/code/byun-sp15 $ cat test.txt | ./a.out > output.log
will@will-mint ~/code/byun-sp15 $ vim output.log 
will@will-mint ~/code/byun-sp15 $ ./a.out << test.txt

None of which worked. I need help with my bash-fu, how can I use a string of space-separated numbers in a text file as input for my a.out?

Will
  • 4,299
  • 5
  • 32
  • 50
  • `./a.out < test.txt` or the unoptimized version: `cat test.txt | ./a.out` should work, if test.txt contents are proper. – anishsane Apr 29 '15 at 04:23
  • Take a look at [expect](http://stackoverflow.com/tags/expect/info). – Cyrus Apr 29 '15 at 04:24
  • @anishane: I tried both, does this mean there a problem in my c++? – Will Apr 29 '15 at 04:27
  • @Cyrus: I was hoping not to to have to write a script, after searching SO trying to get one command to work – Will Apr 29 '15 at 04:28
  • 2
    When you did `test.txt > ./a.out`, you destroyed your `a.out`. Please compile again. – tivn Apr 29 '15 at 04:31
  • Awesome thanks @tivn that worked! './a.out < test.txt' was solution I found here, seemed strange it wasn't working... – Will Apr 29 '15 at 04:33

0 Answers0