Click to See Complete Forum and Search --> : Output problems


shilpichaudhry
December 10th, 2002, 10:17 AM
I have to use an input command, where the value I enter can be null (press enter), or some value.

I tried using cin >> (but this requires that I enter at least one alphabet)

scanf("%s", x) // same problem

getch() and then putch() if I use this, I cannot use backspace to erase what had been written earlier.

Any suggestions?
Shilpi

KevinHall
December 10th, 2002, 11:42 AM
Use gets() in C or cin.getlin() for C++.

- Kevin

Philip Nicoletti
December 10th, 2002, 11:45 AM
This should work ...


#include <string>
#include <iostream>

using namespace std;

int main()
{
string line;
getline(cin,line);

if (line.empty())
{
cout << "enter with no input" << endl;
}

return 0;
}


Note: if using old version of VC++, you might need
to process the fix found at

http://www.dinkumware.com/vc_fixes.html

Look for "Fix to <string>" and "Fix to <istream>"

shilpichaudhry
December 10th, 2002, 12:55 PM
Thanks for ur reply. There are times when the program just skpis the code. I am using cin.getline instead. SO if I have something like

cout << "x:
cin.getline(x, 100);
cout << "y":
cin.getline(y, 100);

The output is displayed as
x:
y:

without giving me a chance to type in the value of x.

Could you help?
Thanks, SHilpi

shilpichaudhry
December 10th, 2002, 04:14 PM
I am really surprised at the results I am getting.
If I use
string line;
cprintf("X:");
getline(cin, line);
cprintf("\n y:");
getline(cin, line);

At the first getline if I type '1' I get the value of line as empty, and if I type 2 at the second getline, I get value of line as 1 instead of 2.
Shilpi