'No suitable function from string to char exists
I'm fairly new to C++ and have been messing around with classes lately. In my problem I need to take in the ticket name and number and make functions that tell the user if the seat is available or not. If it is not available, the seat can be booked and needs to be stored. To solve this problem, I created a dynamic array that takes in the seat information and grows by length everytime a seat has been booked. Since this array takes in strings, I also defined it as a string. However, when I try to store in the seats and row names, I get an error that says that a string cannot be converted to a char. I tried to change the array to char type but that did not do the trick. Below I have added my code (sorry it's some what lengthy) with explanation. Hopefully I have provided you with enough info, thanks a lot!
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//Initializing Ticket class
class ShowTicket{
public: //Defining variables
string row;
string seatNumber;
bool sold; //true or false statement if the seat is available or not
string status;
int size;
string *arr = new string[size,2];
int *newSize = &size; //pointer that points to size and changes +1 length if
//seat gets booked
//Constructor
ShowTicket(string rows, string seatNumbers){
row = rows;
seatNumber = seatNumbers;
sold = false; //Seat is available by default untill it gets booked
status = "available"; //printing that the seat is available by default, untill
//it gets booked
string *arr = new string[size,2]; //2D dynamic array that grows in size and
//stores seat number and row
}
bool isSold(string row, string seat); //function taking in row and seat and checks
//if the seat is available
void sell_seat(string row, string seat); //function that sells a seat and stores it
//in arr
string print_ticket(string row, string seat);//function to print the ticket info
};
bool ShowTicket::isSold(string rows, string seats){
for(int i; i<= size; i++){ //for loop to loop over each element of the array
if (arr[i][0] != rows && arr[i][1] != seats){//check if each element matches
//with a sold ticket, if not it
//returns sold = false
return sold;
}
}
}
void ShowTicket::sell_seat(string row, string seat){ //This function adds 1 element to
//to pointer array to store in new
//data
*newSize = size+ 1; //new size is initialized and with that size is incremented by
//+1
string *temp = new string[newSize,2]; //temporary array is created with the new size
for(int i; i <size; i++){
temp[i] = arr[i]; //temporary array copies data from array
}
delete [] arr; // old pointer array is deleted to avoid memory leaks
arr = temp; //array is pointing to array with +1 length
arr[*newSize][0] = row; //new ticket info is stored in the newly created space
arr[*newSize][1] = seat; //^^
sold = true; //giving info that the ticket is indeed sold
status = "sold"; //when calling the function, it now also prints the ticket is
//stored
}
string ShowTicket::print_ticket(string row, string seat){
string a = "";
cout << row << " " << seatNumber << " " << status; //printing ticket info
return a;
}
int main(){
ShowTicket myticket1("AA","101"); //creating tickets with input
ShowTicket myticket2("AA","102");
ShowTicket myticket3("BB","103");
if(!myticket1.isSold()){//checking if the ticket is already sold
myticket1.sell_seat();//if not, it is bought
}
cout << myticket1.print_ticket() << endl; //printing ticket information
cout << myticket2.print_ticket() << endl;
cout << myticket3.print_ticket() << endl;
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
