Hello valkjo1,
As I promised here is your program with some blank lines and better formatting. Read the comments that I put in the program and notice how I used "fullName"ninsted of "r" and how "fullName" was used later.
The use of the "std::cin.ignore()" line is needed after a "std::cin >>"" and before a "std::getline" to remove the new line from the input buffer and anything else that might be there.
I did test your program and did make some changes that you may find work better and give a better output to the display. Mostly the use of a new line at the beginning of the text of a "std::cout" statement and the removal of the "std::endl" at the end of some lines.
Your problem with using "getline" with the variable "name" is most likely the problem because of the way some of the code is written, e.g.,
name = to_string(data.size() + 1) + " " + name + " " + r;
, is putting to much information into "name" Notice how I changed that in my code.
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// reserv.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h" // <--- Not needed when posting code here.
#include "iostream" // <--- semi-colons not needed here.
#include <vector>
#include <iomanip>
#include <string>
#include <limits>
//using namespace std; // <--- Best not to use.
int main()
{
int sel{};
int tables = 5;
int seats = 20;
int resSeat{};
std::string name;
std::string fuleName; //<--- Changed from "r".
std::vector <std::string> data;
std::cout << "Welcome to reservation system, what would you like to do? ... " << std::endl;
bool runner = true;
while (runner)
{
// <--- Needs an exit. Choice 4.
//std::cout << "1. Print All Reservers, 2. Add a reservation, 3. Remove a reservation ...: ";
std::cout << "\n1. Print All Reservers\n2. Add a reservation\n3. Remove a reservation\n4. Exit: " << std::endl;
std::cout << "Enter Choice: "; // <--- Added.
std::cin >> sel;
switch (sel)
{
case 1:
std::cout << "\nReservations: " << data.size() << "\nAvailable Tables: " << tables << "\nAvailable Seats: " << seats << std::endl;
// <--- Kind of pointless if "data.size()" is zero. Would not run any how.
for (unsigned int i = 0; i < data.size(); i++)
std::cout << data[i] << std::endl; // print reservation data
break;
case 2:
if (tables > 0) // tables available must be > 0
{
std::cout << "\nEnter your full name: "; // <--- Changed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::getline(std::cin, fuleName); // use enters name
// cout << "Enter last name: \n"; // full name entry breaks, if i have this as a 2nd input it works fine (user must enter first/last name seperately though)
// cin >> name;
//name = fuleName + " " + name; // <--- Too much information going into "name".
//name = fuleName; // <--- Not needed here because of what you do later.
bool wrong = true;
do
{
std::cout << "# Of Seats (max 4): "; //<--- Changed.
std::cin >> resSeat; // reserved seats
if (resSeat <= 4) // max 4
{
wrong = false;
}
} while (wrong);
// <--- Changed next line. TMI. Also changed to "+=".
name += std::to_string(data.size() + 1) + ". " + fuleName; // +" " + name + " " + fuleName; // add the reservation number to the string (e.g 1st reservation -> 1)
// <--- Next line can be combined with the above line.
name += "*" + std::to_string(resSeat); // add seats reserved to string after a *
tables = tables - 1;
seats = seats - resSeat;
data.push_back(name); // push into vector
std::cout << "\nThank you! Reservation has been added\n";
}
else
{ //<--- {}s not needed for one line, but OK.
std::cout << "\nWe are fully reserved today.. Sorry :-(\n";
}
break;
case 3:
int deal;
char conf;
// <--- You might want to print the reservations first to make it easier to know which reservation number to delete. Also check if "data" is has information to delete. > zero.
std::cout << "\nEnter which reservation to delete: "; // <--- Changed.
std::cin >> deal;
deal = deal - 1;
std::cout << "To erase: " << data[deal] << " y/n?: "; // <--- Changed.
std::cin >> conf;
if (conf == 'y' || conf == 'Y')
{
tables = tables + 1;
int ia = data[deal].at(data[deal].find("*") + 1) - '0';
std::cout << "\nRefunded: " << ia << " Seats.." << std::endl;
seats = seats + ia;
data.erase(data.begin() + (deal));
}
else
{
std::cout << "\nOperation cancelled.."; // <--- Changed.
}
break;
case 4: // <--- Added.
runner = false;
break;
default:
runner = false; // <--- Not the best idea. An error message would be bettern in place of this line.
break;
}
}
//system("pause"); // <--- Best not to use "system" anything.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
|
The lines just before the "return" you can use to replace the
system("pause");
that you have used and not everyone can use.
When posting code here it is best to leave out the line
#include "stdafx.h"
as it is only by Visual Studio and is consider a non standard C++ header file.
As far as the compiler is concerned where you put the opening { of a set is not a problem. As you can see in the above code putting th opening { on the next line and proper indenting makes the code easier to read along with the {}s lining up in the same column makes it easier to find when one is missing.
Hope that helps,
Andy
P.S. Notice the "std::cin.ignore" before the getline. What happens is the use of "std::cin >>" before a "std::getline()" leaves the new line in the input buffer that the "std::getline()" will read and process continuing on to the next line of code. Referring to line 48 in my code.