hhh

hhh
Last edited on
Sounds like a pretty basic assignment using classes. Have you worked with classes yet?
hhh
Last edited on
This would be much easier with a class that could use some sort of data structure but without you can implement separate arrays for each item type, i.e. an array of movies and an array of CDs. Unless you only need one item type, then use one array obviously.

For each to be accompanied by other information I would use an array of structs so each can hold multiple data types. This was you can enter the other information along with the item in the array and sorting will not affect which item is accompanied by which associated information.

As for sorting there are many available techniques such as bubble or insertion sort the two most likely to have been covered in your course before this point as they are easy to implement.
hhh
Last edited on
Since you've just started and haven't seen structures or classes I'm going to hope you've seen pointers. Here's an outline of the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string.h>

using namespace std;

int main(){

int choice = 3; // To store the selection of the user when prompted
int listSize = 0; // To store the size of the list
string * titles; // Pointer to a string
float * price; // Pointer to a float
bool quit = false; // To quit when the user wants to

While(quit == false){//Keep doing this loop while the user doesn't want to quit
     cout << "Enter '1' to add, '2' to show list, '3' to sort list or '4' to quit: ";
     cin >> choice; // Store the choice of the user in choice

     if(choice == 4){
          // Make the program quit
     }
     else if(choice == 2 && listSize != 0){
          // Iterate through the arrays titles and price
     }
     else if(choice == 1){
          // Ask for a title and a price
          // If this is the first item in the list then you need to create an array of 1 element
          if(listSize == 0){
               titles = new string[1]; // makes titles point to an array of strings which we just created
               price = new float[1]; // makes price point to an array of floats we just created
          }

          // If this is not the first time then you need to create an array that is one element larger than the existing array.
          else{
               string * tempString = new string[listSize + 1]; // A temporary pointer to an array we just created
               string * tempFloat = new string[listSize + 1];
               // Then create a loop to iterate and copy everything in the titles and price arrays in to tempString and tempFloat.
               // Then have the last element of tempString and tempFloat store the new entries of the user.
               // Now delete titles and price to free up the memory they were occupying
               delete [] titles;
               delete [] price;
               // Make titles and Price equal to tempString and tempFloat
               titles = tempString;
               price = tempFloat;
               tempString = NULL; // Make these NULL pointers
               tempFloat = NULL;

          }

     }
     else if(choice == 3){
          // Sort the list.
     }
}

    cout << "Press Enter" << endl;
    cin.ignore(255,'\n'); // Waits for an enter to be pressed before quitting.
    return 0;
}
Last edited on
hhh
Last edited on
yw
Last edited on
Topic archived. No new replies allowed.