Hello, currently new to C++ and learning the ropes.
I'm trying to create a simple database-like program, but the following code doesn't output any meaningful data. The program compiles fine, though.

The rest of the program is still under work, so I'm just posting the relevant parts which I think are related to the problem:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//Entering protofunctions first
void UserVerif ();
void UserCreate();
void PayVerif ();
void NameVerif();
void OrderTake ();
void OrderVerif (); 

class ProdRecord
{
      public:
      string name;
      float price;
      int stock;
      ProdRecord(string name, float price, int stock)
      {}  
};

void OrderTake()
{
       int ordertin;
       vector<ProdRecord>products;
       
       products.push_back(ProdRecord("Diablo 3", 150.00, 0));
       products.push_back(ProdRecord("Call of Duty 5", 150.00, 20));
       products.push_back(ProdRecord("Starcraft 3", 150.00, 30));
       products.push_back(ProdRecord("Battlefield 4", 150.00, 30));
       products.push_back(ProdRecord("Warcraft 4", 150.00, 25));
       products.push_back(ProdRecord("Catz 'N Dogz", 50.00, 100));
     
       system("cls");
       cout << "Please select your desired product:" << endl;
       cout << "1. " << products[0].name << "RM" << products[0].price << "STOCK= " << products[0].stock << endl;
       cout << "2. " << products[1].name << "RM" << products[1].price << "STOCK= " << products[1].stock << endl;
       cout << "3. " << products[2].name << "RM" << products[2].price << "STOCK= " << products[2].stock << endl;
       cout << "4. " << products[3].name << "RM" << products[3].price << "STOCK= " << products[3].stock << endl;
       cout << "5. " << products[4].name << "RM" << products[4].price << "STOCK= " << products[4].stock << endl;
       cout << "6. " << products[5].name << "RM" << products[5].price << "STOCK= " << products[5].stock << endl;
       cout << "Please enter 1-6 for the product of your choice." << endl;
       cin >> ordertin;
}

int main()
{
    OrderTake ();
    
    system ("pause");
    return 0;
}

Dani AI

Generated

Good catch by — the program’s visible “no meaningful data” symptom comes from the records not being given real values before printing. Initializing data members in the constructor is the correct fix; the member-initializer list is the idiomatic and efficient way to do that in C++ (it’s also required for const or reference members). See the language reference for constructors for details and edge cases: Constructors (cppreference).

A few practical follow-ups and improvements for this small database program:

  • Print the vector contents with a loop instead of hard-coding indexes. Use a range-based loop or an indexed loop and format the price with std::fixed + std::setprecision(2) so money looks right. See setprecision docs: std::setprecision.
  • Construct records in place with emplace_back and call reserve if you know how many items you'll have; this avoids extra copies: vector::emplace_back and vector::reserve.
  • Validate user input and check bounds (use products.size() or at() which throws) before accessing an index. Don’t assume the console won’t receive bad input.

Example patterns to adopt (not repeating the exact code above): use a simple loop to display all elements; validate the selected index in a loop until it’s inside 1..products.size(); and prefer const std::string& parameter types for constructors to avoid unnecessary copies.

Finally, watch for two common pitfalls: (1) shadowing — if constructor parameters have the same names as members, prefer the initializer list or this-> to avoid accidentally assigning a parameter to itself; (2) avoid system("cls")/system("pause") for portability and safety. After applying ’s initialization hint and the checks above, ’s confirmation that it worked is expected.

Recommended Answers

All 2 Replies

Hello, currently new to C++ and learning the ropes.
I'm trying to create a simple database-like program, but the following code doesn't output any meaningful data. The program compiles fine, though.

The rest of the program is still under work, so I'm just posting the relevant parts which I think are related to the problem:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//Entering protofunctions first
void UserVerif ();
void UserCreate();
void PayVerif ();
void NameVerif();
void OrderTake ();
void OrderVerif (); 

class ProdRecord
{
      public:
      string name;
      float price;
      int stock;
      [CODE]ProdRecord(string name, float price, int stock)
      {}  [/CODE]};

void OrderTake()
{
       int ordertin;
       vector<ProdRecord>products;

       products.push_back(ProdRecord("Diablo 3", 150.00, 0));
       products.push_back(ProdRecord("Call of Duty 5", 150.00, 20));
       products.push_back(ProdRecord("Starcraft 3", 150.00, 30));
       products.push_back(ProdRecord("Battlefield 4", 150.00, 30));
       products.push_back(ProdRecord("Warcraft 4", 150.00, 25));
       products.push_back(ProdRecord("Catz 'N Dogz", 50.00, 100));

       system("cls");
       cout << "Please select your desired product:" << endl;
       cout << "1. " << products[0].name << "RM" << products[0].price << "STOCK= " << products[0].stock << endl;
       cout << "2. " << products[1].name << "RM" << products[1].price << "STOCK= " << products[1].stock << endl;
       cout << "3. " << products[2].name << "RM" << products[2].price << "STOCK= " << products[2].stock << endl;
       cout << "4. " << products[3].name << "RM" << products[3].price << "STOCK= " << products[3].stock << endl;
       cout << "5. " << products[4].name << "RM" << products[4].price << "STOCK= " << products[4].stock << endl;
       cout << "6. " << products[5].name << "RM" << products[5].price << "STOCK= " << products[5].stock << endl;
       cout << "Please enter 1-6 for the product of your choice." << endl;
       cin >> ordertin;
}

int main()
{
    OrderTake ();

    system ("pause");
    return 0;
}           

end quote.

You forgot to initialize your variables in your class constructor. You need to do

ProdRecord(string n, float p, int s)
      {nsme = n; price = p; stock = s;}  

or

ProdRecord(string n, float p, int s) : name(n), price(p), stock(s) 
      {}  

Ah yes, that worked. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.