我的C++函数似乎没有被调用

My C++ Functions do not seem to be called

本文关键字:调用 C++ 函数 我的      更新时间:2023-10-16

当程序到达一个函数调用时,程序似乎只是忽略它并继续前进。结果是一个无限循环,不允许输入项目或价格。

我对C++还很陌生,所以我还在学习它的语法基础。如果有人能发现我做错了什么,或者解释一下,我将感谢你的洞察力。

#include <iostream>
#include <string>
using namespace std;
void itemPriceDisplay(string item1, string item2, string item3, string item4, string item5, int item1Price, int item2Price, int item3Price, int item4Price, int item5Price);
string addItems();
int addPrice(string item1, string item2, string item3, string item4, string item5);
int main()
{
//////////////
// Variables//
//// Below////
string moreItems = "Y";
//////////////
// Variables//
////Above////
std::cout << "This program will display a list of inputed items their prices, and then calculate the total and tax.nnn";
while( (moreItems == "Y") || (moreItems == "y") )
{
string addItems();
int addPrice();
cout << "Would you like to list another item? (Y/N)";
cin >> moreItems;
void itemPriceDisplay();
}
return 0;
}
string addItems()
{
string item1;
string item2;
string item3;
string item4;
string item5;
cout << "Enter the name of the first item: ";
cin >> item1;
cout << "Enter the name of the second item: ";
cin >> item2;
cout << "Enter the name of the third item: ";
cin >> item3;
cout << "Enter the name of the fourth item: ";
cin >> item4;
cout << "Enter the name of the fith item: ";
cin >> item5;
return 0;
}

int addPrice(string item1, string item2, string item3, string item4, string item5)
{
int item1Price;
int item2Price;
int item3Price;
int item4Price;
int item5Price;
cout << "Enter the price of the " << item1 << ":  ";
cin >> item1Price;
cout << "Enter the price of the " << item2 << ":  ";
cin >> item2Price;
cout << "Enter the price of the " << item3 << ":  ";
cin >> item3Price;
cout << "Enter the price of the " << item4 << ":  ";
cin >> item4Price;
cout << "Enter the price of the " << item5 << ":  ";
cin >> item5Price;
return 0;
}
void itemPriceDisplay(string item1, string item2, string item3, string item4, string item5, int item1Price, int item2Price, int item3Price, int item4Price, int item5Price)
{
// List items and their price.
cout << "n Here is a list of your entered items and their prices: nn"
<< item1 << "        $" << item1Price << "n"
<< item2 << "        $" << item2Price << "n"
<< item3 << "        $" << item3Price << "n"
<< item4 << "        $" << item4Price << "n"
<< item5 << "        $" << item5Price << "n";
return;
}

我正在尝试创建一个涉及调用两个函数的循环,然后询问用户是否愿意再次运行它。如果没有,程序应该运行输出结果的最后一个函数。

您遇到了一些问题:

1> 在while循环中,您只声明了一些函数,而不执行它们。

while( (moreItems == "Y") || (moreItems == "y") )
{
string addItems();   // Only Declared the function addItem(), don't execute it
int addPrice();      // Only declared the function addPrice(), don't execute it
cout << "Would you like to list another item? (Y/N)";
cin >> moreItems;
void itemPriceDisplay(); // Only declared the function itemPriceDisplay, don't execute it
}

为了执行它们,您必须从中获取返回值。例如:

要执行addItem(),itemPriceDisplay(),您需要执行以下操作:

string result = addItem();
int price = addPrice();
itemPriceDisplay();

2> 函数addItem()返回字符串,但始终只返回0。

这些行:

string addItems();
int addPrice();
void itemPriceDisplay();

不要叫任何东西。它们不会产生可执行代码。他们所做的就是告诉编译器函数的存在。第一个是说"嘿,编译器,有一个名为addItems的函数,它返回一个string,不接受任何参数

函数调用的前面永远不会有这样的类型名。

试着把写string addItems();的行改为只写addItems();。这将导致调用函数addItems

现在,声明名为addPriceitemPriceDisplay的函数的地方并没有那么简单地转换为函数调用。根据上面的声明,这些函数接受参数,这些值告诉他们要做什么。你没有任何明显的参数来喂养它们。

就我个人而言,看看你的程序的其余部分,我认为你对函数是什么以及如何调用它们有一些非常深刻的误解。我认为向您解释它们已经超出了StackOverflow答案的范围。