gpt4 book ai didi

c++ - 如何获取 const* char 参数的键盘输入?

转载 作者:行者123 更新时间:2023-11-30 04:32:03 51 4
gpt4 key购买 nike

我有一个对象可以处理较小对象的数组。我现在正在创建一个更大的接口(interface)对象,它需要收集输入数据并将其发送到 const* char 的参数中。我将使用什么代码来捕获诸如 20 个字符的标题之类的键盘输入并能够将其传递到此参数中?

简而言之:
你如何获得名称的键盘输入并将其传递给这个:

void Insert(const char* t)

我只能使用 iostream、iomanip、cstring、cctype 库

编辑:您要求提供完整的代码,所以就在这里。我所有的输入都有问题...

#include <iostream>
#include "store.h"

using namespace std;

void ShowMenu()
// Display the main program menu.
{
cout << "\n\t\t*** BOOKSTORE MENU ***";
cout << "\n\tA \tAdd a Book to Inventory";
cout << "\n\tF \tFind a book from Inventory";
cout << "\n\tS \tSell a book";
cout << "\n\tD \tDisplay the inventory list";
cout << "\n\tG \tGenre summary";
cout << "\n\tO \tSort inventory list";
cout << "\n\tM \tShow this Menu";
cout << "\n\tX \teXit Program";
}

char GetAChar(const char* promptString)
// Prompt the user and get a single character,
// discarding the Return character.
// Used in GetCommand.
{
char response;// the char to be returned

cout << promptString;// Prompt the user
cin >> response;// Get a char,
response = toupper(response);// and convert it to uppercase
cin.get();// Discard newline char from input.
return response;
}

char Legal(char c)
// Determine if a particular character, c, corresponds
// to a legal menu command. Returns 1 if legal, 0 if not.
// Used in GetCommand.
{
return((c == 'A') || (c == 'F') || (c == 'S') ||
(c == 'D') || (c == 'G') || (c == 'O') ||
(c == 'M') || (c == 'X'));
}

char GetCommand()
// Prompts the user for a menu command until a legal
// command character is entered. Return the command character.
// Calls GetAChar, Legal, ShowMenu.
{
char cmd = GetAChar("\n\n>");// Get a command character.

while (!Legal(cmd))// As long as it's not a legal command,
{// display menu and try again.
cout << "\nIllegal command, please try again . . .";
ShowMenu();
cmd = GetAChar("\n\n>");
}
return cmd;
}

void Add(Store s)
{
char* aTitle;
char aAuthor[21];
Genre aGenre = FICTION;
double aPrice = 10.00;

cout << "Enter title: ";
cin >> aTitle;

cout << "Enter author: ";
cin.getline(aAuthor, 20);

cout << aTitle << " " << "aAuthor\n";

s.Insert(aTitle, aAuthor, aGenre, aPrice);
}

void Find()
{
}

void Sell()
{
}

void Genre()
{
}

void Sort()
{
}

void Intro(Store s)
{
double amount;

cout << "*** Welcome to Bookstore Inventory Manager ***\n"
<< "Please input the starting money in the cash register: ";
cin >> amount;

s.SetCashRegister(amount);
}

int main()
{
Store mainStore;// Create and initialize a Store.

Intro(mainStore);//Display intro & set Cash Regsiter

ShowMenu();// Display the menu.

mainStore.Insert("A Clockwork Orange", "Anthony Burgess", SCIFI, 30.25);
mainStore.Insert("X-Factor", "Anthony Burgess", SCIFI, 30.25);

char command;// menu command entered by user
do
{
command = GetCommand();// Retrieve a command.
switch (command)
{
case 'A': Add(mainStore); break;
case 'F': Find(); break;
case 'S': Sell(); break;
case 'D': mainStore.DisplayStore(); break;
case 'G': Genre(); break;
case 'O': Sort(); break;
case 'M': ShowMenu(); break;
case 'X': break;
}
} while ((command != 'X'));

return 0;
}

最佳答案

考虑 std::istream::getline(char *, std::streamsize) .

但是,请确保向它传递一个指向已分配内存的有效指针!也就是说,像这样使用它:

char buffer[80];
std::cin.getline(buffer, sizeof buffer);

并且是这样的:

char *p;
std::cin.getline(p, 80); // Undefined behavior: using uninitialized variable


编辑。你有这个代码:

char* aTitle;
...
cout << "Enter title: ";
cin >> aTitle;

这是一个错误。您创建了一个名为 aTitle 的指针,但没有对其进行初始化。该指针现在指向您不拥有的内存。

>>> 运算符将数据写入指针指向的位置。由于您的指针不指向您控制的任何内容,因此 >>> 在通过您的指针写入时将调用未定义的行为。

教训:确保为所有指针提供有效值。 (更广泛的教训:永远不要使用指针。(好吧,几乎从不。))

紧随其后,您将获得以下代码:

cout << "Enter author: ";
cin.getline(aAuthor, 20);

但是,请考虑输入状态。您的用户刚刚键入“Jaws”,然后按 ENTER。您的 cin>>aTitle 读取“Jaws”,并在输入流中留下“\n”。

这个 istream::getline 调用读取到第一个换行符,这是跟在“Jaws”(!) 之后的换行符,不是跟在“Peter”之后的换行符本奇利”!所以现在,您在 aTitle 中有“Jaws”(假设您修复了之前的错误),在 aAuthor 中没有,并且“Peter Benchley\n”仍在输入流中。

教训:不要将格式化输入与 getline 混合使用。在整个程序中始终使用其中一个。

关于c++ - 如何获取 const* char 参数的键盘输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7934440/

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