将字符串添加到2D数组并显示COUT消息(没有错误消息?)的错误
Error with adding string to 2d Array and displaying cout message (No error messages?)
我是这个论坛的新手。任何关于代码,布局或其他相关内容的构建性 critisive 非常欢迎!
我遇到的问题是(如标题所建议的)在您的标准座位保留程序中将fullName
字符串添加到数组seatArray
。
我遇到的错误是一旦编译,'please enter your second name'
之后就没有输出。当我编译时,调试器中没有出现错误。我还假设这意味着我的数组seatArray
没有任何添加到我的数组中,所以如果您至少可以将我指向正确的方向,我会感激不已!
nb:所有类都是单独的标头文件
首先,我的main.cpp文件
// Main class - Scotia2
// Main cpp file
#include <iostream>
#include "menu.h"
using namespace std;
int main()
{
Menu m;
m.userMenu();
return 0;
};
我的Menu
类(我不确定这与问题有关,但无论如何我都会包括)
// Menu class - Scotia2
#include <iostream>
#include "flight.h"
using namespace std;
class Menu
{
// Access specifier
public:
int userMenu()
{
Flight f1;
Booking b1;
int option;
cout << "Scotia Airlinesn";
cout << "n 1- Check Availability";
cout << "n 2- Book a Seat";
cout << "n 3- Cancel a Reservation";
cout << "n 4- Exit n ";
cout << "-------------------nn ";
cin >> option;
do{
switch (option)
{
case 1: f1.seatPlan();
break;
case 2: b1.addBooking();
break;
//case 3: c1.cancel; // Link to cancel.h
//break;
case 4: return (0);
break;
}
}while (option == 0); // End of do-while
return option;
cout << "n";
}// End of userMenu function
};// End of menu class
我的Flight
类:
// Flight Class - Scotia 2
// Contains information on seating (array), space available and return to menu option.
#include <iostream>
#include <string>
#include "booking.h"
using namespace std;
class Flight
{
public:
public:
struct Seat
{
int Available;
string fullName;
};// End of struct
// Structure for seat plan (24 spaces available)
struct Seat seatArray[4][6];
void seatPlan()
{ //------
cout << "Scotia Airlines Seating Plann";
cout << "------------------------n";
cout << " 1D 2D 3D 4D 5D 6Dn";
cout << " 1C 2C 3C 4C 5C 6Cn";
cout << " n";
cout << " 1B 2B 3B 4B 5B 6Bn";
cout << " 1A 2A 3A 4A 5A 6An";
cout << "------------------------nnn";
//------
for (int i=0;i<4;i++)
{
for (int j=0;j<6;j++)
{
if (seatArray[i][j].Available == 0)
cout << seatArray[i][j].fullName;
else
cout << "Seating Plan is unavailable";
}
}// End of for loop
}// End of seatPlan function
};// End of Flight class
最后,我的Booking
课程。我假设这是发生错误的地方。我尝试以不同的方式将代码列出,因为我认为事情可能没有按正确的顺序执行。但是,这并没有真正起作用。
//Booking class - Scotia Airlines
//This class will reserve a seat for passenger
#include <iostream>
#include <string>
using namespace std;
class Booking{
public:
struct Seat
{
int Available;
string fullName;
};// End of struct
// Structure for seat plan (24 spaces available)
struct Seat seatArray[4][6];
//variables for taking in customer details, calculating ticket cost (inc discounts) and adding details to system
public:
string fName, sName, busName, fullName;
int age, livesAt;
float discount, tickPrice, tCost;
void addBooking()
{
cout << "tBooking Menu nn";
cout << "Please select ticket type: n";
cout << "1- Business n";
cout << "2- Western Isles n";
cout << "3- Ordinary n";
cin >> livesAt;
// This will be used to calc total cost for each passenger dependant on ticket type
if(livesAt == 1)
{
discount = 0.75;
cout << "Please enter your business namen";
cin >> busName;
}
else if (livesAt == 2)
{
discount = 0.90;
}
else
{
discount = 0.0;
}
// Calculation - Standard ticket price is 60
tickPrice = 60.0;
tCost = (tickPrice * discount);
//Create full name for pass to asign to seat
fullName = fName + " " +sName;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 6; j++)
{
if(seatArray[i][j].Available == 1)
{
cout << "Please enter your first name n";
cin >> fName;
cout << "Please enter your second name n";
cin >> sName;
cin >> seatArray[i][j].fullName;
seatArray[i][j].Available = 0;
}// end of if
}// end of nested for
};//end of for
// Message on screen for customer displaying cost of flight
cout << "*******************************n";
cout << "tBooking for " << fullName << " confirmedn";
cout << "tTotal cost = " << tCost << " GBP.n";
}// End of addBooking function
};// End of Booking class
我知道代码可能有点草率(相当新手的程序员),因此,如我所说,任何提示等都非常欢迎!#
善意,
zm
取第二个名称(cin >> sName;
)后,您将再次使用cin >> seatArray[i][j].fullName;
提示用户。您是说要做以下操作吗?
cout << "Please enter your first name n";
cin >> fName;
cout << "Please enter your second name n";
cin >> sName;
fullName = = fName + " " + sName;
seatArray[i][j].fullName = fullName;
此外,一旦输入第二个名称,循环将继续,并将提示下一个可用座位的名称。您需要设置一个条件,一旦预订了座位,就可以突破两个for
循环。您可以使用以下操作来做到这一点:
bool booked = false;
for(int i = 0; i < 4 && !booked; i++)
{
for(int j = 0; j < 6 && !booked; j++)
{
if(seatArray[i][j].Available == 1)
{
cout << "Please enter your first name n";
cin >> fName;
cout << "Please enter your second name n";
cin >> sName;
fullName = = fName + " " + sName;
seatArray[i][j].fullName = fullName;
booked = true;
}
}
}
cout << "Please enter your first name n";
cin >> fName;
cout << "Please enter your second name n";
cin >> sName;
cin >> seatArray[i][j].fullName;
这将:
- 提示
- 在局部变量fname
- 提示第二个名字
- 在局部变量sname
- 读取另一个 whitespace-delimited字符串到
seatArray[i][j].fullName
这些读物中的任何一个都会等待您键入某些内容,因此,一旦您输入第二个名称,它将默默地返回到fullName
。
您已经有此代码:
//Create full name for pass to asign to seat
fullName = fName + " " +sName;
但是是在循环之前,当fName
和sName
尚未分配任何值时。
回答"请输入您的第二个名字"后,您的程序仍在等待输入:
cin >> seatArray[i][j].fullName;
您试图用这一行做什么:
cin >> seatArray[i][j].fullName;
为什么用户已经输入和姓氏后输入全名?也许我误会了。正如@Ahenderson指出的那样,只需在您的.h文件中声明安全平面并在.cc中定义方法。否则,它是内衬的,这意味着它将在项目中包含该.H文件的任何地方复制代码。从空间的角度效率低下。通常,它可以很好地对内联功能,仅返回某些或进行很小的计算的功能。
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 我的代码中有错误,未声明的标识符
- C++ LeetCode #377 的 DP 解决方案中,此代码是否有错误?
- 试图建立银行管理系统,但有错误
- 我试图创建临时对象的方式有错误吗
- 我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误
- 我在OpenCV中有错误的"approxPolyDP(ROI_Vertices, ROI_Poly, 1.0, true)"
- C++ 乘法定义的构造函数错误消息似乎错误
- 我在这个 C++ 代码上有错误吗?
- 是视觉C++优化器错误还是我的代码中有错误?
- 为什么一个简单的C++程序会有错误的结果?是 #define 引起的吗?
- 对此有错误
- C :数据文件有错误:预期的无限制ID
- 一个带有参数的程序,为什么此程序有错误的答案
- 使用 C/C++ 获取错误消息MSB6006错误代码 2
- 仅使用boost.datetime在Windows上有错误
- 使用向量循环时,是否有错误
- Microsoft Visual Studio发现我所有的CUDA设备端函数都有错误
- 继承默认构造函数在 gcc 中失败并在 clang 中工作,哪个有错误?
- 为什么从不同分配器的列表中剪辑列表元素时会有错误消息?以及如何解决