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 ;
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 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 2 years ago.
Improve this question
I'm new to programming and one of the projects I would like to tackle would be a quiz, the problem is I do not know how to store my quiz scores in a text file, can someone help plz? A given example would be nice
Here is slightly modified example of the one given in cplusplus
// basic file operations
#include <iostream>
#include <fstream>
int main () {
std::ofstream myfile("example.txt");
if(myfile.is_open())
{
myfile << "Writing this to a file.\n";
myfile << 1 << 2 << 3;
}
else
{
std::cout << "error opening file" << std::endl;
}
return 0;
}
This creates example.txt in your working directory if it does not exist yet, opens it, streams to it and closes the file as myfile is destroyed. Now the file contains the following text:
Writing this to a file.
123
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 6 years ago.
Improve this question
I used cin.getline after cin.ignore() but I am getting an error saying unassigned int... Not sure what to do or what is wrong. Any suggestions?
Here is my code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string phras;
cout << " Provide a phrase, up to 30 characters with spaces > " << endl;
cin.ignore();
cin.getline(phras, sizeof(phras));
cout << " The phrase is: " << phras << endl;
cout << endl;
return 0;
}
UPDATE
I changed cin.getline(phras, sizeof(phras));
to getline(cin,phras)
Problem solved! Thanks for the help everyone!
The problem is that
char letter[1];
isn't large enough. If a C string is to hold up to N characters, it needs to be declared char letter[N+1] to allow room for the null terminator character. So if the user is going to type a single character, it needs to be:
char letter[2];
As a result, you're getting undefined behavior when cin >> letter writes 2 characters into the array that only has room for 1.
Similarly, if the user is allowed to type a 10-letter word, it should be:
char word[11];
and it should be:
char phrase[31];
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 7 years ago.
Improve this question
Im newbie in c++ programming. How can I do something like this?..
int question1;
question1: "What is your name?";
to set the text value in integer?
#include <string>
#include <iostream>
int main( )
{
std::string name;
std::cout << "What is your name?: " << std::endl;
std::cin >> name;
std::cout << "Your Name: " << name << std::endl;
std::cin.get( );
return 0;
}
Simply read the input in as string.
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 9 years ago.
Improve this question
thanks for taking the time to help me out.
I'm really new with C++ and Xcode. I was working on a simple program to help me understand loops, so my goal was to make a simple "echo machine". This is my code:
string words;
int main()
{
do {
cout << "Enter text.";
cin >> words;
cout << "You entetered " << words << "!";
}
while (words != "goodbye");
return 0;
}
My result is nothing but lldb in parenthesis. I am very frustrated and can't find what I'm doing wrong anywhere. Please help and thank you so much.
Are you just missing the include directives for the standard headers you're using?
Try this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string words;
do {
cout << "Enter text: ";
cin >> words;
cout << "You entered " << words << "!\n";
} while (words != "goodbye");
return 0;
}