gpt4 book ai didi

c++ - 从 'const char*' 到 'char' 的无效转换

转载 作者:行者123 更新时间:2023-11-30 02:55:53 25 4
gpt4 key购买 nike

我目前正在研究一个杂项剧院座位计划!我刚刚开始通过 C++ 取得进展,但我无法弄清楚我在编码中不断遇到的这个错误。我目前正在尝试编码的是一种逐字符输入“*”的方法,最终帮助我构建一个函数来显示电影院的座位表。我非常接近完成名为 readSeating 的函数......尽管它存在一些明显的问题。通常我可以非常有效地调试我的程序,但我就是想不通这个。请看看我的代码,感受一下我的程序,让我有任何想法。请让答案简单一些;再一次,我不是 C++ 的高手

附言

抱歉显示了整个代码...这是我的第一篇文章...我真的不知道该包含什么,不该包含什么;我希望人们能够通过自己测试来感受该程序...再次感谢!

/* 
* This is a program built by a team of students
* to help local movie theaters sell tickets
*
* File: main.cpp
* Author(s):
*
*
* Created on April 15, 2013, 11:10 AM
*/


#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;

//Function Prototypes
void Title_Card();
void seating_prices();
void seating_chart();
void pick_seating();
void purchase_history();
void quit();
void update_file();
void Show_Menu();
void is_digit(int&);
void Admin_Menu();
void edit_seating_prices();
void edit_seating();
void quit_to_original();
void purchase_history_admin();

int readSeating (char, vector<char>&); //Reads SeatingChart.txt
int readPrices(string, vector<double>&); //Reads SeatingPrices.txt
vector<double> prices(15); //For SeatPrices.txt
vector<char> seating(450); //For SeatingChart.txt



//Actual Program
int main() {
Title_Card(); //Calls Title Page

readSeating("SeatingChart.txt", seating); //Reads SeatingChart.txt
readPrices("SeatPrices.txt", prices); //Reads SeatPrices.txt
Show_Menu(); //Shows Introductory Menu

system ("pause");
return 0;
}




//**************************************************************
// Definition of the ShowMenu function. Shows introductory menu*
// and controls where user ends up in the program. *
//**************************************************************


void Show_Menu() {
int choice;
string password;

cout << "Welcome to the our theater program! Made for a person" << endl;
cout << "who is looking for seating, purchasing a ticket, and" << endl;
cout << "searching for other miscellaneous things... We hope" << endl;
cout << "you enjoy the program!" << endl << endl;
cout << "Below is a list of options the user can choose from:" << endl << endl;
cout << "1.\tSeating Prices" << endl;
cout << "2.\tPick Seating" << endl;
cout << "3.\tPurchase History" << endl;
cout << "4.\tQuit" << endl << endl;
cout << "Enter a choice... (1-4): ";
cin >> choice;

//Secret Administrator Access
if (choice == 219){
system ("cls");
cout << "Enter the administrator password: ";
cin >> password;

if (password == "sterilegorilla")
Admin_Menu();
else {
system ("cls");
Show_Menu();
}
}

is_digit(choice);//is_digit function doesn't work atm


while (choice < 1 || choice > 4){
cout << endl << "You have entered an invalid choice!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> choice;
is_digit(choice); //is_digit function doesn't work atm
if (choice == 219){
system ("cls");
cout << "Enter the administrator password: ";
cin >> password;

if (password == "sterilegorilla")
Admin_Menu();

else {
system ("cls");
Show_Menu();
}
}
}

switch (choice) {
case 1:
seating_prices();

case 2:
pick_seating();

case 3:
purchase_history();

case 4:
quit();
}

}





//**************************************************************
// Definition of the seating_prices function. Displays to the *
// user, SeatPrices.txt *
//**************************************************************


