0

I want to grade my students' HWs quickly so I decided to write a batch script for this purpose. I know it is easier to do it in Ubuntu but since students are required to code their HWs in Windows, I have to evaluate them in Windows as well. Well, so far I wrote the following piece of batch file but it doesn't take the char variable.

Note that, purpose of the HW is draw a rectangle and fill it with the character given by keyboard (scanf)

@echo off
set file=%1
gcc %file% -o %file%.exe
REM Followings are "row col fill" respectively
echo 6 10 A | %file%.exe :: #1
echo 6 6   | %file%.exe :: #2
move %file% graded\
move %file%.exe graded\

Here, both at #1 and #2, it draws the rectangle with spaces however at #1 it should fill it with As.

In case of my students can find this question on web, I can't put whole solution here but I can show the scanf() char part:

//take num_of_rows and num_of columns
....
printf("\nFilling char?: ");
fflush(stdin);
scanf("%c",&fill);
...
//draw rectangle

And finally, here is the required output:

TEST-1
Enter number of rows (between 3-20) : 5
Enter number of columns (between 3-80) : 40
Enter a filling character (one char) : #
 +--------------------------------------+
 |######################################|
 |######################################|
 |######################################|
 +--------------------------------------+
TEST-2
Enter number of rows (between 3-20) : 8
Enter number of columns (between 3-80) : 25
Enter a filling character (one char) : (blank)
 +-----------------------+
 |                       |
 |                       |
 |                       |
 |                       |
 |                       |
 |                       |
 +-----------------------+

WARNING: Please don't put whole solution of C code, since it is a HW.

stack me
  • 25
  • 4
  • Without an [MCVE], it will be difficult to help you. But `fflush(stdin);` is not a [good idea](https://stackoverflow.com/q/2979209/1212012). A `scanf(" %c",&fill);` could do the job maybe. – Mathieu Oct 21 '19 at 12:32
  • `fflush(stdin);` probably discards everything you piped in. You could use `scanf("%d %d %c", &row, &column, &fill);` – mch Oct 21 '19 at 12:33
  • 1
    When you provide `"6 10 A"` sequentially to `scanf("%d")`, `scanf("%d")`, and `scanf("%c")` ... the first one will capture the 6 leaving `" 10 A"` in the buffer; the 2nd one will capture 10 leaving `" A"` in the buffer, the 3rd one will capture the space leaving `"A"` in the buffer. **Use `fgets()` for user input** – pmg Oct 21 '19 at 12:34
  • @mch with `fflush()` it works perfectly if I execute the binary file standalone (without batch file), however, with batch file, it still draws the rectangle correctly but doesn't fill it with the given value. So it means passing the arguments like this shouldn't be a problem because it takes `number_of_rows` and `number_of_columns` values even if we use 3 different `scanf()`'s. Finally, using `fflush()` is mandatory and it is already announced to the students by Instructor of the course (I am just the T.A.). – stack me Oct 21 '19 at 14:01
  • @pmg Same answer goes for you too, plus, after `fflush()` the students MUST use `scanf()` also. So I can't use `fgets()`. I don't want to edit their codes. It would take much more time to evaluate. – stack me Oct 21 '19 at 14:01
  • Well @stackme, my next suggestion is to have the instructor look at this page herself. – pmg Oct 21 '19 at 14:05
  • @pmg :) I am sure he wouldn't care how I evaluate them and how much time I have wasted, so I am all alone :) but thanks anyway. – stack me Oct 21 '19 at 14:06
  • Longshot, but any difference if you remove the trailing space after the A? `echo 6 10 A|%file%.exe` – avery_larry Oct 21 '19 at 14:10
  • @avery_larry ... maybe leading space??? `echo 6 10A | ...` – pmg Oct 21 '19 at 14:11
  • Well, I would presume there needs to be a delimiter between `10` and `A`. But maybe? Though the claim is that the line works outside of the batch file. I also know nothing about C. – avery_larry Oct 21 '19 at 14:13
  • @avery_larry already tried, still draws rectangle but doesn't fill it. weird... – stack me Oct 21 '19 at 14:14
  • And you're saying that `echo 6 10 A | hw.exe` works directly from the command line? – avery_larry Oct 21 '19 at 14:17
  • ok, I got the problem, if I remove the `fflush()` it works with the batch file perfectly but as I told you guys earlier, it is mandatory to use it so I am still open suggestions. – stack me Oct 21 '19 at 14:17
  • @avery_larry yes, I do. – stack me Oct 21 '19 at 14:18
  • It seems to be impossible with an `fflush(stdin);` between the `scanf`s to pipe the stdin into the process. The difference is the timing when you type it manually. You are typing `6 `, `scanf` sees the space and returns, `fflush` removes the space, then you type `10 `, `scanf` return and `fflush` removes the space and then you enter `A`, which `scanf` reads. If you pipe everything in at once, `fflush` will remove everything, which is not consumed by `scanf`. This means that the requirement to `fflush` is wrong. – mch Oct 21 '19 at 14:18
  • @mch you are right,I think I have to evaluate the HWs one by one after all... – stack me Oct 21 '19 at 14:19
  • Or you replace `fflush(stdin); scanf("%c", &fill);` by `scanf(" %c", &fill);` before you compile it. – mch Oct 21 '19 at 14:21

1 Answers1

0

Since I don't have a running Windows here and no idea how the rest of a solution of the homework looks like, this might or might not work. It might be that fflush(stdin) only purges the current input line and not the complete input.

You said (in the comments) that your sample solution works without the batch.

Then you might like to try to put the input in separate lines of an input file that will be fed into the homework programs:

echo.6>   input.txt
echo.10>> input.txt
echo.A>>  input.txt
%file%.exe < input.txt

echo.6>  input.txt
echo.6>> input.txt
echo. >> input.txt
%file%.exe < input.txt

The echo commands have to be literally like shown, of course you will vary rows, columns, and fill characters.

Some notes:

  • The dot after echo makes it think that an argument is given. Especially in the last command shown it would otherwise just output "Echo is ON" (or OFF.)

  • There should be no space after the row/column number if you don't like to have one in the input after the number.

  • In the respective third command there is only one character between echo. and >> input.txt. This is the fill character, 'A' and space, resp.

  • You may omit it the space between >> and the file name input.txt. It is just for clearity.

  • Just in case you don't know: > purges the output file before writing into it; >> will append to it.

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • Actually it didn't work but this is the only answer here. I solved my problem by editing the .c file before compiling with using "find & replace" feature of the powershell so I replaced `fflush` with `//fflush` like suggested here: https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – stack me Oct 23 '19 at 05:50