Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Consider the following code written in C++11:
#include <iostream>
#include <bitset>
#include <cstdint>
int main() {
std::uint64_t a = 0000000000000000000000000000000000000000000000001111111100000000;
std::bitset<64> b(a);
std::cout << b << std::endl;
return 0;
}
The output of the code is :
0000000000000000001001001001001001001001000000000000000000000000
Why this output does not correspond to the a value?
If you want to write a binary number you need to use the 0b prefix.
std::uint64_t a = 0b0000000000000000000000000000000000000000000000001111111100000000;
std::bitset<64> b(a);
Your example fixed and working live
As mentioned your "binary" string is actually an octal representation of a much larger number. Alternative methods would be converting from a string, from the decimal representation or prefixing the string with '0b' to denote that a binary representation follows
#include <iostream>
#include <bitset>
#include <cstdint>
int main() {
std::bitset<64> foo (std::string("0000000000000000000000000000000000000000000000001111111100000000"));
std::uint64_t bar = foo.to_ulong();
std::uint64_t beef = 0b0000000000000000000000000000000000000000000000001111111100000000;
std::bitset<64> dead (beef);
std::cout << foo << std::endl;
std::cout << bar << std::endl;
std::cout << dead << std::endl;
std::cout << beef << std::endl;
return 0;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In a function, a string is passed which has alphabets, numbers and special characters. The function should return only the alphabets from the string.
I tried the following code but it is giving along with the numbers as well. Where am I going wrong?
In this code (using c++11) std::string::find_first_not_of stl algorithm is being used
std::string jobName1 = "job_2";
std::size_t found =
jobName1.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJK
LMNOPQRSTUVWXYZ");
while (found!=std::string::npos)
{
jobName1.erase(found,1);
found=jobName1.find_first_not_of("abcdefghijklmnopqrstuvwxyzAB
CDEFGHIJKLMNOPQRSTUVWXYZ",(found+1));
}
Input:
job_2
Output:
job2
Expected:
job
From your description, this is should do the job:
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string jobName1 = "job_2";
std::regex regex ("[0-9_]*");
auto result = std::regex_replace (jobName1, regex, "");
std::cout << result << std::endl;
return 0;
}
If you're looking for something other than the code you've written, you can use std::remove_if:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string jobName1 = "job_2";
jobName1.erase(std::remove_if(jobName1.begin(), jobName1.end(),
[](char ch){return !std::isalpha(ch);}),
jobName1.end());
std::cout << jobName1;
}
Output:
job
As an alternative to already given std::string::find_first_not_of, std::regex_replace, std::remove_if solutions, std::copy_if can be used to copy the charectors of given string to new one, if it std::isalpha.(credits to #PaulMcKenzie for poting the right algorithum)
(See Live)
#include <iostream>
#include <string>
#include <algorithm> // std::copy_if
#include <iterator> // std::back_inserter
#include <cctype> // std::isalpha
int main()
{
const std::string jobName1{ "job_2" };
std::string result;
std::copy_if(std::cbegin(jobName1), std::cend(jobName1),
std::back_inserter(result),
[](const char chr) noexcept { return std::isalpha(chr); });
std::cout << result;
return 0;
}
Output:
job
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to create a little game in c++, and therefore I need a function to return random numbers in a specific range.
Most of the answers I found were similar to this one https://stackoverflow.com/a/19728404/5780938, and I think this is the solution I'm looking for.
To test if the function does, what I want it to, I tried outputting the results in several different ways.
At the moment my code looks like this:
#include "stdafx.h"
#include <iostream>
#include <random>
int zufälligeZahl();
int main()
{
using std::cin;
using std::cout;
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
return 0;
}
int zufälligeZahl()
{
std::random_device rd;
std::mt19937 zGenerator(rd());
std::uniform_int_distribution<int> uni(1, 13);
int random_integer = uni(zGenerator);
return random_integer;
}
I've tried this in many different ways, but no matter what I do, it doesn't work. Either the output is something like 00A8106E, or I don't get any output at all.
I'm using Visual Studio Community 2015.
You are not calling the function zufälligeZahl, you are printing out the address of the function.
Fix your code by actually calling the function:
cout << zufälligeZahl() << "\n";
You forgot the parentheses.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am working with a src someone made for a game I am making. the person recently has left and I am trying to pick up the pieces, I don't really know c++ but I was hoping to get some help with a compile issue coming from a specific file I am trying to compile. is there a way I can stretch the limits of a const or perhaps use a different type that will hold much larger values?
The numeric limits for the fundamental types of your implementation are defined in the <limits> header, and the trait class std::numeric_limits provides the values for your machine. That's the end of it, you cannot "beat" those limits with fundamental types. Example (from cppreference.com):
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tlowest\thighest\n";
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
Live on Coliru
If you want more than this, or even arbitrary precision/length numbers, then you need to use a multi-precision library, e.g. Boost.Multiprecision.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This code is part of a larger program, i have to pass a string to a function and get a string back but i am having issues with the return type. Why is the following code not generating output Trib 98
P.S newbie here, hope the code posting format is correct
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string>
using namespace std;
string Platin(string x)
{string ans = x + " 98" ;
return ans; }
int main()
{Platin("Trib");}
The call to Platin in main is returning a string, but you never do anything with it. Change int main to
int main()
{
cout << Platin("Trib") << endl;
}
Alternatively, if you don't want to output the return value, and just want to keep it for later use, you can do this
int main()
{
string functionOutput = Platin("Trib");
}
You don't see anything because you didn't say anywhere you wanted to output something. Your line:
Platin("Trib");
just returns a temporary object (string) that is not used anywhere (and that, of course, is not displayed).
In practice, you need to pass the newly string created to the standard output stream, as follows:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string>
using namespace std;
string Platin(string x)
{string ans = x + " 98" ;
return ans; }
int main()
{
cout << Platin("Trib") << endl; // <- Instruct your program to output something
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm not entirely sure what is going on here. I'm guessing because my input is a string and I'm cycling through it one character at a time it is always returning as type char.
I pretty sure a string is actually char*. The only way I can think to fix this is to include and check what type of character it is, but I'd like to avoid doing that. Is there an alternative method using typeid.name() to figure out what the char is?
I'm using gcc compiler
voidQueue outQueue;
string temp = "32ad1f-31f()d";
int i = 0;
while(temp[i] != '\0')
{
outQueue.enqueue(temp[i]);
i++;
}
template<typename T>
void voidQueue::enqueue(T data)
{
T *dataAdded = new T;
*dataAdded = data;
string type(typeid(data).name());
cout<< type;
myQueue::enqueue((void *)dataAdded,type);
}
i want it recognize that char('9') is actually an int
You can use std::isdigit for this:
#include <cctype>
bool digit = std::isdigit(static_cast<unsigned char>(temp[i]);
In your example, T is char and gcc returns "c" for typeid(char).name(), as demonstrated by the following program:
#include <iostream>
#include <typeinfo>
int main() {
std::cout << typeid(char).name() << std::endl;
std::cout << typeid(short).name() << std::endl;
std::cout << typeid(int).name() << std::endl;
std::cout << typeid(long).name() << std::endl;
}
On my compiler, this prints out
c
s
i
l
Given that the name() strings are implementation-defined, this is compliant behaviour.