We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,127 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

cin.peek

What's up guys? I have this question....I'm trying to extract a number from a stream after I extract a charachter
but after charachter it displays ASCII value, not the actual number:

    cin >> ch;
    num1 = cin.peek(); cin.ignore(80, '\n');
    cout << ch << num1;
3
Contributors
9
Replies
1 Day
Discussion Span
6 Months Ago
Last Updated
10
Views
Seni0RRunn3r
Newbie Poster
6 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

try

cin >> ch;
num1 = cin.peek(); cin.ignore(80, '\n');
cout << ch << (char)num1;

as the peek function reads and returns the next character without extracting it, leaving it as the next character to be extracted from the stream. So, if you still have a number, it will consider it a character, so it will display it's ASCII value.
Look up cin.peek(): Click Here

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

Thank you guys for your assitance. I got it now!

Seni0RRunn3r
Newbie Poster
6 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

But hey! I can't figure out how to use the cin.peek for the extracting numbers or charachters from the randomly typed input

Seni0RRunn3r
Newbie Poster
6 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Put the stream into a string, than use that string for your puposes, extract characters, or numbers. To get a number out of a digit character use this function: atoi.

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

I can't figure out how to use the cin.peek for the extracting numbers or charachters from the randomly typed input

std::istream::peek() will retrieve, but will not extract the character from the input buffer (the charaxter remains in the input buffer).
http://en.cppreference.com/w/cpp/io/basic_istream/peek

Use std::istream::get() to read and extract the next character.
http://en.cppreference.com/w/cpp/io/basic_istream/get

#include <iostream>

int main()
{
    // retrieve the next character without extracting it
    using traits_type = std::istream::traits_type ;
    traits_type::int_type c = std::cin.peek() ;
    if( c != traits_type::eof() )
         std::cout << "peeked char is: " << traits_type::to_char_type(c) << '\n' ;

    // read (and extract) the next character
    char ch ;
    if( std::cin.get(ch) ) std::cout << "extracted char is: " << ch << '\n' ;
}
vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11

I would use it, but I have an assignment and I cannot use any charachter arrays or strings... I can only use either putback or peek methods...

Seni0RRunn3r
Newbie Poster
6 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I figured out peek now I can extract number later, however I cannot ignore the other charachters....
1) I need to read the first charachter
2) than I need to read two number, ignore everything in between
taht's what I got

    cin >> ch; 
    num1 = cin.peek(); num2= cin.peek();
    cin >> num1;
    cin >> num2; cin.ignore(80, '\n');
    cout << ch << num1 + num2;

it reads the numbers everything well only if i don't type any charachters between.
I'll try to figure it out (it's damn interesting!) but I don't know If i have much time

Seni0RRunn3r
Newbie Poster
6 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I can only use either putback or peek methods...

std::istream::peek() provides look-ahead for just one character, not more. So what you are trying to do is not possible (portably). You need to use atleast one function that will extract the character just peeked so that you can now peek at the next character in the input buffer - for instance, std::istream::ignore(1) will extract and throw away one character from the input buffer.

I need to read the first charachter

Reading the first character from the input buffer is easy - throw away leading white-space characters, peek to retrieve the first non-whitespace char, and then remove that char from the input buffer.

than I need to read two number, ignore everything in between

Reading in a number is a bit more interesting.
a. Throw away characters one by one till the next character is a digit.
b. Retrieve the first digit character.
c. Remove it from the input buffer.
d. Repeat for each digit in the input immediately following the first digit - read its value, update the value of the number, and remove that character from the input buffer.

#include <iostream>
#include <cctype>

char extract_char()
{
    // throw away leading white space
    while( std::isspace( std::cin.peek() ) ) std::cin.ignore(1) ;

    // retrieve the first non-whitespace char
    int c = std::cin.peek() ;
    std::cin.ignore(1) ; // and remove it from the input buffer

    return char(c) ;
}

inline int extract_first_digit()
{
    // throw away leading non-digit characters
    while( !std::isdigit( std::cin.peek() ) ) std::cin.ignore(1) ;

    // get the digit
    int n = std::cin.peek() - '0' ;
    std::cin.ignore(1) ; // and remove the numeral from the input buffer

    return n ;
}

int extract_number()
{
    int number = extract_first_digit() ;

    while( std::isdigit( std::cin.peek() ) ) // extract trailing digits
    {
        number *= 10 ;
        number += std::cin.peek() - '0' ;
        std::cin.ignore(1) ;
    }

    return number ;
}

int main()
{
    char c = extract_char() ;
    int a = extract_number() ;
    int b = extract_number() ;
    std::cout << c << ' ' << a << ' ' << b << '\n' ;
}

PS.:
Check for eof and handling of a leading minus sign have been elided for brevity.
You should add that functionality on your own for the program to be complete.

vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11

thank you I got it! appreciate the help

Seni0RRunn3r
Newbie Poster
6 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0904 seconds using 2.73MB