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;

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

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

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

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.

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' ;
}

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...

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

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.

thank you I got it! appreciate the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.