r - Saving user input values into an array -


essentially want save of user inputs array , add them when user enters 0. don't know how that. in script, x value inevitably changed every time.

here's script far:

print("this program sum series of numbers.") x <- 1:100     num <- c(x) while (num[x] != 0) {     print("enter next number (enter 0 when finished)")     num[x] <- as.numeric(readlines(con=stdin(),1)) } sum <- sum(num) print(paste("the sum of numbers is", sum)) 

i error:

in while (num[x] != 0) { : condition has length > 1 , first element used

can please me?

here's 1 possible solution:

print("this program sum series of numbers.") next_entry <- 1 entries <- vector() while (next_entry != 0) { print("enter next number (enter 0 when finished)") next_entry <- as.numeric(readlines(con=stdin(),1)) entries <- c(entries, next_entry) } sum <- sum(entries) print(paste("the sum of numbers is", sum)) 

the problem script "num" defined, since set 1:100 on 3rd line.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -