This might seem like a silly question, but how do I read in a newline char, by itself, as input?

For example, if I gave the user a choice to enter a string for a new filename, or let them just press ENTER for some other choice.

forgot to add that this is in C++.

This might seem like a silly question, but how do I read in a newline char, by itself, as input?

For example, if I gave the user a choice to enter a string for a new filename, or let them just press ENTER for some other choice.

forgot to add that this is in C++.

Don't quite understand what you're trying to say.

In the previous thread, we told you to do this to remove the trailing newline you got in your string (from the input stream):

if (line[strlen(line)-1] == '\n')
    line[strlen(line)-1] = '\0';

Now if the that if() statement is false, that means that there are still charecters left in the stream. You should then flush out any characters left in the stream like this:

while ((ch = cin.get()) != '\n' && ch != EOF);

For example, if I gave the user a choice to enter a string for a new filename, or let them just press ENTER for some other choice.

It depends on how you get the input, but you can check for an empty string with getline().

#include <iostream>
#include <string>


int main()
{
  using namespace std;

  string filename;

  if ( getline( cin, filename ) ) {
    if ( filename.empty() )
      cout << "Some other choice\n";
    else
      cout << "Open the file " << filename << '\n';
  }

  return 0;
}

I guess you could also peek() the stream and doing weird stuff, but that's more work than it probably needs to be. ;)

I dun think i was clear enough...

How do i get it so that if i just prompt the user for input and if he presses ENTER w/o typing anything else in, a function is called?

I dun think i was clear enough...

How do i get it so that if i just prompt the user for input and if he presses ENTER w/o typing anything else in

I'm not quite sure why you want to do that...

cout << "Enter something:" << endl; // prompts for user input
getline(cin, filename);   // gets user input
myFunction(filename);       // calls a function before user types anything else in

Of course, this won't restrict the user to typing lots of charecters, but if you hit simply hit enter without typing anything else in, the program will accept that and move on (as opposed to cin >>, where it keeps waiting until you type something other than whitespace).

As mentioned, use getline() to read the input. Test the first character and if it's '\n', only a RETURN was entered. Otherwise process the line 'cuz something useful (hopefully) was entered.

I dun think i was clear enough...

How do i get it so that if i just prompt the user for input and if he presses ENTER w/o typing anything else in, a function is called?

I still don't think you're being clear enough. My example did that exactly. :confused:

As mentioned, use getline() to read the input. Test the first character and if it's '\n', only a RETURN was entered. Otherwise process the line 'cuz something useful (hopefully) was entered.

That won't work with getline() because getline() trims the '\n'. The first character would be '\0' in a C string and s.empty() would be true for a C++ string.

argh sorry i was in a rush and only had a minute to run through the posts. thanks for the replies i will try out the things you guys suggested.

As mentioned, use getline() to read the input. Test the first character and if it's '\n', only a RETURN was entered. Otherwise process the line 'cuz something useful (hopefully) was entered.

That won't work with getline() because getline() trims the '\n'. The first character would be '\0' in a C string and s.empty() would be true for a C++ string.

Sorry 'bout that. I thought getline() was just a wrapped fgets() . Ya larn sumtin' new ever' day, eh? :mrgreen:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.