Not getting any output

1
2
3
4
5
6
7
8
9
10
11
#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.
oren-laptop% ./a.out   
hjjfjdgb
gbsgb
gbfsgb
3
Last edited on
If you're running this in an IDE; chances are it'll finish and return (destroy the console) before showing you the results.

I suggest you add a getchar() after the printf() method so it'll pause and wait for input before quitting.
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?
You should be writing this in a decent C++ syntax. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#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.
Last edited on
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
}

Last edited on
Why the need for eof?
while( getline(cin, input) )

What is the problem with C?
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!
Last edited on
Okay, let's do it in a way that (hopefully) makes everyone happy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional>
#include <cstdio>
using namespace std;

struct Char
{
    char ch;

    Char(char c='\0'):ch(c) {}
    bool operator==(const Char & c) const { return ch==c.ch; }
};

istream & operator>>(istream & is, Char & c)
{
    int n;

    if ((n=getchar())==EOF) is.setstate(ios_base::badbit);
    else c.ch=n;

    return is;
}

int main()
{
    cout << count_if(istream_iterator<Char>(cin),
        istream_iterator<Char>(),
        bind1st(equal_to<Char>(),'\n')) << endl;

    cout << "\nhit enter to quit...";
    cin.clear(); cin.get();
    return 0;
}

EDIT: Oops... Forgot the C part...
Last edited on
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)
Topic archived. No new replies allowed.