0

Hi everyone i'm student who's learning c++ for engineering my professor was let me do the mini project for score on this code i want to make a canteen table reservation(with other function more than reservation) program and save Table number ,Name and Time but from this code in my .txt file there's only the time of reservation How can i fix this or ps1. Sorry for bad grammar ps2. you all can suggest me to improve my coding :D

***EDITED I just attach result of my .txt file my .txt file

#include <iostream>
#include <windows.h>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;


void table_set(int table[6][21]);
void reserve(int table[6][21],bool restatus[6][21],string name_data[6][21],bool time_data[101][20],string time_table[20]);
void table_map(int table[6][21]);
void time(bool time_data[100][20],string time_table[20],int cnum,string name);
void timetableshow(string time_table[]);







int main()
    {   
        ofstream outdata("E:/C++ PJ/Final Project/Reserving Data.txt");
        if(!outdata)
        {   
            cout << "Reserving Data File not found or cannot be open";
            exit(1);
        }
        //data_array
        const char *day[5]={"Mon","Tue","Wed","Thu","Fri"};
        //Array for table (5*20)
        int table[6][21]={0};
        bool restatus[6][21]={0};

        string name_data[6][21];//to store name (1name/1table)
        bool time_data[101][20]={0};// to store time 
        table_set(table);
        string time_table[20]={"0","7.30","8.00","8.30","9.00","9.30","10.00","10.30","11.00"
        ,"11.30","12.00","12.30","13.00","13.30","14.00","14.30","15.00","15.30","16.00","16.30"};


        //introduce
        cout << "Welcome to Canteen reserve program"<<endl;
        cout << "Student can reserve table and order the food and beverage from seller before you come to canteen!" << endl << endl;
        cout << "Please Enter alphabet to choose service below" << endl;
        cout << "-> T for Table Reserve service" << endl;

        reserve(table,restatus,name_data,time_data,time_table);

        for(int i=1;i<=5;i++)
            {
                for(int j=1;j<=20;j++)
                    {
                        cout << setw(3) << left << restatus[i][j];
                    }
                cout << endl;
            }
        outdata << setw(10) << right << "Table" << setw(10) << right << "name" << setw(10) << right << "time" << endl;
        outdata.close();




        return 0;
    }
void table_map(int table[6][21])
    {
        for(int i=1;i<=5;i++)
        {
            for(int j=1;j<=20;j++)
            {
                cout << setw(3) << left <<  table[i][j];
            }
            cout << endl;   
        }
    }
void reserve(int table[6][21],bool restatus[6][21],string name_data[6][21],bool time_data[101][20],string time_table[20])
    {
        ofstream outdata("E:/C++ PJ/Final Project/Reserving Data.txt");
        int cnum;
        cout << "Please choose your table to reserve" << endl;
        table_map(table);   
        cout << "TABLE ====> : ";
reserve:        
        cin >> cnum;
            while(cnum<1||cnum>100)
                {
                    cout << "Sorry we have only 100 table please choose again: ";
                    cin >> cnum;
                }
            for(int row=1;row<=5;row++)
                {
                for(int column=1;column<=20;column++)
                    {
                        if(table[row][column]==cnum)
                        {
                            while(restatus[row][column]!=0)
                            {
                                cout << "This table not available please choose another : ";;
                                goto reserve;
                            }
                            restatus[row][column]=1;
                        }
                    }
                }
        string name_for_reserve;
        cout << "Please let me know your name : ";
        cin >> name_for_reserve;
            for(int row=1;row<=5;row++)
                {
                for(int column=1;column<=20;column++)
                    {
                        if(table[row][column]==cnum)
                        {
                            {name_data[row][column]=name_for_reserve;}
                        }
                    }
                }
        cout << "I was reserve table#" << cnum << endl;
        cout << "By the name :" << setw(10) << left << name_for_reserve;
        outdata << setw(10) << right << cnum << setw(10) << right << name_for_reserve << setw(10) << right << "time" << setw(10) << "\t" << right << endl;
        time(time_data,time_table,cnum,name_for_reserve);





    }

void table_set(int table[6][21])
    {
        int table_counter=1;
        {for(int row=1;row<=5;row++)
            {for(int column=1;column<=20;column++)
                {
                    table[row][column]=table_counter;
                    table_counter++;
                }
            }
        }

    }