void seating_prices(){
system ("cls");
cout << "The Current Seating Prices Are:" << endl << endl;
for (int count = 0; count < 4; count++){
cout << " " << setprecision(4) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
for (int count = 4; count < prices.size(); count++){
cout << " " << setprecision(3) << showpoint << prices[count] << " | Row " << (count + 1) << endl;
}
cout << endl;
system ("pause");
system ("cls");
Show_Menu();
}




//**************************************************************
// Definition of the pick_seating function. Displays the *
// current seating chart and allows the user to pick their seat*
//**************************************************************


void pick_seating(){ //Not Finished
system ("cls");
int row_choice;
int seat_choice;

cout << "The Current Seating Chart Is:" << endl;

//Display Seating Chart



//Picking your seat
cout << "Enter the row number you would like to be in (1-15): ";
cin >> row_choice;
while (row_choice < 1 || row_choice > 15){
cout << endl << "You have entered an invalid row number!" << endl;
cout << "Enter the row number you would like to be in (1-15): ";
cin >> row_choice;
}

cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
while (seat_choice < 1 || seat_choice > 30){
cout << endl << "You have entered an invalid seat number!" << endl;
cout << "Enter the seat number you would like to have (1-30): ";
cin >> seat_choice;
}

cout << "Congratulations! You have picked your seat!" << endl << endl;
system ("pause");
system ("cls");
Show_Menu();
}




//**************************************************************
// Definition of the purchase_history function. Displays the *
// current the total sum of all movie ticket purchases *
//**************************************************************


void purchase_history(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Show_Menu();
}



//**************************************************************
// Definition of the quit function. Allows the user to quit the*
// program entirely *
//**************************************************************


void quit(){
update_file();
exit(0);
}



//**************************************************************
// Definition of the is_digit function. Designed to make it *
// possible to enter only integers when asked for integers *
//**************************************************************



void is_digit(int & input){ //Not working atm
if (isdigit(input)){
return;
}
if (isalpha(input)){
cout << endl << "You have entered an invalid data type!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> input;
}
if (isspace(input)){
cout << endl << "You have entered an invalid data type!" << endl;
cout << "Enter a choice... (1-4): ";
cin >> input;
}
}




//**************************************************************
// Definition of the update_file function. Designed to update *
// the seating chart upon leaving the pick_seating function *
//**************************************************************



void update_file(){ //Not finished

//This function is supposed to
//Update the seating chart
//upon exit of the pick_seating function
}




//**************************************************************
// Definition of the Admin_Menu function. Displays the *
// administrator-based menu if user knows the code and password*
//**************************************************************


void Admin_Menu(){

system ("cls");
int choice = 0;

cout << "Hello! This is the administrator version";
cout << endl << "of the theatre program; mainly used for editing purposes." << endl << endl;
cout << "Below is a list of options the administrator can choose from:" << endl << endl;
cout << "1.\tEdit Seating Prices" << endl;
cout << "2.\tEdit Seating" << endl;
cout << "3.\tPurchase History" << endl;
cout << "4.\tQuit to Original" << endl;
cout << "5.\tQuit Program" << endl << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;

while (choice < 1 || choice > 5){
cout << endl << "You have entered an invalid choice!" << endl;
cout << "Enter a choice... (1-5): ";
cin >> choice;
}

switch (choice) {
case 1:
edit_seating_prices();

case 2:
edit_seating();

case 3:
purchase_history_admin();

case 4:
quit_to_original();

case 5:
quit();
}
}



//**************************************************************
// Definition of the edit_seating function. Administrator-only *
// function to edit the seating chart *
//**************************************************************



void edit_seating(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}



//**************************************************************
// Definition of the edit_seating function. Administrator-only *
// function to edit the prices of the seats. *
// This will in turn, overwrite SeatPrices.txt *
//**************************************************************



void edit_seating_prices(){ //Not finished
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}



//**************************************************************
// Definition of the purchase_history_admin function. *
// Administrator-only function designed to show admin the *
// total sum of ticket sales... NOT EDITABLE *
//**************************************************************


void purchase_history_admin(){
system ("cls");
system ("pause");
system ("cls");
Admin_Menu();
}



//**************************************************************
// Definition of the quit_to_original function. Administrator- *
// only function designed to return admin to original program *
//**************************************************************


void quit_to_original(){
system ("cls");
Show_Menu();
}




//**************************************************************
// Definition of the read_file function. Reads SeatPrices.txt *
// and stores the pre-determined prices into a vector named *
// prices. *
//**************************************************************



int readPrices(string myFile, vector<double>& vect) {

//input file
ifstream SeatPrices;

SeatPrices.open(myFile.c_str());


//if file cannot be found
if (!SeatPrices)
cout << "Cannot find the file!" << endl;


for (int index = 0; index < vect.size(); index++){
SeatPrices >> vect[index]; //Reading the file "SeatPrices.txt"
}

SeatPrices.close(); //Closes the file
return 0;
}




//**************************************************************
// Definition of the readSeating function. Reads a text file *
// with a seating chart in it. *
//**************************************************************



int readSeating(char myFile, vector<char>& vect){ //Not EVEN CLOSE TO FINISHING


//input file
ifstream SeatingChart;
SeatingChart.open(myFile);

//if file cannot be found
if (!SeatingChart)
cout << "Cannot find the file!" << endl;

for (int index = 0; index < vect.size(); index++){
SeatingChart >> vect[index]; //Reading the file "SeatingChart.txt"
}

SeatingChart.close(); //Closes the file
return 0;
}








//**************************************************************
// Definition of the Title_Card function. Starts the program *
// with a title card, showing a little introductory title *
//**************************************************************


void Title_Card(){
cout << endl << endl << endl << endl;
cout << "\t\t" << "************************************************\n";
cout << "\t\t" << "* THEATER SEATING! *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* A program created by a team of three *\n";
cout << "\t\t" << "* students to help small theaters sell *\n";
cout << "\t\t" << "* more tickets *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* Team of Students: *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "* ************* *\n";
cout << "\t\t" << "* *********** *\n";
cout << "\t\t" << "* ************* *\n";
cout << "\t\t" << "* *\n";
cout << "\t\t" << "************************************************\n";
cout << endl << endl;
system ("pause");
system ("cls");
}

最佳答案

首先(我认为这是你打算写的)

int readSeating (char, vector<char>&);

应该是

int readSeating (char*, vector<char>&);
^^^

第一个参数需要一个左值。这意味着它可以简单地分配给某些东西。

不管你怎么调用它

readSeating("SeatingChart.txt", seating);

通过为第一个参数传递一个临时变量。编译器想知道你不会修改它是为了让你通过一个临时的。您可以通过声明函数来告诉它您不会修改它。

int readSeating (const char*, vector<char>&);
^^^^

同样适用

int readSeating(char myFile, vector<char>& vect)

int readSeating(const char* myFile, vector<char>& vect)

我建议您继续将它们更改为字符串

int readSeating (string, vector<char>&);
int readSeating(string myFile, vector<char>& vect)

关于c++ - 从 'const char*' 到 'char' 的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16099731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com