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.
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 am facing this error while reading the text file and the file contain a "name" string.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
char ch;
ifstream read;
read.open("sam.txt");
while (read.eof())
{
read.get(ch);
cout << ch << endl;
}
return 0;
}
The issue you mention in the comments happens because when the end of the file is reached, get fails and does not overwrite the previous value of ch. A quick way to fix that is:
while (!read.eof())
{
if(read.get(ch))
{
cout << ch << endl;
}
}
Now ch is printed only when read.get(ch) returns successfully.
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 is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
What have I done wrong with the following code?
#include <iostream>
using namespace std;
main ()
{
int a;
int b;
int sum;
cout << "Enter first number \n";
cin >> a;
cout << "Enter second number \n";
cin >> b;
sum = a+b;
cout << "The sum of both numbers is" << sum << endl;
return 0;
}
Does the editor you are using tells errors, so the code is not executing? Or som exception rises? Or it is executing but nothing is shown? Please specify your problem accurately.
Anyway, you must use
int main ()
instead of
main()
Notice that your code returns a value. The last line of you code is:
return 0;
Thus, you must specify an int return type.
Check your initial lines with this.
#include <iostream>
using namespace std;
int main ()
{
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
i started learning c++ last week and ive finally learned enough to try and stand on my own feets. well guess what i have a problem. the program im trying to make will ask for a file already existing or creates a new one if the name isnt found, and places information in the file lines. when you type -1 you close the program.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string x;
string Input;
int line=0;
cout << "Please enter the name of the file with the file type" << endl;
cin >> x;
ofstream SelectedFile;
SelectedFile.open(x);
while(Input != "-1"){
cout << "Enter the content of the " << line <<" line, or type -1 to quit." << endl;
cin >> Input;
line++;
}
SelectedFile.close();
}
I guess you're having a compile error because std::ofstream::open does not take an std::string as an argument in C++98 standard. Try this one:
SelectedFile.open(x.c_str());
Or compile with C++11 support.
Update: Where you write things into file? I think you forgot to write that part
SelectedFile << line << std::endl ;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is the professor's code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
#include <new>
int main()
{
char *p;
int index = 8;
cout << "Input how many characters:";
cin >> index;
p = new char [index + 1];
cin >> p;
cout << "p is: " << p;
delete [] p;
p = NULL;
return 0;
}
After I ANSWER "how many characters" statement with a number the program stops.
Anyone knows why?
First you have
cin >> index;
where you have to input the number of characters.
Then you have
cin >> p;
where you have to input some characters - but no more than the number you gave before. Are you doing that? It might be helpful to give another prompt:
cout << "Input up to " << index << " characters:";
cin >> p;
I hope your professor is going to follow this up with an explanation of buffer overruns, input validation, exception safety, and how to use std::string to avoid faffing around with manual allocation. Otherwise, you're being taught some very bad habits.