/n means in this code

Jan 19, 2014 at 8:02pm

I know that/n means new line but what does '/n' mean in this code?


#include<iostream>
#include<string>
using namespace std;
void main()
{char c;
int noc=0;
int nol=0;
while(cin.get(c))
{
++noc;
if(c=='\n'){
++nol;
}
}
cout<<"char:"<<noc<<endl<<nol<<endl;
system("pause");
}
Jan 19, 2014 at 8:10pm
That the char variable c contains a newline character. If you use cout<<c; it will go to the next line.

You can also make the code you post easier to read by clicking the <> button on the side of the post window, and putting your code inside the auto generated tags. While not a big deal in this case, when you go to post more complex code in the future, it will make life easier for everyone.
Last edited on Jan 19, 2014 at 8:12pm
Jan 19, 2014 at 8:39pm
closed account (iAk3T05o)
 
int main()

not
 
void main()
Jan 19, 2014 at 9:25pm
This code gets one character (written to c) at a time. For each character in the stream, noc is incremented. Whenever the new character is a new line, nol is incremented.

Running this code might not work so well. cin.get() returns the state of cin. Which, if a new line is entered, becomes empty pretty quick. Not sure if you'd be able to get more than 1 nol before the code exits. If it was an istringstream object or ifstream object, I think it'd be a bit nicer.
Last edited on Jan 19, 2014 at 9:25pm
Jan 20, 2014 at 12:49am
hanahyasser wrote:
I know that /n means new line but what does '/n' mean in this code?

I'm sorry what?? If you know it means newline, why are you asking this question?

Stewbond wrote:
Running this code might not work so well. cin.get() returns the state of cin. Which, if a new line is entered, becomes empty pretty quick.


std::istream::get(char &)
The above does not set any error flags once a newline character is read
Last edited on Jan 20, 2014 at 12:50am
Jan 20, 2014 at 11:01pm
Actually, it's "\n", NOT "/n".

EDIT: And your code is quite messy... Try use [code].
Last edited on Jan 20, 2014 at 11:01pm
Topic archived. No new replies allowed.