Trying to understand more about bitset in c++ - c++

I was trying to understand more about using bitset to convert a string into binary then transfer it into another string rather then just cout straight. I will always receive an exception error "Unhandled exception at 0x773D3DB2 C++ exception: std::invalid_argument at memory location 0x006FF800.
" using visual studio 2017 may i know why?
#include <string>
#include <bitset>
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
std::string hello = "C";
std::string yellow;
for (auto character : hello)
{
yellow += std::bitset<7>(hello).to_string();
cout << yellow;
return 0;
}
}

You are guilty of not having warnings enabled for your compiler, or using a compiler not capable of warning you that the variable character is not used in your loop.
Change
yellow += std::bitset<7>(hello).to_string();
to
yellow += std::bitset<7>(character).to_string();
Output:
1000011
Also, note that you don't actually loop, because your loop returns on its first iteration. I assume that your code example has gone through several iterations of trial & error by this point...

Related

Why is there no error for this code

According to Stroustrup Programming.. the below code should produce a range error. The error is in the last line i<v.size() instead of i<=v.size.
I ran the code and it outputs the contents of the vector including v[v.size] instead of throwing an exception. I am using CodeBlocks with mingw.
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
vector<int>v;
for (int x;cin>>x;) v.push_back(x);
for (int i=0;i<=v.size();i++) cout << "\nv[" << i << "]==" << v[i];
}
What error did you expect? There is no compilation error here, but there is a runtime error. When printing the read values it will always go one step too far. If you read 2 values it will print indexes 0, 1 and 2.
This can be fixed by changing i<=v.size() to i<v.size().
It is very common in indexed for-loops to always follow the following pattern; then you know you'll never be wrong:
for (int i = 0; i < count; i++) {
...
}

Output always 65535 characters?

This is my code.
#include <iostream>
using namespace std;
int main()
{
string s;
cin >> s;
cout << s.length();
return 0;
}
why it gives output as 65535 even if i increase characters in s.
I have added the samle input here https://ideone.com/c2V8YX
The Ideone FAQ answers:
What is the size limit for the source code, input and output?
64 kB.
Note that this limitation has nothing to do with C++ language, or even the particular implementation of it. The limitation is by Ideone (and it's understandable. You wouldn't want to allow people to upload unlimited data to fill up your server). It appears that their behaviour is to silently truncate the input.
This is not exactly wonderful code but the general idea seems sound. Basically reading chunks and appending them (probably to a buffewr as opposed to a raw string) but ya get the idea
#include <iostream>
using namespace std;
int main() {
string all;
char snippet[100];
while(cin.get(snippet, 100)){ all += snippet; }
cout<< all.length() << '\n';
return 0;
}

C++ - Looping through vector runs infinitely

I'm new to C++, came from Java (started learning yesterday).
I'm trying to loop over the elements of a vector. For some reason when I do this, it endlessly outputs empty lines.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string expression = "5+4";
std::vector<char> characters(expression.begin(), expression.end());
for (char c : characters) {
std::cout << c << std::endl;
}
}
I keep running into this problem, I have no clue why it is doing this.
I tried searching if other people had this problem, but I couldn't find any questions about it or answers... what am I doing wrong?
EDIT:
I'm using MinGW
GIF or it didn't happen
I'm not an expert in CPP, but i assume your code is inserting string to the vector and iterate through the vector, if so, heres the code :
#include <iostream>
#include <string>
#include <vector>
using namespace std; //i add this line to remove the "std" in every line
int main()
{
string expression = "5+4";
vector <char> characters(expression.begin(), expression.end());
for (int i=0; i< characters.size(); i++)
{
cout << characters[i] << endl;
}
}
An explanation that i can tell you is, you do not printing the vectors with its index so it'll have some bug also the looping i use i think more clear to see.

Output letters one at a time in C++?

I've seen a couple of solutions to this question, but I'm still running into an issue. I've tried several of the solutions I've seen on this site, but I keep having the same issue. I'm not sure how to make this work, and I'm also not sure why it's not working, since I'm really new to C++.
A brief explanation of what I am attempting to do: I'm writing a simple old-school text-based story/game for a video game design club at my school, and at several points I have the user make decisions, or input things such as their name, which is then used in an output line, as it would be referenced several times. I have had no problem doing that, using something simple like this:
#include <iostream>
#include <string>
#include <limits>
#include <windows.h>
using namespace std;
int main(){
string name, place ;
cout << "???: What is your name? ";
getline (cin, name);
cout << "???: Hello, " << name << "!\n" << "\n";
}
My problem is that I'd like to have the text appear one character at a time, like dialogue, but whenever I try to use something I've seen written by others, it doesn't seem to like it very much. The only one that I tried that I can now find again is this:
#include <iostream>
#include <unistd.h> // include <windows.h> on windows
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
usleep(60000); // use Sleep on windows
}
}
int main()
{
type_text("Hej hej hallÄ!");
}
Apparently there is some sort of conflict regarding my attempt to output the name back to the user when I try to use that code with what I've written. I'm not really sure what the problem is, since I'm so new to C++, can anyone help me out?
Consider using std::this_thread::sleep_for, as it is standard C++11. Example:
#include <iostream>
#include <thread> // standard C++11
// function to output as if it was being typed
void type_text(const std::string& text)
{
// loop through each character in the text
for (std::size_t i = 0; i < text.size(); ++i)
{
// output one character
// flush to make sure the output is not delayed
std::cout << text[i] << std::flush;
// sleep 60 milliseconds
std::this_thread::sleep_for(std::chrono::milliseconds(60));
}
}
int main()
{
type_text("Hello, World!");
}
If you have access to a C++14 compiler, you can simply make use of the std::chrono user-defined literals and have a more "natural" syntax:
using namespace std::literals;
std::this_thread::sleep_for(60ms);

No match for 'operator>>'

I can't figure out why it says this. I am new as you can probably tell... here is the code:
#include <iostream>
using namespace std
int main() {
if (cin >> "hi"
cout << "hello"
return 0;
}
"The thing you were using" (read: your compiler) wanted you to end your using namespace std statement with a semicolon, not to dump one at the start of a function definition.
Your code has a number of extreme and baffling syntax errors, to the extent that it's not even clear what you're trying to accomplish.
Below is a hint to get you started but, from now on, I strongly recommend that you read a good, peer-reviewed C++ book and learn the language before asking any further questions about nonsense code!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
getline(cin, input);
if (input == "hi") {
cout << "hello" << endl;
}
return 0;
}