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