Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
as this is my first attempt at using pointers for vectors, I did some research on it and tried it on my program but however it doesn't give me the results I wanted,though it's closed to it.Please forgive me if I asking a lousy question.
Your error is when you print the contents of the vector, not when you add data to it.
for (int i= 0; i<5; i++) {
if(storeData.empty()) {
cout <<"<no other records available>" << endl;
}
// error: storeData[i] may not be valid
else if(storeData[i].getCivIndex() == 0 && storeData[i].getXOrdinate() == 0 && storeData[i].getYOrdinate() == 0) {
cout << "<no other records available>" << endl;
}
else {
cout << "Civ Index:"<< storeData[i].getCivIndex() << ",at Sector("<< storeData[i].getXOrdinate() << "," << storeData[i].getYOrdinate() <<")" << endl;
}
}
You're always looking to read 5 records here even if the vector contains fewer. You should check the size of the vector before dereferencing it, otherwise you are reading random memory and undefined behavior will result.
if(storeData.size() <= i) {
cout <<"<no other records available>" << endl;
}
if(storeData.size() > 5) {
storeData.resize(5);
}
first I think here you mean if(storeData.size()<5) otherwise the following printing will certainly fail since you only have access to storeData[0] while you try to access others.
When you do resize without a second argument, the items added into the vector will be default constructed. In case the PointTwoD struct doesn't provide a default constructor, its data members won't be initialized, which means it could end up with random values. That's why you have those weird numbers. (0 is also possible).
The solution is to define a default constructor for pointTwoD and set its data member to 0 in the initialization list or call resize with a second argument.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I trying to save write a list of objects. I don't know how to check if the created object was really saved, for now I try to test it by calculating the list size
void Hash::insertTest(Customer *cl) {
int index = hashFunction(cl->code); //OK
clientes[index].push_back(*cl);
int sizetable = sizeof(clientes)/sizeof(clientes[0]); /// I Think this is wrong.
cout << "size" << sizetable << endl; //prints 0, no insert worked
}
List:
list<Customer> *clientes;
Full Code is here with a web compiler
No, already you did it :) Please check my comments and This source :)
void Hash::insertTest(Customer *cl) {
int index = hashFunction(cl->code); //OK
clientes[index].push_back(*cl);
int sizetable = sizeof(clientes)/sizeof(clientes[0]); /// I Think this is wrong.
cout << "size" << sizetable << endl; //prints 0, no insert worked
cout << "Look -> " << clientes[index].front().name<<endl; //// You already save client in this list.
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This is a project, where I need to make a list of items that the user will input and they need to be written in a file. However it prints #4761920nan for each entry in it. And then it prints it all that's supposed to be in the file on the console instead. Any light on this issue will be appreciated.
int main()
{
ofstream data("list.txt");
int n;
cout << "How many cabins are in your region? "; cin >> n; cout<<endl;
cabin*tour=
new cabin[n];
for (int i=0; i<n; i++)
{
tour[i].nameInput();
data<<tour[i].nameOutput();
tour[i].capacity();
data<<tour[i].capacityOutput();
tour[i].gps();
data<<tour[i].gpsOutput();
};
data.close();
return 0;
}
This is what nameOutput() looks like:
char cabin::nameOutput()
{
cout<<"Name: "<< name<<endl;
}
Your function cabin::nameOutput has char as the return type but doesn't return anything. Thus, the call
data << tour[i].nameOutput();
will write appropriately to standard output (due to cout), but not appropriately to data. Since nothing is returned, the output written to data is undefined behaviour.
In order to fix this, you may want to specify a return type in your functions like so
// the return type depends on the type of `name`
// this may be char*, std::string, or something else appropriate
std::string cabin::nameOutput()
{
cout << "Name: " << name << endl;
return name;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to write a program that takes a string and an integer in a class object. The program will then sort the class objects, in an array of objects, by the integer, allowing me to then display the names. Unfortunately, when I try to build the array, I have an error on my assignment operator.
My questions are: Do I need to overload the = operator, and if so, how (somehow I've never figured out how to overload operators)? If not, where am I going wrong?
Here is the code:
void InitiativeList::makeList(size_type physicalSize, size_type logicalSize)
{
string sNewActor;
int iNewOrder;
for (size_t index = 0; index < physicalSize; index++)
{
if (logicalSize == physicalSize)
{
grow(physicalSize);
}
cout << "Enter character name: ";
cin >> sNewActor;
if (sNewActor == "Exit")
{
return;
}
cout << "Enter initiative roll: ";
cin >> iNewOrder;
actorOrder[index] = new Actor(iNewOrder, sNewActor);
logicalSize++;
}
}
Thank you for your help.
You don't need new there, because you have an array of actorData and not actorData pointers, and the error is saying that it can't convert actorData pointer to actorData. So replace this line:
actorOrder[index] = new actorData(iNewOrder, sNewActor);
with this:
actorOrder[index] = actorData(iNewOrder, sNewActor);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
int main() { vector g1; vector :: iterator i; vector :: reverse_iterator ir;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end\t:\t";
for (i = g1.begin(); i != g1.end(); ++i)
cout << *i << '\t';
cout << endl << endl;
cout << "Output of rbegin and rend\t:\t";
for (ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << '\t' << *ir;
return 0;
}
Here in this code variable "i" has been declared as a iterator as well as a variable inside a for loop. isn't that a error?
If we see the first for loop it say that the loop will run till i!=g1.end() that means that the value of *(g1.end()) should not be displayed by *i but it is giving. ide shows output 1 2 3 4 5 for me it should be 1 2 3 4.
i is defined as an iterator in the argument list. When you redefine it in the first for loop, this is a new definition only for the scope of the loop -- this is perfectly legal, though not good practice.
vector::end() points to memory after the final item, not to the final item. So, yes, the final item in the vector will be printed.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int i,t,x[20], even, odd, prime;
cout << "Enter 20 integer numbers from 0 to 99: "<<endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i <<":";
cin >> x[i];
}
cout << "\nPrime numbers are: " << endl ;
prime=1;
for (i=2; i<=20 ; i++)
{
for(t=2;t<x[i];t++)
{
if(x[i]%t==0)
{
prime=0;
}
}
if(prime==1)
{
cout << x[i] << endl;
}
prime=1;
}
for(i=1; i<=20; i++) // this is where i have problem.
{
if(x[i]% 2 == 0)
{
even++;
}
else
{
odd++;
}
}
cout << "Number of odd numbers: " << odd << "\n";
cout << "Number of even numbers: " << even << "\n";
return 0 ;
}
When i compile it shows even (40) and odd (10) for input of 0 till 19. Where it should show even 10(including the 0) and odd (10). Im not sure where am i doing it wrongly. I hope someone can help me improve the code.
Variables even and odd are never set to a known value, so you are not formally allowed to read from them. Doing so invokes that most infamous Standardese concept: undefined behaviour. So the values of these variables could be right or could be wrong; the variables and all code trying to read them could be optimised entirely out of your program; or anything can happen. You cannot rely on these variables doing anything right. All attempts to read them make your program ill-formed, so now it can do anything, including things you would never have imagined.
You should search for the abundant background info about these concepts, but I like to think I made a fairly decent summary here: https://stackoverflow.com/a/38150162/2757035
Also, as Thomas points out in the comments, you appear not to understand how array indexing works: Indexes are 0-based. So, int i[20] declares 20 elements numbered from 0 to 19. You try to access index 20, which is not part of the array and hence is more undefined behaviour.