|
-
December 10th, 2002, 11:17 AM
#1
Output problems
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
-
December 10th, 2002, 12:42 PM
#2
Use gets() in C or cin.getlin() for C++.
- Kevin
-
December 10th, 2002, 12:45 PM
#3
This should work ...
Code:
#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>"
-
December 10th, 2002, 01:55 PM
#4
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
-
December 10th, 2002, 05:14 PM
#5
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|