Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Read in numbers from console won't stop at newline.

Programming


Tags
c++, cin, newline, numbers, space delimited line

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 2 Weeks Ago   -   Original Discussion by yifangt
yifangt's Unix or Linux Image
yifangt yifangt is offline VIP Member  
UNIX.COM VIP Member
 
Join Date: Sep 2009
Last Activity: 7 December 2017, 5:47 PM EST
Location: Saskatchewan, Canada
Posts: 516
Thanks: 332
Thanked 8 Times in 8 Posts
Read in numbers from console won't stop at newline.

Hello,
I have snippet code from Lippman's <<C++ primer>>.
The program is to convert regular decimal (0 ~ 15) numbers to basic hexdecimals. The instruction tells the program will execute by hitting newline at the end. When I tried to run the compiled program, hitting ENTER did not work as instructed in the book. I have to hit any invalid letter/key or Ctrl+D (I'm using Ubuntu) to terminate the input.

Code:
#include <iostream>
#include <string>

using namespace std;

int main ()
{
  const string hexdigits = "0123456789ABCDEF";    //All possible hex digits
  cout << "Enter a series of numbers between 0 and 15"
    << " separated by spaces.\nHit ENTER when finished: " << endl;

  string result;        //will hold the resulting hexify-ed string
  string::size_type n;        //hold numbers from the input

  while (cin >> n)
    if (n < hexdigits.size())    //ignore invalid input
      result += hexdigits[n];    //fetch the individual hex digit

  cout << "Your hex number is: " << result << endl;
  return 0;
}

Exact same question was asked in stackoverflow,


Code:
// https://stackoverflow.com/questions/19307979/cin-in-a-while-loop
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
  const string hexdigits = "0123456789ABCDEF";

  cout <<  "Enter a series of numbers between 0 and 15"
       << " separated by spaces. Hit ENTER when finished: "
       << endl;

  string line;
  string result;

  if (getline (cin, line))    // get the whole line
    {
      istringstream iss(result);    // break them by spaces
      int i;
      while (iss >> i)
    {
      result += hexdigits[i];
      result += " ";
    }
      cout << "Your hex result:  " << result << endl;
    }
  else
    {
      cout << "Error handling input!" << endl;
    }

  return 0;
}

1) which used getline() and I can't get the logic flow and the posted code did not work as expected; and
2) it seems there may be better way for this basic trick.
My question is what is the best practice to stop reading space delimited numbers at newline instead of Ctrl+D or by hitting any invalid char?
Thanks!

Last edited by yifangt; 2 Weeks Ago at 01:42 PM.. Reason: typos and reformat
Sponsored Links
    #2  
Old Unix and Linux 2 Weeks Ago   -   Original Discussion by yifangt
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 11 December 2017, 5:38 PM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
The >> operator does not care at all about lines. If you want lines, you'll have to separate it some other way, which is exactly what the stackoverflow code does.

It reads entire lines with getline, then crams the entire line into a stringstream object. A stringstream, unlike standard input, will just hit EOF when it runs out of data instead of waiting for more.
The Following 2 Users Say Thank You to Corona688 For This Useful Post:
dodona (2 Weeks Ago), yifangt (2 Weeks Ago)
Sponsored Links
    #3  
Old Unix and Linux 2 Weeks Ago   -   Original Discussion by yifangt
yifangt's Unix or Linux Image
yifangt yifangt is offline VIP Member  
UNIX.COM VIP Member
 
Join Date: Sep 2009
Last Activity: 7 December 2017, 5:47 PM EST
Location: Saskatchewan, Canada
Posts: 516
Thanks: 332
Thanked 8 Times in 8 Posts
Thanks Corona688!
I did not forget your advice on fgets() + sscanf() in C. Switched from there I was hoping to get similar advice in C++ so that I used "best practice". I am aware this is about very basic I/O stream that I'm still struggling with.
Spend more time on this, and found out my confusion also related to this line of the stackoverflow code, which seems wrong to me:

Code:
istringstream iss(result);  // break them by spaces, Wrong code?
istringstream iss(line);    //This gave me what I expected.

My question seems answered.
Thanks again!

Last edited by yifangt; 2 Weeks Ago at 06:17 PM.. Reason: Formating to get better view
The Following User Says Thank You to yifangt For This Useful Post:
Corona688 (2 Weeks Ago)
    #4  
Old Unix and Linux 2 Weeks Ago   -   Original Discussion by yifangt
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 11 December 2017, 5:38 PM EST
Location: Saskatchewan
Posts: 22,546
Thanks: 1,159
Thanked 4,286 Times in 3,955 Posts
It's the exact same problem as fgets, too.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read Embedded Newline characters with read (builtin) in KSH93 a_programmer Shell Programming and Scripting 6 01-11-2011 02:56 PM
how to stop the message on the console samnyc UNIX for Dummies Questions & Answers 2 11-17-2009 04:09 PM
read numbers from file and output which numbers belongs to which range thepurple Shell Programming and Scripting 6 05-13-2009 07:07 AM
Red stop signs in Solaris Management Console 2.1 kayroreality Solaris 1 02-15-2008 05:20 PM
Script does not stop when doing a read rooseter Shell Programming and Scripting 7 11-05-2007 01:46 PM



All times are GMT -4. The time now is 01:47 PM.