I'm trying to make a simple database program that gives users the ability to add, remove, and edit string variables.
The issue I ran into: Assigning each added variable to the next empty array element.
-I'm using static arrays for now until I learn more about dynamic arrays.
-Since this is out of context I recreated the code here to make more sense, sorry if I missed anything.
//static array declaration
std::string names[5];
//string selection from main function
std::string stringSel = "Item";
//boolean condition for loop
bool completed = false;
//if (names[x].length > 0) then increment x by 1 until names[x].length == 0 and then set that array element to the value from stringSel
int x = 0
while(completed == false)
{
//if the length of the element is greater than zero characters, increment the element
if (names[x].length > 0)
{
x++;
}
//if the length of the element is not greater than zero characters, set the string to that element
else
{
//The empty element is assigned the string and the entire array prints out
names[x] = stringSel;
std::cout << stringSel << " was added to the database.\nThis is the printout of all current items: " << names;
completed = true;
}
}
The error I'm getting is coming from my if-statements condition. The '.' is underlined red
1 IntelliSense: a pointer to a bound function may only be used to call the function
names[x].length
std::string::length() is a member function, not a data member, so to access it you need to make a function call like:
names[x].length()
Related
I am working on client/server project. i am struggling with how to find vector's information in database to match client's account number that requested.
header file
struct storeData
{
int iAccountNumber;
int iPin;
double dBalance;
string sFirstName;
string sLastName;
string sMiddleInitial;
};
vector <storeData> storeDataArray;
storeData dataObj;
in server file..
int MyThread::findAccountNumberInStore(int iAccountNumber)
{
int iIndex = -1;
for(int unsigned i = 0; i <= storeDataArray.size(); i++)
{
//i got error message in if statement. i dont know how to fix it.
if(iAccountNumber == storeDataArray.at(dataObj.iAccountNumber))
{
return i;
}
}
return iIndex; //no account is found...
}
Also how can I store the struct's data in vector (all data in one element)?
From vector.at description:
Returns a reference to the element at position n in the vector.
The function automatically checks whether n is within the bounds of valid elements in the vector, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds.
You can read more about this function here:
vector.at
also you can read a discution about it here:
vector::at vs. vector::operator[]
to your practical problem i suggest using it like this:
if(iAccountNumber == storeDataArray.at(i).iAccountNumber)
You almost have it - the bracket is in the wrong place, and you didn't index the vector correctly, you have
if(iAccountNumber == storeDataArray.at(dataObj.iAccountNumber))
Should be
if(iAccountNumber == storeDataArray.at(i).iAccountNumber)
Also I see you use <= on array size check - this is incorrect. Vector bounds are from 0 to size()-1
I've tried looking around but I can't find anything about this anywhere.
I'm writing a custom array class with a "push" function to add a value to the array.
It seems to work perfectly fine but won't execute more than once.
Take the main method below for example:
int main()
{
Array<int> test(4,5);
test.push(4);
test.writeOrdered("Output.txt");
return 0;
}
This will put the int value 4 into the array at the first available position and execute the writeOrdered function.
The following main method, on the other hand:
int main()
{
Array<int> test(4,5);
test.push(4);
test.push(5);
test.writeOrdered("Output.txt");
return 0;
}
This will put the number 4 into the array at the first available point as above and then stop. It won't execute any further lines of code.
Here's the push function for reference:
void push(Datatype p_item)
{
bool inserted = false;
int i = 0;
while (inserted == false)
{
if (m_array[i] < 0)
{
m_array[i] = p_item;
i++;
inserted = true;
cout << p_item << " saved to array" << endl;
system("pause");
}
}
}
You have an infinite loop. After the first insert m_array[0] >= 0 and i never grows. You would have found it out, had you debugged the code somehow.
Basically I don't understand your push function but the way it is, after you insert a non-negative value into the first position any further call to your push function results in a tight loop.
I imagine that you want the i++ outside the if statement.
Without seeing the full implementation of the Array class I would guess that the array m_array contains negative numbers by default. This will allow the first call to the push method to succeed. The next call to the method contains a value of 4 at index 0 and will be stuck in an infinite loop because inserted will never be set to true nor will the value of i be incremented.
This is the snippet of code that I'm puzzled about. I'm checking for how long an incoming string is. I've appended * in order to have a sentinel value to stop the while loop. Yet, I'm consistently getting a length value that is inclusive of the * and I don't understand why, since the while loop with the nested if ought to stop prior to the *. Can someone point out what I'm doing wrong and why I'm having this issue?
void conversion(string romanIn)
{
length=0;
romanIn.append("*");
while(item!="*")
{
if(item != "*")
{
item = romanIn[length];
length++;
}
cout<<item;
}
you are naturally going to get a +1 the first time through the loop because you aren't initializing the variable "item". Also make it a do while instead of a while loop.
Try this:
do
{
// This line moves out of the if statement
item = romanIn[length];
if(item != "*")
{
length++;
}
cout<<item;
}while(item!="*")
What is the initial value of item?
Let's assume it's 0. You enter the loop
item == 0 != marker, so you enter the if as well, and you say
item = romanIn[0], length++
If romanIn[0] == "*" you will exit the loop, but your length now says 1 which includes the marker
So I am trying to create a BST using an array of pointers. My algorithm is correct (tested a version that doesn't use pointers), but when using the below code, the following occurs:
If I add the first element, it is added to position 1 of the array.
If I add a second element, for some reason, position 1 of the array is overwritten to this element, and then the program continues (else part) and attempts to insert it again.
EG. (traced the program with a bunch of couts)
1. Call add(5, 1)
inserting 5 into position 1
position 1 is now 5
2. Call add(4, 1)
position 1 is now 4
moving right
inserting 4 into position 3
position 1 is now 4
...
template <typename Item> void ABTree<Item>::add(Item input, int index){
if (array[1]==0){
array[1] = &input;
size++;
}else{
if (input < *array[index]){
if (array[2*index] == 0){
array[2*index] = &input;
size++;
}else
add(input, 2*index);
}else{
if (array[(2*index)+1] == 0){
array[(2*index)+1] = &input;
size++;
}else
add(input, (2*index)+1);
}
}
There are a whole lotta troubles in the code you've provided.
Assigning address of the temporary variable is
inadmissible. The "input" variable ends up right after the add
function returns. Storing its address than is meaningless and leads
to almost inevitable crash. In particular, when you're adding second element to your array, those condition checks:
if (array[1]==0)
if (input < *array[index])
yields undefined result, and therefore undefined control flow.
I don't know type of the container you're using but I assume it to
be a kind of vector or even plain C array. If my guess is true, you
should perform a bounds check before accessing array's elements with
the index.
No reallocation of the memory accupied by the array is performed.
Or, alternatively, if you're using a constant size array, no check
is performed (see 2).
So, basing on the output, you've got the following sequence when adding second element:
On the check that head is empty - false;
On the check if (input < *array[index]) - false, since array[index] contains garbage, this is why "inserting 4 into position 3" despite we should add 4 to the position 2;
On the check if (array[(2*index)+1] == 0) - again false, since this element contains garbage too (am I right that you didn't initialize your array with zeros?);
So, recursive call to the add routine happens...
I altered the code to accept a pointer to the item to be inserted instead.
if (array[1]==0){
array[1] = input;
size++;
}else{
if (*input < *array[index]){
if (array[2*index] == 0){
array[2*index] = input;
size++;
}else
add(input, 2*index);
}else{
if (array[(2*index)+1] == 0){
array[(2*index)+1] = input;
size++;
}else
add(input, (2*index)+1);
}
}
So for my code I want to pass an argument to a function and if it doesn't return true, I want it to pass the next index digit in the int array.
So if areaIntA[0] = 0; doesn't return true, I want it to pass areaIntA[0][1] = 01; and if that doesn't return true, areaIntA[0][1][2] = 012; etc...
My updated code:
areaInt = areaIntA[0];
do {
areaCheck = isRegistered(file, areaInt);
if (areaCheck != 1)
{
areaInt = areaIntA[i] * 10 + areaIntA[i+1];
i++;
}
} while (areaCheck != 1);
cout << areaCheck << endl;
This compiles but it shows a segmentation error, anyone know why?
Have the function take in a list (vector). for every call add a new element to the end.
eg.
List< int?> areaIntList;
do {
areaIntList.Add(areaInt[i]);
areaCheck = isRegistered(file, areaInt[i]);
if (areaCheck != 1)
{
i++;
}
} while (areaCheck != 1);
isRegistered takes in a List of your used type.
You're weirdly equating array integer values in a sort of string context. That usually means base-10 math.
Try:
int val = 0;
do {
val = 10 * val + areaInt[i];
areaCheck = isRegistered(file, val);
if (areaCheck != 1)
i++;
} while (areaCheck != 1);
On the first pass, val will be the value of areaInt[0]; On the second pass, it will be areaInt[0] followed by areaInt[1] (e.g. 0 and 1 become 1, 1 and 2 become 12). And so on.
My understanding of your question is this:
isRegistered is a function that takes a file and a variable-length string of digits, returning 1 if the string is registered and 0 otherwise.
areaInt is a C-style string like "31526".
What you want is to see if "3" is registered; if not, see if "31" is registered; if not, try "315", etc until all the digits of areaInt are exhausted.
The end result should be a substring of areaInt which is the shortest registered string, or an error if no registered string was found.
This is how I'd do it.
If the input string areaInt is empty, return an error.
Loop a counter i from 1 to the length of the string:
Construct a test string s from the first i characters in areaInt.
Test if s is registered. If it is, break out of the loop and return s. If not, continue the loop.
If the loop completes at the end of areaInt and no registered string has been found, return an error.
Note. Make sure you stop at the end of areaInt. Otherwise if you get to the end of areaInt and still haven't found a string that is registered, then you will overrun the buffer and try to read an element not in the string.