从'const char *'到'char'的转换无效

时间:2013-04-19 07:43:13

标签: c++ char argument-passing

我目前正在制作一个杂项剧场座位计划!我刚刚开始通过C ++进步,但我无法弄清楚我继续编写的这个错误。我目前正在尝试编码的是一种逐字符输入“*”字符的方法,最终帮助我构建一个功能来显示电影院的座位表。我非常接近完成名为readSeating的函数...虽然它有一些明显的问题。通常我可以非常有效地调试我的程序,但我只是想不出这个。请查看我的代码,了解我的程序,让我有任何想法。请保持答案有点简单;再说一遍,我不是C ++的高手

P.S。

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

/* 
* 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");
 }

2 个答案:

答案 0 :(得分:2)

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

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

应该是

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

期望第一个参数lvalue。这意味着它可以分配给它的东西。

但是你将其称为

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)

我建议您继续将其更改为strings

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

答案 1 :(得分:1)

为你修好了。字符串文字(例如:“somestring”)的类型为“char const *”。它不能被视为一个字符。

/* 
* 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 (const 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(const 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");
 }