I want to make the program that counts the number of each digit of some number.
The number is multiple of three integer input from console. I convert the number to string for counting and used count function.
#include<iostream>
#include<algorithm>
#include<string>
#include<stdlib.h>
using namespace std;
int main() {
int a;
int b;
int c;
cin >> a;
cin >> b;
cin >> c;
int multi = a * b * c;
string str_multi = to_string(multi);
for (int a = 0; a <= 9;a++) {
char* tmp;
_itoa_s(a, tmp,2, 10);
cout << count(str_multi.begin(), str_multi.end(), tmp) << endl;
}
}
And I get the errorcode C2446.
The error saying,"'==':Not converted from 'const_TY' to 'int'"
This error was in the xutility file. I think the problem is count function or
_iota_s function but I don't know how to solve this.
The last parameter to std::count needs to be a char in this case (or be convertible to a char), so just change your loop to:
for (int a = '0'; a <= '9'; a++) {
cout << count(str_multi.begin(), str_multi.end(), a) << endl;
}
Live demo
Related
I have this integer:
4732891432890432432094732089174839207894362154
It's big so I want to delete all digits in it except the digit 4 and I don't know how. This is my code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
unsigned ll n, lastDigit, count = 0;
ll t;
cin >> t;
while(t--)
{
cin >> n;
while (n !=0)
{
lastDigit = n % 10;
if(lastDigit == 4)
count++;
n /= 10;
}
cout << count << "\n";
}
return 0;
}
I used the while loop because I have multiple test case not only that number.
Just to show you current C++ (C++20) works a bit different then wat most (older) C++ material teaches you.
#include <algorithm>
#include <iostream>
#include <string>
#include <ranges>
bool is_not_four(const char digit)
{
return digit != '4';
}
int main()
{
// such a big number iwll not fit in any of the integer types
// needs to be stored in memory
std::string big_number{ "4732891432890432432094732089174839207894362154" };
// or input big_number from std::cin
// std::cout >> "input number : "
// std::cin >> big_number;
// NEVER trust user input
if (!std::any_of(big_number.begin(), big_number.end(), std::isdigit))
{
std::cout << "input should only contain digits";
}
else
{
// only loop over characters not equal to 4
for (const char digit : big_number | std::views::filter(is_not_four))
{
std::cout << digit;
}
// you can also remove characters with std::remove_if
}
return 0;
}
Needle in the haystack. I'm a beginner in programming and we only learned a thing or two so far, barely reached arrays yet.
Input: 1 4325121
Output: 2
Input two values in one line. The first one shall accept any integer from 0-9 and the other one shall take a random positive integer.
Using a while loop, count how many of the first integer (0-9) is present in the digits of the second inputted integer and print the result.
No arrays to be used here, only while loops and else-if conditions with basic coding knowledge and without the use of advanced coding.
As you said, you need to keep it as simple as possible. Then this can be a solution:
#include <iostream>
int main()
{
int first { };
int second { };
std::cin >> first >> second;
int quo { second };
int rem { };
int count { };
while ( quo > 0 )
{
rem = quo % 10;
quo /= 10;
if ( first == rem )
{
++count;
}
}
std::cout << "Result: " << count << '\n';
}
Using while loop
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 4325121;
int count = 0;
while(b > 0)
{
int m = b % 10;
if(m == a)
{
count++;
}
b /= 10;
}
cout << count;
return 0;
}
Nice little problem. But actually, to keep it as simple as possible no calculations are needed at all. I simplified my example, and it just keeps working on the input text, which is 100% sufficient to solve the problem:
#include <iostream>
#include <string>
using namespace std;
int main() {
char digit;
std::string number;
cout << "Input: ";
cin >> digit >> number;
int count = 0;
for (char const character : number)
if (character == digit)
count++;
cout << "Result: " << count << endl;
return 0;
}
Given the question, this code solves the problem.
I'm having an issue with populating a double array, as I keep getting an error. Here is a snippet of the code I'm trying to run:
#include<iostream>
using namespace std;
int main()
{
int n;
double d;
double array_d[1000];
cout << "You have chosen to use Double as your datatype\n";
cout << "Enter the length of the array : \n";
cin >> n;
cout << "\nInput the array elements : \n";
for (d = 0.0; d < n; d++)
cin >> array_d[d];
return 0;
}
error message:[Error] invalid types 'double [1000][double]' for array subscript
Try changing your for loop to use integers:
for (unsigned int index = 0u; index < n; ++index)
{
cin >> array_d[index];
}
The issue is that when d == 0.3333, array_d[0.3333] is kind of hard to address. Array cells are singular and there is no definition of a partial cell in an array.
Also, get into the habit of using braces with all for loops. The compiler will thank you.
My code is supposed to print an "a" amount of numbers (1 < a < n < 100000) which are simultaneously divisible by "x" and indivisible by "y" - all this for "t" amount of data sets.
I've written a code which does just that using only the main() function, but - as I'm learning about functions - I'm trying to rewrite this code to include my own function. For example, if I enter t=1, n=35, x=5 and y = 14, the output should be: "5 10 15 20 25 30".
Code 1 is the code which works fine, only with the main function. Code 2 is the code I'm currently working on which is supposed to include my function "check"
I've managed to rewrite the code 2 to a point where it returns the ASCII symbols corresponding to the numbers I'm supposed to obtain, but I'm having problems converting these symbols into numbers fulfilling my requirements.
When entering "1 7 2 4" as input data, the code returns two symbols instead of "2 6".
Any help with fixing this issue would be very appreciated...
Code 1:
#include <iostream>
using namespace std;
int main()
{
int t, n, x, y;
cin >> t;
for (int i=0; i<t; i++)
{
cin >> n >> x >> y;
for (int a=0; a<n; a++)
{
if ((a%x==0)&&(a%y>0))
cout << a << " ";
}
cout << endl;
}
return 0;
}
Code 2:
#include <iostream>
using namespace std;
string check (int n, int x, int y)
{
string result;
for (int a=0; a<n; a++)
{
if ((a%x==0)&&(a%y>0))
{
result += a;
result += " ";
}
}
return result;
}
int main()
{
int t, n, x, y;
cin >> t;
for (int i=0; i<t; i++)
{
cin >> n >> x >> y;
cout << check (n, x, y) << endl;
}
return 0;
}
What's the problem ?
This is because the following statement is understood by the compiler as if you wanted to add a single char to the string (so the char corresponding to the ascii code of a, if the string encoding is ascii):
result += a;
You may test this behavior of operator+= by trying:
result += 64; // ascii code for #
How to solve it ?
To get the result you expect, you need to convert a explicitly into a string. So change the line to:
result += to_string(a);
Isn't there an easier way ?
Alternatively, if you have a lot of formatting, and if you're comfortable with streams, you may want to consider stringstream:
string check (int n, int x, int y)
{
stringstream result;
for (int a=0; a<n; a++)
{
if ((a%x==0)&&(a%y>0))
{
result << a << " ";
}
}
return result.str();
}
The stringstreams behave as ordinary streams (e.g. cout), except that they write the result into memory. You can then easily transform the end result using the str() member function.
As of late, I've been doing a complete review of C++ and came across a code snippet containing the following:
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a)
{
b+=a;
}
cout << b << endl;
return 0;
}
The code snippet seems very straightforward: it puts input from the console into a and adds this onto b, for as long as valid input is presented. However, whenever I try to run this program with an input of integers, seperated with spaces, it gives a very large negative integer (-1218019327 being the most recent result, when the entire input only consisted of the number '1'). Only when I modify the code does it give correct output:
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a)
{
cout << a << endl;
b+=a;
}
cout << b << endl;
return 0;
}
Why does adding a cout statement change the result of my code so thouroughly (yet positively)?
Both programs result in undefined behavior, you did not initialize b. Try:
int b = 0;
You have to initialize b=0;. Or b will give you garbage value.
#include <iostream>
using namespace std;
int main()
{
int a, b=0;
while (cin >> a)
{
cout << a << endl;
b+=a;
}
cout << b << endl;
return 0;
}
By pressing ctrl-z you will get the value of b.