#include <stdio.h>
/* count lines in input */
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
Its a C problem. First question: The programme is supposed to count how many lines the input has.
However when I run it it doesnt print out the answer?
Second question. How can an input have more than one line. If you press enter that doesnt make a new line, it ends the input process so I dont see how one can put in a new line without pressing enter/return...
Works fine on my computer, what environment are you using? On windows you should put getchar(); after line 10. This will force the window to stay open.
I should have clarified my initial post.
My console stays open but nothing is printed after I input a random line, that's the problem
I also dont know how to put more than one line as input. And how do I make a new line? Enter? Doesn't that finish the input from the user? How do you make a new line without finishing the input?
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
cout << "Please enter a line of text: ";
string input = "";
std::getline(cin, input);
cout << "You entered: " << input << endl;
return 0;
}
Zalta, that isn't helpful, his problem has nothing to do with that. I see his problem.
No, enter doesn't finish the input, the input will continue until you signal end of file by pressing on windows ctrl-Z, or on unix ctrl-D.
rocketboy9000: I'm encouraging him to use std structures to achieve his result. His code is pretty much C, not C++. There are much tidier ways to achieve what he wants.
For example:
1 2 3 4
string input = "";
while (!getline(cin, input).eof()) {
// Do stuff
}
Nothing is wrong with using a straight up while(getline(cin, input));. I wanted to clarify the intent of the condition in the while loop for the OP; so he understood the logic of the loop.
If this were a C forum; I'd provide the answer in C. This is a C++ forum so I try to steer the OP towards writing correct C++ code. Having someone understand the fundamentals of C++ as early as possible (especially stream I/O) is going to help them in the long run imo.
Zalta, you fool: I quote the OP, "Its a C problem." And on windows, (I have now confirmed this) he just needs to press ctrl-Z after entering his lines.
Meh. He should be asking this on a C forum. As for his original code, all he needs to do is move the printf statement outside the loop for it to print only the total. May need to flush the console too.
The print statement is outside the loop. You once again haven't read his post. His problem was that he doesn't know how to give more than one line of input, and how to signal end of file from the console.
READ the op's post before making assumptions about what the problem is!
Rocketboy9000, thanks alot for the help, thats exactly what I was looking for. Infact both my questions were actually one in the same thing. I was putting in separate lines every time I pressed enter and it wasnt printing out any answer because I hadnt signalled EOF (CTRL Z)