I'm having trouble figuring out how to read in numbers until I hit a blank line. This is what I have so far, and I've looked into getline and cin and others.
bool keep_going = true;
string z;
int a;
while(keep_going){ //stay in while loop until blank line
getline(cin, z); //read in line
a = z.size() - 1; //get
if( z[a] == '\n'){ //check for blank line
keep_going = false;
}//end if
}//end while
I'm new to c++ so any help is nice
How to check for blank line?
Moderator: Board moderators
-
- Experienced poster
- Posts: 136
- Joined: Fri Apr 15, 2005 3:47 pm
- Location: Singapore
- Contact:
This might help :
Code: Select all
int main () {
string s;
while (getline(cin,s)) {
if (s.length() == 0) break;
int n = atoi(s.c_str());
printf("%d\n",n);
}
return 0;
}
I think what you need is something like this:
This will read until end of file and skips blank lines as well. Hope this helps.
Code: Select all
int main()
{
string str;
while (getline(cin,str))
{
if (str.length() > 0)
{
// process input
}
}
return 0;
}
-
- Guru
- Posts: 584
- Joined: Thu Jun 19, 2003 3:48 am
- Location: Sanok, Poland
- Contact:
Do you need it all spell out? You already have tools necessary to accomplish that goal (I'll just add that string::size() method is nicer than string::length()). If you don't, my advice is that you get a good book and read it, your questions indicate lack of basic understanding of programming in general and C++ in particular. (If I'm wrong about that, you should start applying those skills, otherwise you won't progress).
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...