问题
I saw something with a similar title has been answered, but the content was too dense for me as I don't know a lot of c++.
I am very new to programming, I cannot figure out how to store the previous iteration in my while loop. I am trying to use a while loop to write a users text into a file, and end their input with two \n
characters. This is where my problem is because with my current code the input ends with one instance of enter.
My code looks like this, but I know temp
and new_advice
are the same:
char new_advice, temp;
while( (!(new_advice == '\n' + '\n'))) && (!(temp == '\n')) )
{
temp = new_advice;
cin.get(new_advice);
fout.put(new_advice);
}
Edit* Ok, I don't think I was clear about what I was asking.
The original assignment said "Allow the user to type in advice so that it can be any number of lines long. The user is told to end his or her advice by pressing the return key two times. Your program can then test to see that it has reached the end of input by checking to see when it reads two consecutive occurrences of the character '\n'."
Does this change any answers to my question?
I have now tried to do this with the code:
char new_advice;
int temp = 0;
while(temp < 2)
{
if(new_advice == '\n')
{
temp = temp++;
}
cin.get(new_advice);
fout.put(new_advice);
}
But this is not working either, my program does not recognize enter as ending the program. Would the context of the whole program be worth posting?
回答1:
I don't really want to answer your specific question, since the idiomatic way of doing what you want is rather different, and quite a bit cleaner:
std::ofstream outfile("output.txt");
for (std::string line; std::getline(std::cin, line); )
{
if (line.empty()) { break; }
outfile << line << std::endl;
}
回答2:
So many things.
First, get rid of redundant parentheses for clarity. Then, use a != b
instead of !(a == b)
.
This yields while(new_advice != '\n' + '\n') && temp != '\n')
, and I’m not sure what this is supposed to do (even ignoring the mismatching parentheses which will result in a compile error): '\n' + '\n'
adds two characters, which is an integer addition. But even if it didn’t, and were a string concatenation instead, you’d subsequently compare the result (a two-character string) to a single character. This would never yield true
.
Now, about the logic: just delimit each line with a return, and add an extra return character at the end:
std::string line;
while (std::getline(std::cin, line))
std::cout << line << "\n";
std::cout << "\n";
… easy as π.
But beyond that, it’s unusual to end input with two newlines. Rather, it’s convention (in UNIX) that each line is delimited by exactly one newline at the end.
回答3:
char
s are promoted to int
s if used in +
expression. What exactly are you trying to compare?
My code looks like this, but I know temp and new_advice are the same:
Then this(!(new_advice == '\n' + '\n'))) && (!(temp == '\n'))
will fail immediately.
And your parenthesis' don't match up.
回答4:
Simplify your loop to just check while you have input: while(cin) {
.
If you need to store a previous value, leave that variable only outside the loop:
char previous = '\0';
while (cin) {
I think you really want to find an end of line and add a '\n', but this isn't portable between windows and unix. The std::getline answer works much better. But, assuming you wanted to do this yourself...
Declare and give your current character a value in the loop.
char current;
cin >> current;
Then double check you actually received valid input by checking cin
again.
if(!cin) {
break;
}
Next, do whatever you need with this character:
fout << current;
if(`\n` == current) {
fout << '\n'; // though std::endl would be more portable
}
Finally, update your previous variable.
previous = current;
}
I tried to walk through fixing this to understand the multiple concepts involved, though I don't think you need most of this. The std::getline
answer will work, but not teach you about all the things you tried to do.
来源:https://stackoverflow.com/questions/12468373/how-do-i-store-a-previous-iteration-in-a-while-loop-in-c