Reputation: 19
When the program reaches a function-call the program seems to just ignore it and move on. The result is an infinite loop without allowing for the input of items or prices.
I'm pretty new to C++, so I'm still learning the basics of its syntax. If anyone could spot what I've done wrong and or explain it I would be grateful for the insight.
#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.\n\n\n";
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: \n\n"
<< item1 << " $" << item1Price << "\n"
<< item2 << " $" << item2Price << "\n"
<< item3 << " $" << item3Price << "\n"
<< item4 << " $" << item4Price << "\n"
<< item5 << " $" << item5Price << "\n";
return;
}
I am trying to great a loop that involves calling two functions and then askes the user if s/he would like to run it again. If not, the program should run one final function that outputs the results.
Upvotes: 0
Views: 129
Reputation: 1180
You have some problems:
1> In while loop, you only declared some functions, your don't execute them.
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
}
For executing them, you have to get the return value from them. For example:
To executer addItem(), itemPriceDisplay(), you need to do:
string result = addItem();
int price = addPrice();
itemPriceDisplay();
2> Function addItem() return string but you always return 0 only.
Upvotes: 1
Reputation: 56068
These lines:
string addItems();
int addPrice();
void itemPriceDisplay();
do not call anything. They result in no executable code. What they do is tell the compiler that a function exists. The first is saying "Hey, compiler, there's a function named addItems
that returns a string
and takes no parameters. Similarly for the other two.
A function call is never preceded by a type name like that.
Try changing the line that says string addItems();
to just say addItems();
instead. That should result in the function addItems
being called.
Now, the places where you're declaring functions named addPrice
and itemPriceDisplay
are not so trivially converted to function calls. According to the declarations up above, those functions take parameters, values that tell them what to do in detail. You don't have any obvious parameters to feed them.
Personally, looking at the rest of your program, I think you're suffering from some very profound misunderstandings about what functions are and how to call them. I think it's beyond the scope of a StackOverflow answer to explain them all to you.
Upvotes: 0