more help...

I think my logic makes sense, but the program is doing half of what I want.
the program:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string F;
string I;
string H = "inputdefault"; //a default value
string D = "outputdefault";//another default value

cout << "Enter input file: " ;
getline (cin, I);

if (I == " ") //if enter is pressed
{
I = H; //should change I to H
cout << H << endl; //should print "inputdefault" to console
}

else
cout << I << endl; //this prints any input to console, and works fine

cout << "Enter output file: " ;
getline (cin, F);

if (F == " ")
{
F = D; //should change F to D
cout << D << endl;//should print "outputdefault" to console
}

else
cout << F << endl; //this prints any input to console, and works fine

return 0;
}

My question:
why is blank returned when enter is pressed? If you don't input anything, except enter, it should output the default. Any thoughts? maybe its how the if else statements are bracketed? Let me know...
If the user inputs nothing, you get an empty string: ""

You are checking for a space: " ", which you will get if you press space, then enter.

" " and "" are two different strings.

EDIT:

It would also help if you chose more descriptive variable names. F, I, H, D are pretty confusing.

Also, code tags and proper indenting would also be nice.
Last edited on
Thanks disch. My code doesn't look pretty now. I'll make it look better when I'm done. Also, why does "" equal enter? Is that how c++ knows it as?
Also, why does "" equal enter?


It doesn't.

getline() doesn't give you the new line (the "enter"). It gives you everything input before it. Then it drops the newline.

If the user inputs nothing before they hit enter, then getline gives you an empty string (ie: nothing)

"" is an empty string.
Topic archived. No new replies allowed.