CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Output problems

  1. #1
    Join Date
    Mar 2002
    Posts
    96

    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

  2. #2
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Use gets() in C or cin.getlin() for C++.

    - Kevin

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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>"

  4. #4
    Join Date
    Mar 2002
    Posts
    96
    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

  5. #5
    Join Date
    Mar 2002
    Posts
    96
    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
  •  





Click Here to Expand Forum to Full Width

Featured