void time(bool time_data[100][20],string time_table[20],int cnum,string name_for_reserve)
    {
        const string name = name_for_reserve;
        ofstream outdata("E:/C++ PJ/Final Project/Reserving Data.txt");
        int timech[2];
        cout << "This is time table for reserve the table" << endl;
        cout << "Remember! if you're not arrive at canteen on time your reserve will be cancle!" << endl;
        cout << "And when check-out time arrive next students of your table can access the table immedietly!" << endl;
TIME:   for(int i=1;i<=19;i++)
            {
                cout << setw(7) << left << i << ".  " << time_table[i] << endl;
            }
        cout << "choose your check-in time By insert 1-19" << endl;
        cin >> timech[1];
        while(timech[1]<1||timech[1]>19)
            {
                cout << "Please insert correctly! :";
                cin >> timech[1];
            }
        while(time_data[cnum][timech[1]]=0)
            {
                cout << "Sorry But this time was reserve by other please choose new: ";
                cin >> timech[1];
            }
        cout << "next please choose your check-out time (1-19): ";
        cin >> timech[2];
        while(timech[2]<1||timech[2]>19)
            {
                cout << "Please insert correctly! :";
                cin >> timech[2];
            }
        while(time_data[cnum][timech[2]]=0)
            {
                cout << "Sorry But this time was reserve by other please choose new: ";
                cin >> timech[2];
            }
        bool timecheck=0;
        int error;
        for(int i=1;i<=20;i++)
            {
                if(time_data[cnum][i]=1)
                {
                    timecheck=0;            
                    error = i;
                    break;
                }
            }
        if(timecheck=0)
            {
                cout << "Sorry but on this period of time was reserved by someone at" << time_table[error] << "Please choose again:";
                goto TIME;
            }
        if(timecheck=1)
            {
                goto ACCEPT;
            }
ACCEPT: for(int i=timech[1];i<=timech[2];i++)
        {
            time_data[cnum][i]=1;
        }
        cout << cnum << endl;
        cout << name_for_reserve << endl;
    //  outdata << setw(10) << right << "Table" << setw(10) << right << "name" << setw(10) << right << "time" << endl;
        outdata << setw(10) << right << cnum << setw(10) << right << name << setw(10) << right << "time" << setw(10) << "\t" << right << time_table[timech[1]] << " - " << time_table[timech[2]] << endl;
    }



  • 2
    Please avoid using `goto` for flow control, more times than not it indicates poor code design. Also I don't see any reason to `#include ` since you don't appear to use anything from there, so i would avoid that since it is massive – Cory Kramer May 11 '20 at 14:45
  • Remember that `=` is used to assign and `==` is used to compare. – user4581301 May 11 '20 at 15:19
  • 1
    @CoryKramer thx a lot i'll fix it immidietly! :D – Takdanai Sornchan May 11 '20 at 15:19
  • You should pass `std::string` by reference. If you need many strings, use `std::vector`. – Thomas Matthews May 11 '20 at 15:25
  • Also remember that array indexes start at zero. `for(int i=1;i<=5;i++)` will start one array element late and avoids breaching the side of the array because extra memory has been allocated. No bug because it's being used consistently, but this often makes code harder, not easier, to write and makes it easy for other programmers to misuse the code. – user4581301 May 11 '20 at 15:25
  • Avoid using magic numbers. It makes the code harder to read. For example, in `for(int i=1;i<=19;i++)` is 19 the right number? Other places in the code 20 is used. Which is correct? Are they both correct? Are none of them correct? Make it easy on yourself. More reading on the evils of magic numbers at [What is a magic number, and why is it bad?](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) – user4581301 May 11 '20 at 15:30
  • To improve the question add the output you get and the expected output. Also strongly consider isolating the error by manufacturing a [mre]. A good MRE makes it almost impossible for the bug to hide, and a found bug is usually a squished bug. – user4581301 May 11 '20 at 15:33
  • I would not submit the code. It is not good. 0 comments. It has design errors. It uses ````goto```` and it has serious hard bugs like in ````cin >> timech[2]````. Please let me ask: Why are you using C-Style arrays? Did you learn about ````std::vector```` already? Are you allowed to use ````std::vector````? You are using ````std::string````. Still you are using ````const char*```` for some strings. Is there a reason? If you answer my questions, then I will help you with your code. Especially the ````std::vector```` (or also ````std::array````) question is most important. Is C++17 OK? – A M May 11 '20 at 15:40
  • @ArminMontigny I'm not learning about vector. my professor didn't teach me about it yet. and for question can i use string replace cons char (On this part i'll use it to browse the menu of day) – Takdanai Sornchan May 11 '20 at 15:45

0 Answers0