You are looking at: Practice test 2. Your place is still lesson 11, task 1: fopen.
test/2/shopping.c: shopping.txt into a dynamic array of structs, the sums and the most expensive line; a bad line and no file – a message and an error code.
Checklist
The file test/2/shopping.c, the program test2_shopping.
The program reads the file shopping.txt (from the folder it is run in). Each line is one item: a name (one word, up to 30 bytes), a quantity (a whole number, at least 1) and a price (with a dot, not negative):
Milk 2 1.29
Bread 1 0.99
Coffee 2 4.49
Apples 6 0.35
Cheese 1 3.80
Tea 3 1.50
This test/2/shopping.txt is already there, and course_data(shopping.txt) copies it to where ▶ runs the program. Edit the original: the next Build refreshes the copy.
Print every item with its sum, the total and the most expensive line (the largest quantity × price; of several equal ones, the first):
Items: 6
Milk: 2 x 1.29 = 2.58
Bread: 1 x 0.99 = 0.99
Coffee: 2 x 4.49 = 8.98
Apples: 6 x 0.35 = 2.10
Cheese: 1 x 3.80 = 3.80
Tea: 3 x 1.50 = 4.50
Total: 22.95
Most expensive: Coffee (8.98)
Cannot open the file shopping.txt. and return EXIT_FAILURE;.4,49, quantity 0, a negative price, junk at the end, an empty line) – shopping.txt, line 3: bad format. and EXIT_FAILURE. Don't print the other lines.Items: 0 and Total: 0.00, without Most expensive.typedef struct { char name[MAX_ITEM_NAME + 1]; int quantity; double price; } Item;realloc and a temporary pointer, like topic 12's IntArray. There must be no MAX_ITEMS.fgets, not fscanf. sscanf(line, "%30s %d %lf %c", ...) helps take one line apart: the line is good when it returns 3 – the fourth %c catches junk at the end.enum), not exit inside a function. On every path – fclose and free: GitHub CI with LeakSanitizer catches a leak on an error path too.assert.Each test runs the program in its own folder: in the first shopping.txt is the example above (6 items: the capacity grows 4 → 8), in the second line 3 is Coffee 2 4,49, in the third there is no file. The second and third tests expect an error code.
When ctest --preset debug -L test2 shows 100% tests passed, 0 tests failed out of 7, commit: Solve test 2: shopping list.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.