1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
// So when i run a program, it runs but when it gets to reading in a value for
// cin it just ends the program without reading it. I was wondering why
// because i cant seem to figure it out.
#include <iostream>
#include <string>
void update(std::string product[], int qnty[], double amount[], int &count);
int nextIndex(std::string productID[], int &x);
int main()
{
std::string products[] { "alpha", "beta" };
int quantities[] { 10, 20 };
double amounts[] { 13, 666 };
int mycount {};
update(products, quantities, amounts, mycount);
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
void update(std::string product[], int qnty[], double amount[], int &count)
{
std::cout<< "Type in Product ID of the phone you want to update: ";
int id {};
std::cin >> id;
std::cin.ignore(1); // get rid of the following '\n'
int i = nextIndex(product, count); // nextIndex() shouldn't be findProduct()?
std::cout << "Type p to edit price or q to change the quantity: ";
char ans {};
std::cin >> ans;
std::cin.ignore(1); // get rid of the following '\n'
}
// What's the point of passing x as reference if it's not modified inside the function?
int nextIndex(std::string productID[], int &x)
{
int i {};
for(; i<x; i++) {
// I think what you really want is if(productID[i] != "")
if(productID[i] == "") { // if(productID[i].empty()) ?
return i;
}
}
// what if your if-statement doesn't match anything inside productID[]?
}
|