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.
}
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 1 year ago.
Improve this question
Hi I need help with this code. It keeps on printing "ur input:32765" and the number keeps changing. I read a question on stack overflow and it said it wasn't initialized, whatever that means. Can someone help with whats wrong?
#include <iostream>
using namespace std;
int main() {
int x;
cout << "ur input:";
cin >> x;
cout << "" << x;
return 0;
}
Write
if (cin >> x){
cout << "" << x;
} else {
cout << "bad input";
}
otherwise a read of an unintialised x could arise if the cin fails in C++03 or earlier. That can happen if there are not data on the stream that can be read into an int type.
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 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 6 years ago.
Improve this question
I am having an issue with my C++ code. It keeps telling me Undeclared Identifier in the second function (the menu), but I cannot see the issue since I am passing the variable. The code is here
//Menu
void menu(int &selction) //TYPO!
{
cout << "Welcome to the math work along program!" << endl;
cout << "This will generate 2 numbers with the selected operator and\n allow you to solve the equation" << endl;
cout << "1. Addition\n2. Subtraction\n3. Multiplication\nOr type -1 to quit\n";
cin >> selection;
You have a typo. You've misspelled selection as selction
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I am trying to get only the alphabetic characters from an array of characters entered by the user. Here is a snippet:
const int SIZE(100);
int main()
{
char* entryTextArray = new char[SIZE];
char* adjustedTextArray= new char[SIZE];
int i, j;
cout << "Enter text, and I will tell you if it is a palindrome!" << endl;
cin.get(entryTextArray, SIZE);
cout << "Length of char array is " << strlen(entryTextArray) << endl;
for(i=0, j=0; i <= (strlen(entryTextArray)); i++) {
if(isalpha(entryTextArray[i]) && (entryTextArray[i] != '\0')) {
adjustedTextArray[j] = entryTextArray[i];
cout << adjustedTextArray[j] << endl;
j++;
}
}
cout << adjustedTextArray << endl;
}
When I compile, the cout of the adjustedTextArray displays the proper individual entrys, but the cout outside of the loop is the entry text, followed by garbage. I have no idea what is wrong! Help?!
You have the condition:
if (something && (entryTextArray[i] != '\0'))
so you are explicitly avoiding to copy the NUL terminating value from entryTextArray to adjustedTextArray. So you need to place it manually.
But since you are working in C++ using std::string just makes more sense.
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.