-2

I am new to c++ and I need use my functions to store multiple user inputs and then print multiple user inputs. However, my code only does one user input. Should I store it in an array? It is self-explanatory code, so you can skip to the bottom and look for the "??storeinvariable??"

#include <iostream>

using namespace std;

// Will display all bids
void displayBid(string &bidTitle, string &fundPerson, string &vehicleId, float &bidAmount)
{
cout << "Title: " << bidTitle << endl;
cout << "Fund: " << fundPerson << endl;
cout << "Vehicle: " << vehicleId << endl;
cout << "Bid Amount: " << bidAmount << endl;
}

// Ask the user for title, person, vehicleId, amount
void getBid(string &bidTitle, string &fundPerson, string &vehicleId, float &bidAmount)
{
cout << "Enter Title: ";
cin.ignore();
getline(cin, bidTitle);
cout << "Enter Fund";
cin.ignore();
getline(cin, fundPerson);
cout << "Enter Vehicle ID: ";
cin.ignore();
getline(cin, vehicleId);
cout << "Enter Bid Amount: ";
cin.ignore();
cin >> bidAmount;
}

// Loops through adding bids or displaying bids
main(void)
{
  string title, person, id;
  title = "";
  person = "";
  id = "";
  float amount;
  amount = 0.0;

  int choice = 0;
    while (choice != 9) {
        cout << "Menu:" << endl;
        cout << "  1. Enter Bid" << endl;
        cout << "  2. Display Bid" << endl;
        cout << "  9. Exit" << endl;
        cout << "Enter choice: ";
        cin >> choice;

        switch (choice) {
            case 1:
                ??storeinvariable?? = getBid(title, person, id, amount);
                break;
            case 2:
                // display variable??
                displayBid(title, person, id, amount);
                 break;
        }
    }

    cout << "Good bye." << endl;

  return 1;
}
  • Take a look on `std::vector` – DimChtz May 12 '18 at 15:40
  • My advice is to read the basic c++ tutorials before you continue. Do you know what a class or structure is? Also, if by array you mean a raw array, read about the std containers, like std::vector. – Aziuth May 12 '18 at 15:40
  • [Rubber Ducky](https://en.wikipedia.org/wiki/Rubber_duck_debugging) would like to know why you are ignoring the first character of every line. – user4581301 May 12 '18 at 15:47
  • It's hard to tell what is and isn't a good tutorial unless you already have a good grounding in the language. [Until then Books rule](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 May 12 '18 at 15:50
  • Thanks everyone! I know of structs so I will try to use them. This is week one for the course so there's a lot of information that slips by me. Also, I ignore the first character of each line because without out the switch block combines my first two user input requests. – chase dougherty May 12 '18 at 16:02
  • 1
    The skip is because `cin >> choice;` leaves a newline in the stream. The `ignore` is best placed after the read that leaves data that must be ignored. By placing the `ignore` before a read you separate the cause and effect, making the program more difficult to debug and reduce the reusability of the `getBid` function. It MUST have a character in the stream before entering, and this may not always be the case. Plus `getline` consumes and discards the newline for you, so `getline(cin, bidTitle); cin.ignore(); getline(cin, fundPerson);` eats the first character of `fundPerson`. – user4581301 May 12 '18 at 18:30

1 Answers1

1

You could use an array for this. However, it would have to contain two different data types. You could make an array or vector of void*s, but this isn't the best solution.

You could create your own type (perhaps a struct) to store the data entered by the user like so:

typedef struct {
    std::string bidTitle;
    std::string fundPerson;
    std::string vehicleId;
    float bidAmount;
} Bid;

Once you've defined the struct, you would have your functions use this data type.