WxWidgets and STL Vector - c++

So, I am new to programing and I have this problem.
I have written a console app in which I can add objects to STL vector.
Some code:
Element.h (Objects of this class I store in Vector)
class Element
{
private:
string speaker_name;
string color_membrane;
string color_main;
string size_main;
string color_terminals;
string size_terminals;
public:
Element();
~Element();
string Get_color_membrane();
string Get_color_main();
string Get_size_main();
string Get_color_terminals();
string Get_size_terminals();
string Get_speaker_name();
void Set_color_membrane(string);
void Set_color_main(string);
void Set_size_main(string);
void Set_color_terminals(string);
void Set_size_terminals(string);
void Set_speaker_name(string);
Doc.cpp (class with vector)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "Doc.h"
using namespace std;
vector <Element> MyElements;
void Doc::Add_item(Element const& TempElement)
{
MyElements.push_back(TempElement);
}
App.cpp
Doc doc;
Element TempElement;
cout << "Add new Element" << endl;
cout << "Input NAME of speaker" << endl;
cin >> temp_string;
TempElement.Set_speaker_name(temp_string);
cout << "Input COLOR of membrane" << endl;
cin >> temp_string;
TempElement.Set_color_membrane(temp_string);
cout << "Input COLOR of main body of speaker " << endl;
cin >> temp_string;
TempElement.Set_color_main(temp_string);
cout << "Input stroke SIZE of membrane & main body" << endl;
cin >> temp_string;
TempElement.Set_size_main(temp_string);
cout << "Input COLOR of terminals" << endl;
cin >> temp_string;
TempElement.Set_color_terminals(temp_string);
cout << "Input stroke SIZE of terminals" << endl;
cin >> temp_string;
TempElement.Set_size_terminals(temp_string);
doc.Add_item(TempElement);
The thing is: I am writing a window-based app in WxWidgets.
My project looks like this:
(It's just for testing purposes - it does nothing)
And, now I would like to create a method of making some sort of list of this objects from STL vector (where red arrow points),
It should look more or less like this:
and be able to communicate with STL vector
Thanks in advance for all your help.
~Thermoos

The simplest control you can use to display your items is wxListBox. There is also wxEditableListBox which comes with the built-in buttons, but it doesn't look quite the same, check the editlbox sample showing it in action.

Related

C++ executes blanks after class creation

I'm trying to learn C++ to help my sibling with their assignment. So I'm attempting the assignment. It's a simple program to load a dictionary test file with words, their type, and definition to an array of Word type objects. I was able to get started with a normal string array instead of an object array as requested. But as soon as I defined the Word class and the array the code builds without an issue. When I try to run the code the cursor simply blinks for a few seconds and returns to the normal terminal.
Am I doing something wrong with my Class constructor ??
#include <fstream>
#include <string>
using namespace std;
class Word {
public:
string WordEntry;
string Type;
string Definition;
//constructor
Word(string word, string type, string definition){
WordEntry=word;
Type=type;
Definition=definition;
}
};
int main(){
cout << "Test1";
Word *wordArray[318555];
int count=0;
string word, type, definition,blank;
cout << "TEST" << count << "\n";
ifstream file("dictionary2021 (1).txt");
if (file.is_open()){
cout << "File dictionary2021.txt has been opened \n";
while (!file.eof()){
getline(file,word);
getline(file,type);
getline(file,definition);
getline(file,blank);
wordArray[count]= new Word(word,type,definition);
count++;
}
file.close();
cout << "File dictionary2021.txt has " << count/3 << " entries\n";
}
cout << "TEST" << count << endl;
cout << cc;
int selection;
string input;
cout << "Function List - Please hit Enter after your selection \n";
cout << " 1. Word Search \n 2. Repetitive z search \n 3. Wild Card Search\n";
cout << "Selection:";
cin >> selection;
if(selection=1){
cout << "Enter word:\n";
cin >> input;
string str("a");
for (int i = 0; i < 12; i+3)
{
cout << "1";
if (input.compare(str)== 0)
{
cout << wordArray[i+1];
return 0;
}
cout << "2";
}
}
}```
Word* wordArray[318555]; is a huge value and we're talking about 2548440 bytes (or roughly 2.4MB). This might be too large for a single stack frame and can easily be inefficient.
What I suggest is to use std::vector to store the word array and use std::vector<>::push_back() method to insert data to it.
Note: In your code snippet your not deallocating the Word object pointers once everything is done. Either explicitly delete those pointers using delete or use a smart pointer like std::unique_ptr.

How do i get a user to pick a number in c++

i am trying to make the user pick a number thats leads to a another code in c++ how do i do that?
cout << "Choose a text: "
getline(????)
1:
code number one
2.
text number 2
Pick a number?
I would assume you are talking about inputing a integer.
getline mostly is used for a string and spaces between each words, or a word.
For example if the input is like "Happy thanks giving"
the code would be like:
#include <iostream>
#include <string> //to use getline(cin, variable_name)
using namespace std;
int main()
{
string sentenceWithSpace;
cout << "get input" << endl;
getline(cin,sentenceWithSpace);
cout << sentenceWithSpace << endl;
system("pause"); // include this line if you use VS
return 0;
}
if the user is just inputing a value like 1,2,3,4,5,6
#include <iostream>
using namespace std;
int main()
{
int thisIsJustAInteger;
cout << "get input" << endl;
cin >> thisIsJustAInteger;
cout << thisIsJustAInteger << endl;
system("pause"); // include this line if you use VS
return 0;
}

My Vector is not printing despite print function [duplicate]

This question already has answers here:
How to pass objects to functions in C++?
(8 answers)
Closed 3 years ago.
I am working on a program that will establish a class of a Combo meal with specific parameters for the objects. I am storing these in a vector. The vector appears to be running appropriately but when I use the print function it is not printing anything. I need help getting the print function to operate appropriately.
I have tried using the .at() instead of getEntree() but still am not receiving any output. I have read several pieces on here about printing from a vector and still am unable to get any output from my program.
#include <iostream>
#include <string>
#include <vector>
#include "Combo.h"
void fillOrder(vector<Combo>);
void printOrder(vector<Combo>);
int main()
{
vector<Combo> myOrder;
fillOrder(myOrder);
printOrder(myOrder);
}
vector<Combo> newMyOrder;
void fillOrder(vector<Combo> newMyOrder) {
string entree;
string side;
string sideSize;
string drink;
string drinkSize;
cout << "How many meals would you like to order? ";
int orderSize;
cin >> orderSize;
for (int i=0; i < orderSize; i++) {
cout << "Would you like a Hamburger, Cheeseburger, or chicken?" << endl;
cin >> entree;
cout << "Would you like fries, tots, or a salad for your side?" << endl;
cin >> side;
cout << "What size would you like your side?" << endl;
cin >> sideSize;
cout << "What would you like to drink?" << endl;
cin >> drink;
cout << "What size drink would you like?" << endl;
cin >> drinkSize;
Combo newMeal(entree, side, sideSize, drink, drinkSize);
newMyOrder.push_back(newMeal);
}
}
void printOrder(vector<Combo>newMyCombo) {
unsigned int size = newMyCombo.size();
for (unsigned int i = 0; i < size; i++) {
cout << "Your Entree is : " << newMyCombo[i].getEntree()<<endl;
cout << "Your side is : " << newMyCombo[i].getSide()<<endl;
}
}
I am wanting this program to take the order then print a summary of the order, but I am getting no output at all.
Pass by reference
Try:
void fillOrder(vector<Combo>&);
void fillOrder(vector<Combo>& newMyOrder)
If you'd want the print function to not work on a copy (as it is just printing the orders) use void printOrder(const vector<Combo>&); instead. The const qualifier will give you a compile guarantee that a function that should not modify the container does not really do that. Passing this by reference to print will ensure that you don't waste time and space in copying the whole thing.

C++ Array not taking correct input from file

Disclaimer: I am a beginner to programming, so what I say might sound really stupid
I have to make a "Telephone Directory" for school. The program isn't complete, but there are some things that I need to fix before moving on. The array TelephoneNumbers either isn't storing the numbers from the file correctly, or isn't displaying them. For the SeaerchRecords function, the first number in the file is displayed correctly, the second is displayed as "2147483647," and the rest of the numbers display as "0." The modify function also doesn't change the number, and I confirmed this with the while in the function. The string array works perfectly fine, however. May someone explain what I'm doing incorrectly?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string TelephoneNames[100];
int TelephoneNumbers[100];
void ModifyRecords(); //Function to Modify Records
void SearchRecords(); //Function to Search Records
void DeleteRecords(); //Function to Delete Records
int main()
{
fstream inputFile;
fstream outputFile;
char choice;
inputFile.open("Telephone Names.txt"); //To store
for (int count=0;count<100;count++) //file names
{ //into a
inputFile >> TelephoneNames[count]; //string
}
inputFile.close();
inputFile.open("Telephone Numbers.txt");//To store
for (int count=0;count<100;count++) //file #'s
{ //into a
inputFile >> TelephoneNumbers[count];//string
}
inputFile.close();
//Display options available
cout << " Hello, do you want to:\n";
cout << " ======================\n";
cout << "-Modify Records|Enter M\n";
cout << "-Search Records|Enter S\n";
cout << "-Delete Records|Enter D\n";
//Store choice
cin >> choice;
//Send to different function
if (choice=='M'||choice=='m')
{
ModifyRecords();
}
if (choice=='S'||choice=='s')
{
SearchRecords();
}
return 0;
}
void ModifyRecords()
{
string name;
string newname;
int newnumber;
int count=0;
cout << "Enter the name of the person: ";
cin >> name;
for (count=0;TelephoneNames[count]!=name;count++)//To determine where in the strings the new numbers need to be
{
}
cout << "Enter the new name of the person: ";
cin >> newname;
cout << "Enter the new number of the person: ";
cin >> newnumber;
TelephoneNames[count]={newname};
TelephoneNumbers[count]={newnumber};
count=0;
while (count<6)
{
cout << TelephoneNames[count] << endl;
cout << TelephoneNumbers[count] << endl;
cout << endl;
count++;
}
}
void SearchRecords()
{
string name;
int count=0;
cout << "Enter the name of the person you would like to find: ";
cin >> name;
for (count=0;TelephoneNames[count]!=name;count++)//To determine where in the strings the new numbers need to be
{
}
cout << "Name: " << TelephoneNames[count] << endl;
cout << "Number: " << TelephoneNumbers[count] << endl;
}
Since there is no any answer still and I don't see exactly the problem at this point I'll provide some suggestions how you can find a problem in your code.
In any programming situation when you can't find a bug, first task is to locate it as much precisely as you can and check all input data and assumptions. Usually, debugger is used for such purposes, but you can just output text in console before creating final version of your program.
To start with, you must check that you really received names and telephones from your file:
inputFile.open("Telephone Names.txt"); //To store
for (int count=0;count<100;count++) //file names
{ //into a
inputFile >> TelephoneNames[count]; //string
cout << TelephoneNames[count] << endl; //WE MUST SEE WHAT IS REALLY STORED IN TelephoneNames
}
inputFile.close();
inputFile.open("Telephone Numbers.txt");//To store
for (int count=0;count<100;count++) //file #'s
{ //into a
inputFile >> TelephoneNumbers[count];//string
cout << TelephoneNumbers[count] << endl; //WE MUST SEE WHAT IS REALLY STORED IN TelephoneNumbers
}
inputFile.close();
Ok, when it is checked and you are defenitely sure there is no problem in your data we can move to SeaerchRecords function doing the same procedure. We must check what is happening while you are searching:
for (count=0;TelephoneNames[count]!=name;count++)//To determine where in the strings the new numbers need to be
{
cout << "Search step: " << count << " name " << name << " found name " << TelephoneNames[count] << " number " << TelephoneNumbers[count] << endl;
}
Doing so you will locate your bug rather quickly. The problem can be in input files format, in difference of "name" and stored names format etc.
I'll provide several additional suggestion how you can improve your code.
1) Try to use const declarations for such commonly used things as number of records (const int NUMBER_OF_RECORDS = 100; insted of just putting '100' everywhere), it will reduce the amout of work and possible bugs. 2) Try to check all possible problems that you program can encounter if someting is wrong with data. What will happen if you have less than 100 records in your files now? Program crush or silent reading of unappropriate data which is even worse. Check that you haven't reach file end on any step of reading along with current check that you've reached you number of records and do something in case of unappropriate data.
3) Check the possible problems with conditions in your cycles not to run them infinite number of times. Now your condition for(count=0;TelephoneNames[count]!=name;count++)
will execute forever if there is no such name or just crush the program on count 100 or more. You should check that count doesn't exceed that value. Good luck!

C++ pass empty vector of structs to a function

I am trying to pass an empty vector of structures to a function which will read from a file and it will return the number of records read -- it will be an integer.
I initialize the vector of structures in main and when I attempt to pass it to the function as I would regularly do it:
int read_records(vector<player> player_info)
It gives me a "player is undefined" error. I have found a way to go around it as you will see in my code below but logic leads me to believe that there should be a way to pass the empty vector without having to fill in the first subscript.
The code is below. Please note that the read function is not yet complete as I am still wondering about the vector of structs.
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
//function prototypes
int read_records(struct player* player_info);
/*
* Define a struct called player that will consist
* of the variables that are needed to read in each
* record for the players. 2 strings for the first
* and last names and 1 integer to hold the statistics
*/
struct player
{
string first;
string last;
int stats;
};
int main(void)
{
int sort_by, records_read;
vector<player> player_info(1);
player * point = &player_info[0];
cout << "Welcome to Baseball player statistics program!" << endl;
cout << "How should the information be sorted?" << endl;
cout << "Enter 1 for First Name" << endl;
cout << "Enter 2 for Last Name" << endl;
cout << "Enter 3 for Points" << endl;
cout << "Enter your selection: ";
cin >> sort_by;
//read the records into the array
records_read = read_records(point);
system("Pause");
return 0;
}
int read_records(struct player* player_info)
{
//declare the inputstream
ifstream inputfile;
//open the file
inputfile.open("points.txt");
//handle problem if the file fails to open for reading
if (inputfile.fail())
{
cout << "The player file has failed to open!" << endl;
exit(EXIT_FAILURE);
}
else
{
cout << "The player file has been read successfully!" << endl;
}
return 5;
}
Define the type player before you attempt to declare functions that need to know about that type.
struct player
{
string first;
string last;
int stats;
};
int read_records(vector<player> player_info);
Your workaround was successful because naming player in struct player* acts as a [forward] declaration, in a way that naming it in vector<player> does not. (The whys and wherefores of this are too broad for this answer and are covered elsewhere on SO and in your C++ book.)
As an aside, I doubt you want to take that vector by value.
Why don't you put struct player definition before int read_records(vector<player> player_info).