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;
}
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 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 2 years ago.
Improve this question
#include <iostream>
int main() {
int manic;
std::cout << "Enter Size of Array" << "/n";
std::cin >> size; // << "/n";
size = manic;
static const int arr[size];
for(int i = 0; i < size; i++) {
std::cout << "Enter" << i << "Element" << "/n";
std::cin >> (arr[i]); // Error Is Shown in this Line
}
bool r = is_even(arr, size);
std::cout << r;
return 0;
}
My first post here .I typed this code in Visual Studio 2019.The Microsfot Documents do not help.
The compiler gives you the hint that you may uninentionally override a value
std::cin >> size;// << "/n";
size = manic;
You let the user input a value and then override it with another, by the way uninitialized, value.
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 3 years ago.
Improve this question
My program is saying that the function length() is not in the standard library and is producing an error that does not let me run my code
I have tried to place two different libraries into the #include statements. I then tried to place the length function on different variables but the same error occurs.
int main()
{
string line;
ifstream out_file_DOI("declaration_of_independence.txt");
if (out_file_DOI.is_open())
{
int i = 0;
while (getline(cout, out_file_DOI, i))
{
cout << line << endl;
cout << line.length(); //error is on this line...
i++;
}
out_file_DOI.close();
}
}
else
{
cout << "Unable to open file...";
}
//creates the declaration file
out_file_DOI.open("declaration_of_independence.txt");
}
I expect the code to run and show the proper results (proper results being the program encrypting the file (Declaration of Independence).
if u wanna read a line by line from the file to “line” string,
getline(out_file_DOI, line);
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 3 years ago.
Improve this question
I'm creating a little program in order to test the vector class.
I'm using a string vector then i read a text file and i'm tring to write each word in the vector (1 word for each space).
When I try to use the push_back in order to put a string into the vector appear an error that says "There isn't a function to convert a string to a char".
If i made some english error sorry.
Thanks for the help.
I read some guide that explain how work the push_back but in all of this tutorial the use this declaration.
vector<string> v_of_string;<br/>
//allocate some memeory<br/>
v_of_string[1].pushback(string to punt in the vector);<br/>
My Code
int main() {
vector<string> str;
//allocate some memory
ifstream iFile("test.txt");
int i = 0;
if (iFile.is_open()) {
while (!iFile.eof()) {
string temp;
iFile >> temp;
str[i].push_back(temp);
cout << str[i];
i++;
}
iFile.close();
}
return 0;
}
So
str[i].push_back(temp);
is an error, you meant
str.push_back(temp);
You push_back to the vector as a whole, not to one particular element of the vector, so there is no need for [i]. I expect if you go back to your guide then it will say the same.
You could also replace cout << str[i]; with cout << str.back(); to always output the last element of the vector. So actually you don't need the variable i at all.
Also
while (!iFile.eof()) {
string temp;
iFile >> temp;
is incorrect, it should be
string temp;
while (iFile >> temp) {
See here for an explanation. I'd be interested if you got this code from your guide as well. Using eof in a while loop in C++ must be single commonest error we see on stack overflow.
Incidentally str is a poor choice of name for a vector variable IMHO.