Hello cutwo,
I am trying to create a menu with a heap.
|
This statement is rather gormless. Are you wanting to store the menu on the heap for later use? Or, as the code suggests, it is a menu driven program that uses the heap for storage?
I am not use to "repl.it" and what it is like, but if your code is any indication I would suggest getting your own IDE.
Either you did not compile this code or the compiler at "repl,it" is not very good. because the function "main" has several errors.
From the top:
your include files
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
#include <string>
#include <ctime>
//#include <stdio.h>
//#include <string.h>
using namespace std; // <--- Best not to use.
| |
The first two lines are what I look at as being what you are most likely to put into any program. Next would be any header files that are specific to the program, in this case "ctime". The last two header files are C header files. "stdio.h" is most likely included through "iostream" and "string.h" is not even used in this program". When I commented them out it made no difference.
The last line the comment says it all. Many posts have been written here and elsewhere on this subject. Do a search if you are interested.
The class "Order' everything is "public" this tends to circumvent the point of a class. This way any line of code can change these variables and you may not know it happened.
In the class "Heap" you start with a "public" section of functions followed be a "private" section of functions. I am not sure why you want some of the functions private and since I have not been able to run the program yet I do not know how it will work.
To cover the problems in "main":
First I will point yo to a previous by
salem c http://www.cplusplus.com/forum/beginner/250857/#msg1104697 For what little I looked at in his link I tend to prefer the "Allman" style of coding. I feel it makes the code easier to read and follow. Along with the proper indentation it all helps. As an example I offer this:
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
int main()
{
Heap heap(100);
do
{
cout << "1.Insert new Record\n";
cout << "2.Service Record\n";
cout << "3.Display queue\n";
cout << "4.Exit\n";
cout << "Enter your choice : " << endl;
string id;
string input;
for (int i = 0; i < 3; i++)
{
//Format input
string temp = "";
int count = 0;
Order *newOrder = new Order();
for (int j = 0; j < input.length(); j++)
{
if (input[j] != '-')
temp += input[j];
else
{
if (count == 0)
{
newOrder->id = temp;
}
else if (count == 1)
{
newOrder->year = stoi(temp);
}
else if (count == 2)
{
if (temp == "y")
newOrder->warranty = true;
else
newOrder->warranty = false;
}
count++;
temp = "";
} // End of else.
} // End of second for loop.
getline(cin, input);
switch (input)
{
case 1:
cout << "Input the customerid and year of service" << endl;
cin >> customerid;
cin >> year;
cout << "Enter its warranty status in y/n : " << endl;
cin >> warranty;
Order();
break;
case 2:
remove();
break;
case 3:
display{};
break;
case 4:
count;
cout << "Exit & end-of-file" << endl;
break;
default:
cout << "Wrong choice\n";
} // End of switch.
}while (input != 4); // End of first or outer for loop. What is the while loop doing here?
} // End of do/while loop. Missing while condition.
heap.push(newOrder);
} // End of main.
// <--- Should these be inside "main"?
//heap.display();
//return 0;
| |
Keep in mind that the indentation here is a bit exaggerated and your IDE should not look like what is posted here.
If you can put a comment after the closing } it helps now and in the future along with having the {}s line up in the same column.
Once I changed thing around I noticed that line 81 has the while and condition at the end of a for loop when it needs to be on line 83 to go with the do loop.
The very last two lines of code I believe should be above line 85 and the closing } of "main".
On line 24 "j" is defined as an "int", but "input.length()" will return a "size_t" or "unsigned int" which creates a type mismatch. Enough to generate a warning, but not enough to stop the compile or running of the program. It may not always be necessary, but it helps when the types match.
On line 53 you are trying to switch on a string and that does not work. You will find "switch" at the bottom, but the whole page is worth reviewing.
http://www.cplusplus.com/doc/tutorial/control/
The condition of a switch needs to be an integral type meaning some type of an "int" or a "char". A string may contain more than one character which does not work.
Starting with line 57 "customerid", "year" and "warranty" are all undefined variables. Did you mean to use variables from the class "Order" or did you forget to define these variables?
Line 65 the function call to "remove" is missing a parameter.
Line 69 "display" id undefined. Did you want to use a function from a class or do you need to write this function?
Then each case statement is based on a number, but you are trying to switch on a string, so they do not match.
The while statement on line 81, which needs to be after the closing brace } of line 83, is trying to compare a "string" to an "int" and this will not work.
I refer you back to
http://www.cplusplus.com/forum/beginner/251348/ and
http://www.cplusplus.com/forum/beginner/250857/
All the responses from
MikeStgt,
Grey Wolf,
Repeater and
salem c although they may not directly apply to this program they are worth the time to learn from.
Hope that helps,
Andy