I've no idea how to make the following.
I would like to enter a sequence of numbers and when I enter 0, to stop the cin (like I clicked Enter) and automatically cout only those numbers who (number%2==0) on another line. Is it posible to be done with function?
I hope you understood me :)
For example I'm entering 123456785435430 (I've entered '0', so the cin stop immideately and cout me 2 4 6 8 4 4 3)
Here is a simple version that works with a given delimiter,
#include <iostream>
using namespace std;
string input_until_delimiter (char delimiter)
{
string buffer = new string ();
char c = delimiter;
while ((c = get()) != delimiter) buffer += c;
return buffer;
}
You'll need something along the lines of this:
char x;
std::vector<int> evens;
do {
std::cin.get(x);
int v = x - '0'; // int value
if (v % 2 == 0 and v != 0)
evens.push_back(v);
} while (x != '0');
for (std::vector<int>::iterator it = evens.begin(); it != evens.end(); ++it)
std::cout << (*it) << " ";
Here's the version for your exercise:
#include <vector>
#include <iostream>
void brc() {
int x;
std::cin >> x;
if (x == 0) return;
if (x % 2 == 0)
std::cout << x << " ";
brc();
}
int main() {
brc();
return 0;
}
Related
This question already has answers here:
std::cin.getline( ) vs. std::cin
(5 answers)
C++ "cin" only reads the first word [duplicate]
(5 answers)
Closed 2 years ago.
I have a script here that counts even numbers in an int type var.
#include <iostream>
#include <string>
int main()
{
std::cout << "Type a string: " << std::endl;
std::string s;
std::cin >> s;
unsigned int digits = 0, evens = 0;
for ( std::string::size_type i = 0; i < s.size() && s[i] >= '0' && s[i] <= '9'; i++ )
{
++digits;
evens += ( s[i] - '0' ) % 2 == 0;
}
std::cout << "The number has " << digits << " digit(s)." << std::endl;
std::cout << "The number has " << evens << " even digit(s)." << std::endl;
return 0;
}
Im trying to find a way to turn this into a string instead so I can count on how many even numbers or numbers are there in that string?
Type a string:
29 coaches 28
Even: 1
Found Numbers: 1
In python it should be something like:
s = "75,41,14,8,73,45,-16"
evenNumbers = []
for number in s.split(","):
int_num = int(number)
if int_num % 2 == 0 and int_num > 0:
evenNumbers.append(int_num)
print("Even Numbers : \"{}\"".format(evenNumbers))
But I dont know how to do it in C++
Your loop will stop on first non-numeric character: i < s.size() && s[i] >= '0' && s[i] <= '9'
Also, you are counting also alphanumerics too. So your loop needs fixing first. I recommend using regex for such problems it will be much generic:
const std::regex numbers("\\-?\\d+");
for (auto it = std::sregex_iterator(s.begin(), s.end(), numbers); it != std::sregex_iterator(); it++) {
++digits;
char lastCharacterOfMatchingString = *it->str(0).rbegin();
evens += (lastCharacterOfMatchingString - '0') % 2 == 0;
}
This sounds like 3 requirements:
split a string based on a space: see How do I iterate over the words of a string?
try convert the words to numbers - can use std::stoi() for this
count the even numbers - can use modulus for this
So something like:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
int main()
{
//0 - Get input
std::cout << "Type a string: " << std::endl;
std::string s;
std::getline(std::cin, s) ;
//1 - split
std::istringstream iss(s);
std::vector<std::string> words{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
std::vector<int> res; //store the results as per the python example
for(auto it: words){
int val = 0;
try{
val = std::stoi(it, nullptr); //2 - convert
}
catch (const std::invalid_argument& ia) { /* what to do with words */ }
if ((val != 0) && (val % 2 == 0))
res.push_back(val); //3 - count
}
std::cout << "even numbers: " << std::to_string(res.size()) << std::endl;
return 0;
}
Let's imagine my program needs input from the user at different times.
I want this input to prevent flushing the cout buffer.
Can I set cin and cout on different stream buffers?
Example in question: a program that reads two numbers in a line, n1 n2, and depending on the first number being either 0, 1, or 2:
n1 = 0: writes the second number n2 to a vector v
n1 = 1: outputs v[n2] in cout
n1 = 2: pop_back() on v
The MWE is:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int size, n1, n2;
vector<int> v;
cin >> size;
while(size--){
cin >> n1;
if (n1 == 0)
{
cin >> n2;
v.push_back(n2);
}
else if (n1 == 1)
{
cin >> n2;
cout << v[n2] << '\n';
}
else if (n1 == 2)
v.pop_back();
}
return 0;
}
Say I have this test input
8
0 1
0 2
0 3
2
0 4
1 0
1 1
1 2
correct output should be
1
2
4
The program above yields outputs interspersed with the input lines instead.
But I would like them all printed together at end program, without using different means e.g. storing them in some container etc.
So I think I should operate on the buffer, but how?
You could write to your own std::stringstream buffer, and then output that to std::cout when you're ready.
MWE:
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <vector>
using std::cin;
using std::cout;
using std::istream;
using std::runtime_error;
using std::stringstream;
using std::vector;
static auto get_int(istream& in) -> int {
int n;
if (!(in >> n)) {
throw runtime_error("bad input");
}
return n;
}
int main() {
auto ss = stringstream();
auto v = vector<int>();
auto size = get_int(cin);
while(size--) {
auto n1 = get_int(cin);
if (n1 == 0) {
auto n2 = get_int(cin);
v.push_back(n2);
} else if (n1 == 1) {
auto n2 = get_int(cin);
ss << v[n2] << '\n';
} else if (n1 == 2) {
v.pop_back();
}
}
cout << ss.str();
}
No need to modify the buffer. Instead of cout << v[n2] you can store v[n2] in a second vector and print it out on the outside of the loop.
I'm Matt, first time posting. I'm in school and learning c++ at the moment and I'm stuck on this problem. I can't seem to find the solution so I'm looking for help.
#include <iostream>
using namespace std;
int main()
{
char ch;
cin >> ch;
if(ch <= 122){
cout << ++ch;
cout << ++ch;
}else if (ch > 122){
cout << static_cast<char>(97)++ch;
cout << static_cast<char>(97)++ch;
}
}
The program is very basic. All it needs to do is be fed a lowercase letter and the program just needs to spit out the next two characters. My problem is that after 'z' I don't know how to wrap back around to 'a'. I've tried to static_cast but it says I can'. I've tried reassigning the variable and it says I can't. I've tried several other basic things but none seem to work.
Thank you in advance for the help!
First, don't use magic numbers like 122 and 97. Use the actual character value.
Second, just declare a string abcdefghijklmnopqrstuvwxyz, and index into that string. This eliminates the need for 122, 97, or any other number. Not only that, you can probably see how to do the problem much easier when dealing with indices such as 0, 1, 25, etc. instead of 122, 97, etc.
Once you do that, a little bit of insight shows that the next two characters will be at position (if positions start at 0), (index + 1) % 26 and (index + 2) % 26. The % is the modulus operator, and it returns the remainder after a division is done.
For example, if the current character is y, the yis located at position 24 of the string. So
(24 + 1) % 26 = 25 % 26 = 25
and
(24 + 2) % 26 = 26 % 26 = 0
So the next two characters are situated at position 25 and position 0, which are z and a.
Take another example: z:
(25 + 1) % 26 = 26 % 26 = 0
and
(25 + 2) % 26 = 27 % 26 = 1
So the next characters after z are a and b.
Basically, when you get an assignment where the data "wraps around" to 0, then the word "remainder" or "modulo arithmetic" should immediately come to mind.
So the final program would look like this:
#include <iostream>
int main()
{
char ch;
const char * alphabet = "abcdefghijklmnopqrstuvwxyz";
std::cin >> ch;
int position1 = ch - 'a'; // get position of input character
int position2 = (position1 + 1) % 26; // get position of next character
int position3 = (position1 + 2) % 26; // get position of next next character
// output results
std::cout << ch << alphabet[position2] << alphabet[position3];
}
Live Example
(assuming that the input is: 'a' - 'z')
Keep It Simple
Solution 1:
#include <iostream>
int main()
{
char ch = 0;
std::cin >> ch; // "fed a lowercase letter"
// "spit out the next two characters"
if (ch < 'y')
std::cout << ++ch << ++ch;
else if (ch == 'y')
std::cout << "za";
else // (ch=='z')
std::cout << "ab";
}
Solution 2:
#include <iostream>
int main()
{
const char * lut = "abcdefghijklmnopqrstuvwxyzab";
char ch = 0;
std::cin >> ch; // "fed a lowercase letter"
ch -= 'a'; // lowercase letter to index
// "spit out the next two characters"
std::cout << lut[++ch] << lut[++ch];
}
Try to use the following code for your problem.
#include <iostream>
using namespace std;
int main()
{
char ch = '\0';
cin >> ch; // "fed a lowercase letter"
char c_next;
c_next = (ch-'a'+1)%26+'a';
cout <<c_next;
c_next = (ch-'a'+2)%26+'a';
cout << c_next;
return 0;
}
Here is one way at going about tackling your problem that is clean and elegant. It is very readable that uses a look up table, converts caps to lowercase using a bit of modulo arithmetic; it also leverages some of the newer features of modern C++ such as range loops.
#include <iostream>
#include <ccytpe> // needed for ::tolower
int main() {
// ascii a-z [97,122]
char alphabet[26] = {}; // 0 initizlize this will be a look up table
int i = 97;
for( auto & c : alphabet ) {
c = static_cast<char>( i );
i++;
}
// check to see if our table is correct
for( auto & c : alphabet ) {
std::cout << c << " ";
std::cout << '\n';
}
std::cout << '\n';
// Alphabet Seems to be fine.
char c = {};
std::cout << "Please enter a lower case character: ";
std::cin >> c;
if( c >= 'A' && c <= 'Z' ) {
::tolower( c ); // make sure that it's not in caps
} else if( c >= 'a' && c <= 'z' ) {
// nothing to do
} else {
std::cout << "Error: input value\n";
return -1;
}
// Now that we have the correct inputs we can show your next two characters.
// Since we know that the ascii table has a range of [97,122] for
// lower case letters and that our array index starts at 0; what we can do
// is a little bit of arithmetic to take the input character and set that
// to the index value of the array above. Then use the array indexing to
// output the next 2 characters. To do this we simply just need to subtract 97 or 'a'
c = c - 'a';
// Now we can print the two lines using the adjusted c value with
// a little bit of modulo arithmetic using the stride, size, or
// length of the alphabet.
int stride = 26;
std::cout << alphabet[++c % stride] << '\n';
std::cout << alphabet[++c % stride] << '\n';
// And we are done!
return 0;
}
This is what the code would look like without the comments and the code to print the whole alphabet:
#include <iostream>
#include <cctype>
int main() {
char alphabet[26] = {};
int i = 97;
for( auto & c : alphabet ) {
c = static_cast<char>( i );
i++;
}
char c = {};
std::cout << "Please enter a lower case character: ";
std::cin >> c;
if( c >= 'A' && c <= 'Z' ) {
::tolower( c );
} else if( c >= 'a' && c <= 'z' ) {
// nothing to do
} else {
std::cout << "Error: input value\n";
return -1;
}
c = c - 'a';
int stride = 26;
std::cout << alphabet[++c % stride] << '\n';
std::cout << alphabet[++c % stride] << '\n';
return 0;
}
I'm writing a program that accepts a card rank as an input, then coverts these ranks into the values they represent. So some examples would include A, 5, 10, K. I've been trying to figure out ways to do this.
I thought about accepting it as a char, then converting it, like so...
char input = 0;
std::cin >> input;
if(input < 58 && input > 49) //accepting 2-9
{
//convert integers
}
else if(input < 123 && input > 64)
{
//convert characters and check if they're valid.
}
And that would work...except for 10 unfortunately. What's an option that works?
Why not use the code you have, and just have a special case, in a third if block, to handle 10?
Since there's no valid input besides 10 that starts with a 1, this should be pretty straightforward:
char input = 0;
std::cin >> input;
if(input < 58 && input > 49) //accepting 2-9
{
//convert integers
}
else if(input < 123 && input > 64)
{
//convert characters and check if they're valid.
}
else if(input == 49){ //accepts 1
std:cin >> input; //takes a second character
if(input == 48){ //this is 10
//do stuff for 10
}
else{
//throw error, 1 followed by anything but 0 is invalid input
}
}
Why not use std::regex when we're in 2016? #Michael Blake, is it a hard requirement to implement the parsing by hand?
I've been able to achieve the desired effect like so:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::regex regexp("[KQJA2-9]|(10)");
std::string in;
for (;;) {
std::cin >> in;
std::cout << (std::regex_match(in, regexp) ? "yes" : "no") << std::endl;
}
}
We should use char array of size 2 because we can not store 10 in a char. Here is the sample program :
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>
using namespace std;
int main()
{
char s[2];
cin >> s;
if( (s[0] < 58 && s[0] > 48) && ( s[1] == '\0' || s[1] == 48) )
{
int m;
m = atoi(s);
cout << "integer " << m << endl;
}
else if(s[0] < 123 && s[0] > 64)
{
char c;
c = s[0];
cout << "char " << c << endl;
}
else
{
cout << "invalid input" << endl;
}
return 0;
}
Alright so I've been looking though Google and on forums for hours and can't seem to understand how to solve this problem.
I need to write a program that first determines if the number entered by the user is a base 5 number (in other words, a number that only has 0s, 1s, 2s, 3s, and 4s in it). Then, I have to count how many 0s, 1s, 2s, etc are in the number and display it to the user.
I've seen people saying I should convert int to a string and then using cin.get().
I noticed that I can't use cin.get() on a string, it needs to be a char.
I can only use a while loop for this assignment, no while... do loops.
Any help is appreciated!!
Here's what I have so far, obviously with all my mistakes in it:
//----------------------------------------------
// Assignment 3
// Question 1
// File name: q1.cpp
// Written by: Shawn Rousseau (ID: 7518455)
// For COMP 218 Section EC / Winter 2015
// Concordia University, Montreal, QC
//-----------------------------------------------
// The purpose of this program is to check if the
// number entered by the user is a base of 5
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
// Declaring variables
int number;
int zeros;
int ones;
int twos;
int threes;
int fours;
bool base5;
// Get data and calculate
cin >> number;
string numberString = to_string(number);
// Determine if the number is a base 5 number
while (cin.get(numberString) == 0 || cin.get(numberString) == 1 ||
cin.get(numberString) == 2 || cin.get(numberString) == 3 ||
cin.get(numberString) == 4)
base5 = true;
// Determine the number of each digits
zeros = 0;
ones = 0;
twos = 0;
threes = 0;
fours = 0;
return 0;
}
Several things you need to beware of:
One way to get a specific character from a std::string is by []. e.g.
std::string myString{"abcdefg"};
char myChar = myString[4]; // myChar == 'e'
cin.get(aString) is not trying to get data from aString. It continues to get data from stdin and store in aString. Once you have get the data and put into the string, you can simply manipulate the string itself.
a short piece of code that will count number of vowel in a string. If you can understand it, there should be no problem doing your work. (Haven't compiled, probably some typos)
std::string inputString;
std::cin >> inputString;
// you said you need a while loop.
// although it is easier to with for loop and iterator...
int i = 0;
int noOfVowels = 0;
while (i < inputString.length()) {
if (inputString[i] == 'a'
|| inputString[i] == 'e'
|| inputString[i] == 'i'
|| inputString[i] == 'o'
|| inputString[i] == 'u' ) {
++noOfVowels;
}
++i;
}
std::cout << "number of vowels : " << noOfVowels << std::endl;
Well you can try this approach. This will solve your needs I guess.
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int number=0;
int zeros=0;
int ones=0;
int twos=0;
int threes=0;
int fours=0;
bool valid=true;;
int counter = 0;
cout<<"Enter the number: ";
cin >> number;
stringstream out;
out << number; //int to string
string numberString = out.str();
cout<<"\n\nNumber after string conversion : "<<numberString;
cout<<"\nPrinting this just to show that the conversion was successful\n\n\n";
while (counter < numberString.length())
{
if (numberString[counter] == '0')
zeros++;
else if(numberString[counter] == '1')
ones++;
else if(numberString[counter] == '2')
twos++;
else if(numberString[counter] == '3')
threes++;
else if(numberString[counter] == '4')
fours++;
else
valid=false;
counter++;
}
if(valid==true)
{
cout<<"\nZeros : "<<zeros;
cout<<"\nOnes : "<<ones;
cout<<"\nTwos : "<<twos;
cout<<"\nThrees : "<<threes;
cout<<"\nFours : "<<fours;
}
else
cout<<"\n\nInvalid data...base of 5 rule violated";
_getch();
return 0;
}
If you don't want to use std::string then use characters, first loop over the input from the user until ENTER is pressed.
char ch = 0;
while ((ch = cin.get()) != '\n')
{
...
}
For each character read, check if it is a digit (std::isdigit) and if it is in the range 0..4, if not quit and give some message of not being base 5
have an array of ints to keep track of the frequency of the digits
int freq[5] = {0,0,0,0,0};
after you have checked that the character is valid subtract the ascii value from the digit and use that as index in the array, increment that:
freq[ch - '0']++;
e.g.
char ch;
int freq[5] = {0};
while ((ch = cin.get()) != '\n')
{
cout << ch;
freq[ch-'0']++;
}
for (int i = 0; i < sizeof(freq)/sizeof(freq[0]); ++i)
{
cout << static_cast<char>(48+i) << ":" << freq[i] << endl;
}
Here's a useful function that counts digits:
// D returns the number of times 'd' appears as a digit of n.
// May not work for n = INT_MIN.
int D(int n, int d) {
// Special case if we're counting zeros of 0.
if (n == 0 && d == 0) return 1;
if (n < 0) n = -n;
int r = 0;
while (n) {
if (n % 10 == d) r++;
n /= 10;
}
return r;
}
In your code you can use this naively to solve the problem without any further loops.
if (D(n, 5) + D(n, 6) + D(n, 7) + D(n, 8) + D(n, 9) != 0) {
cout << "the number doesn't consist only of the digits 0..4."
}
cout << "0s: " << D(n, 0) << "\n";
cout << "1s: " << D(n, 1) << "\n";
cout << "2s: " << D(n, 2) << "\n";
cout << "3s: " << D(n, 3) << "\n";
cout << "4s: " << D(n, 4) << "\n";
You could also (or should also) use loops here to reduce the redundancy.