2

I am using an executable made from C, and I am trying to pass variables in R to it.

C code:

#include <stdio.h>
#include <stdlib.h>
int sumUp(int x, int y, int z, int *sum);

int main()
{
    int x1,x2,x3;
    int total = 0;

    scanf("%d %d %d",&x1,&x2,&x3);
    sumUp(x1,x2,x3,&total);
    printf("Your total is :%d\n", total);

    system("pause");

}

int sumUp(int x, int y, int z, int *sum)
{
    *sum = x + y + z;
}

and Here is my R code:

x <- 0
y <- 0
z <- 0

readint <-function(){
  a <- readline(prompt = "Enter a number: \n")
}

x <- as.numeric(readint())
y <- as.numeric(readint())
z <- as.numeric(readint())

system("Practice.exe", intern = TRUE,input = "x y z")

I am having an issue when I use variables for input as it will print an incorrect value. However, when I use direct input, like input = "1 2 3", I will get the correct answer. I checked the types for for my variables, and they seem to be correct. Here is the output for reference:

Is there something wonky going on between R and C, or am I doing something incorrect with system? Even if I vary the values of x,y,z, I will get 84. So I imagine there is something going on under the hood here that I am missing. I tried using args instead of input, but ran into the same issue, except that it gave a value of 126 no matter what I entered, variable or directly. Any help is appreciated, thank you.

5
  • 1
    First thing first, remove system("pause") from your C code, it has no place in non-interactive programs and is unnecessary beyond that in 99.99% of cases. Secondly, you’re using the input parameter incorrectly in R. You’re supposed to pass a character vector of the actual values here, not of variable names. Commented Jun 3, 2019 at 16:47
  • Third, quoting the help page, system2 is recommended for new code. Commented Jun 3, 2019 at 16:52
  • @KonradRudolph Is there a proper way to pass input to an executable by variable name? Commented Jun 3, 2019 at 16:54
  • 1
    @hkj447 You need to pass the values. Check out the paste function for converting multiple values to string and concatenating them. Commented Jun 3, 2019 at 16:55
  • @KonradRudolph Paste worked, thank you! Commented Jun 3, 2019 at 17:05

1 Answer 1

1

I rather do this in C:

#include <stdio.h>
#include <stdlib.h>
int sumUp(int x, int y, int z, int *sum);

int main()
{
    int x1,x2,x3;
    int total = 0;
    x1 = strtol(argv[1], NULL, 10);
    x2 = strtol(argv[2], NULL, 10);
    x3 = strtol(argv[3], NULL, 10);

    sumUp(x1,x2,x3,&total);
    printf("Your total is :%d\n", total);

    system("pause");

}

int sumUp(int x, int y, int z, int *sum)
{
    *sum = x + y + z;
}

And this in R:

x <- 0
y <- 0
z <- 0

readint <-function(){
  a <- readline(prompt = "Enter a number: \n")
}

x <- as.numeric(readint())
y <- as.numeric(readint())
z <- as.numeric(readint())
cmd <- paste("Practice.exe", x, y, z)
system(cmd, intern = TRUE)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.