0

I've scoured the internet to no avail. I do not understand what this question is asking.

void case_three(int x, int y, char *actualResult) {
    int i, j, s, t, p, q;

    s = i = x;  // initialize variables with value from x
    t = j = y;  // initialize variables with value from y
    p = func(++i, ++j);
    q = mac(++s, ++t);
                // Copy the output to actualResult below... 
    printf("\n\n");                                                 //first variable increment
    printf("Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);

    // Replace the quoted content in the following strcpy statement with the actual output from last printf statement above.
    // Do not alter the text or add any spaces to it. 
    strcpy(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d", p, q);
    printf("\n\n");
    printf(actualResult);
}

When I run the code in VS I get 1 and 1 for the func and mac solutions. When I print the actualResult string, I get HUGE numbers that change everytime I execute it. In addition, when I attempt to compile in gcc, I get a error: too many arguments to function strcpy at the strcpy(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d", p, q); line.

So, I need to copy the output from the printf function to the char string actualResult but don't know how to do it correctly.

Any help is appreciated.

1 Answer 1

2

Very simple: use "sprintf()" instead of "printf()" to do formatted output to a string.

You don't need "strcpy()", and you can't use strcpy with formatting commands.

EXAMPLE:

/* The exact same output will go to your terminal as to the string "actualResult" */
printf("Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);
sprintf(actualResult, "Q3: Result from func(x, y) = %d and mac(x, y) = %d.", p, q);
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect, thank you. I was reading the instructions as saying I HAD to use "strcpy" and couldn't get it to work.
Recommend getting in the habit of using snprintf() to avoid unexpected buffer overruns. The key word here is "unexpected".
I would think, from reading the instructions in the comment, that you are expected to run the program get the output from the last printf() and then replace the quoted part of the strcpy with the what was printed out. For example if the output was this is the output, then you would change the string copy to read: strcpy(actualresult, "This is the output");, this way the output from the last printf would be the same (probably for some automated grading mechanism). However, as you saw with gcc, I suspect there is a typo in the strcpy as shown in the problem -- ask your instructor

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.