Programming

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

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


👤 Login to reply

yifangt yifangt is offline VIP Member  
UNIX.COM VIP Member
 
Join Date: Sep 2009
Last Activity: 16 July 2018, 4:28 PM EDT
Location: Saskatchewan, Canada
Posts: 534
Thanks: 352
Thanked 10 Times in 9 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; 01-24-2018 at 04:24 PM.. Reason: typos and reformat
Sponsored Links
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 July 2018, 3:26 PM EDT
Location: Saskatchewan
Posts: 22,727
Thanks: 1,194
Thanked 4,351 Times in 4,006 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 (11-26-2017), yifangt (11-27-2017)
Sponsored Links
yifangt yifangt is offline VIP Member  
UNIX.COM VIP Member
 
Join Date: Sep 2009
Last Activity: 16 July 2018, 4:28 PM EDT
Location: Saskatchewan, Canada
Posts: 534
Thanks: 352
Thanked 10 Times in 9 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; 01-24-2018 at 04:26 PM.. Reason: Formating to get better view
The Following User Says Thank You to yifangt For This Useful Post:
Corona688 (11-27-2017)
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 20 July 2018, 3:26 PM EDT
Location: Saskatchewan
Posts: 22,727
Thanks: 1,194
Thanked 4,351 Times in 4,006 Posts
It's the exact same problem as fgets, too.
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 01:56 PM
how to stop the message on the console samnyc UNIX for Dummies Questions & Answers 2 11-17-2009 03:09 PM
read numbers from file and output which numbers belongs to which range thepurple Shell Programming and Scripting 6 05-13-2009 06:07 AM
Red stop signs in Solaris Management Console 2.1 kayroreality Solaris 1 02-15-2008 04:20 PM
Script does not stop when doing a read rooseter Shell Programming and Scripting 7 11-05-2007 12:46 PM



All times are GMT -4. The time now is 12:54 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?