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

Thread: std::cin.getline() space and new line setting input

  1. #1
    Join Date
    Jan 2009
    Posts
    16

    std::cin.getline() space and new line setting input

    Hey there.

    What I'm trying to accomplish is simply. A user enters the input '2 1 3 18' (not including the quotes)
    and all 4 numbers are put into an array.

    This is my code thus far (note variables are set up properly etc the only issue I'm having is saving the input)

    Code:
    for (int i = 0; i < m; ++i){
       counter = 0;
    
       while (counter < n){
           cin.getline(input, 100, ' \n'); //so either a space or a new line is like pressing enter
           aMatrix[i][counter] = (int)input; //I wanted to put (long double)input; but that doesn't work - any ideas?
           counter++;
       }
    }
    Also running the code line for line, the first time cin.getline() is run I'm unable to enter anything.

    Thanks in advance!
    Last edited by Lang; January 15th, 2009 at 11:09 AM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: std::cin.getline() space and new line setting input

    Fundamentally, you can't put both a space and a newline inside a char literal. It's one or the other, not both. In order to pass multiple chars the function would need to accept a const char*, which it doesn't.

    What you want is to make a single call to getline() in the outer loop, and then read from the result using an istringstream in the inner loop.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,716

    Re: std::cin.getline() space and new line setting input

    I'm not exactly sure what you mean by your comments concerning
    "space or enter", but you can also read directly into the variable:

    Code:
    for (int i = 0; i < m; ++i)
    {
       counter = 0;
    
       while (counter < n)
       {
           cin >> aMatrix[i][counter]; 
           counter++;
       }
    }

  4. #4
    Join Date
    Jan 2009
    Posts
    16

    Re: std::cin.getline() space and new line setting input

    One of the requirements is that all the numbers for each equation are on the same line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)