Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I want to let the users input strings with more than 4,095 characters, but std::cin/std::istream seems to have some sort of undocumented limit and keeps cutting off the string to only allow 4,095 characters.
Is there some way to get past this "limit" using one of the many input functions, preferably allowing any length? There might not be a limit in the cpp standard, but the operating system I am currently using may have an input buffer limit - Arch Linux.
The strings may reach lengths of 1 ≤ L ≤ 10e6 where L is the length of the string.
int main() {
/* The user inputs a string with more than
4095 characters.
Let's say that every character in the string
is 'a' except for the 4095th character in the
string, which is 'B', and the end of the string
looks like "...aaBaaaaaaaaaaaaa"
The string will get cut off to "...aaB" (4095
characters) meaning that there seems to be some
sort of limit. */
std::string str;
/* Manually paste a string that has 10,000
characters into a terminal emulator (Alacritty)
with CTRL+C → SHIFT+INSERT */
std::cin >> str;
/* This next line is supposed to output 10,000,
but instead, it outputs 4,095. */
std::cout << str.length() << std::endl;
}
The provided example should be simple enough to understand without needing a better reproducible example. Providing a better example would only clutter the post with a lot of unneccessary information since it should be simple enough as it is.
The input data I give it looks like this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa but imagine 10,000 a's and it does not have any whitespaces.
Note: This program is compiled with this command:
g++ -g -O2 -std=gnu++17 -static light.cpp && ./a.out
Related
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 5 years ago.
Improve this question
I want to build a program to save text to files , but I want my program to secure or encrypt the content of the text , for example , if the user input "salamence" to the program , the program would output (into a file) "hjkjupfqp" or something like that so people can't read it unless they have access to the program (I want the program to be able to decrypt the text file too) so it is possible in c++ to read strings input one by one character and modify them into another characters , and how to do that ?
A string is a sequence of chars put in a container that has other stuff in it. The chars themselves can be accessed through the [] operator. A char is basically an 8-bit integer that can be displayed. An integer can be manipulated arithmetically(+,-,*,...), bit-wise(&,^,|,<<,...), etc.
So you could do something like this:
#include <iostream>
#include <string>
using namespace std; //bad idea, but simplifies stuff
int main(){
string s;
cin>>s; //reads the string
for(int i=0;i < s.size;i++){ //loops through all characters of the string
s[i]++; //adds one to the string
}
cout<<s; //outputs the modified string
}
This will turn "abc" into "bcd", wich is a rather stupid form of encryption, but it proves the concept.
To decrypt you would need to copy the loop, but replace s[i]++ with s[i]--.
Since you seem to be a beginner, I would actually recommend using c-style strings, but that is outside the scope of this question.
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 5 years ago.
Improve this question
I have a question, after I loop on a file and extract several letters with a counter to know how many characters have been extracted how can I reposition my pointer to point back the first one extracted. Here is what I have tried so far:
int get_length(ifstream &inp,int &length){
int columns=0;
inp>>columns;
length++;
while(columns!=0)
{
inp>>columns;
length++;
}
if (!inp.good())
inp.clear();
inp.seekg(-length,std::ios::cur);
return length;
}
For some reason its not going back the same length, it's getting it wrong by one, I've tried adding to length by one then writing that seek function I don't know what's wrong here, I'm questing if I'm using the seek function incorrectly?
I think that the problem is this:
You are incrementing 'length' each time an integer value is read from the fstream 'inp'. Depending on on how many characters wide the integer representation is you will need to increment length by that amount. That and new-line chars and any other whitespace in the fstream.
If your test data contains:
10
11
12
13
Then by the time you read 13 you will have consumed 12 bytes of file data.
Your counter will have only incremented 4 times.
You could do this more easily and accurately by placing a call to
auto const position_start = inp.tellg();
at the start of your function and once you read the data you're interested in 'rewind' to the start position with a call to
inp.seekg(position_start, std::ios::beg);
'ifstream' is a specialisation of 'basic_ifstream', so 'ifstream::seekg()' takes an offset in bytes (chars). However, the formatted input (to an int) will advance the the current position by some number of bytes (0 or more) as it converts the input to an integer value. Use 'ifstream::tellg()' at the top of the function to get the current file position and another call to 'tellg()' before calling 'seekg()' to get the new file position. The difference in the two values will be 'length'.
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 6 years ago.
Improve this question
I have a C program that reads from keyboard, like this:
scanf("%*[ \t\n]\"%[^A-Za-z]%[^\"]\"", ps1, ps2);
For a better understanding of what this instruction does, let's split the format string as follows:
%*[ \t\n]\" => read all spaces, tabs and newlines ([ \t\n]) but not store them in any variable (hence the '*'), and will keep reading until encounter a double quote (\"), however the double quote is not input.
Once scanf() has found the double quote, reads all caracters that are not letters into ps1. This is accomplished with...
%[^A-Za-z] => input anything not an uppercase letter 'A' through 'Z' and lowercase letter 'a' through 'z'.
%[^\"]\" => read all remaining characters up to, but not including a double quote into ps2 ([^\"]) and the string must end with a double quote (\"), however the double quote is not input.
Can someone show me how to do the same thing in C++
Thank you
C++ supports the scanf function. There is no simple alternative, especially if you want to replicate the exact semantics of scanf() with all the quirks.
Note however that your code has several issues:
You do not pass the maximum number of characters to read into ps1 and ps2. Any sufficiently input sequence will cause a buffer overflow with dire consequences.
You could simplify the first format %*[ \t\n] with just a space in the format string. This would also allow for the case where no whitespace characters are present. As currently written, scanf() would fail and return 0 if no whitspace characters are present before the ".
Similarly, if no non letters or if no other characters follow before the second ", scanf would return a short count of 0 or 1 and leave one or both destination array in an indeterminate state.
For all these reasons, it would be much safer and predictable in C to first read a line of input with fgets() and use sscanf() or parse the line by hand.
In C++, you definitely want to use the std::regex package defined in <regex.h>.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to write a simple hangman game in c++ by randomly selecting a word from a list, checking the string length, and writing that many *s into a new string to serve as placeholders in the yet un-guessed word. The max length is 9 letters. I have the game working almost flawlessly -- the problem is that whenever my word has 8 or 9 letters, the program prints the correct number of *s followed by one or two � characters. Research tells me these are unprintable characters, but I've tried for a while now and I'm not sure why they're here, why they only show up with a word length>7, or how to get rid of them. Below is relevant code. Any suggestions?
Generating *s:
char word[80];
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word[i]='*';
}
You forgot to add the \0 terminator at the end of the string. After the for loop, add:
word[i] = '\0';
Or, best, use std::string instead of a C string.
Try using std::string instead.
std::string word;
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word+='*';
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've been asigned with a homework, but I don't know what to do exactly.
Input file is randomly generated from letter from 'a' to 'z'. User will type a mask only with 0/1, for example 011011, where 1 means vowel and 0 means consonant.
Output will be all matches found in input that match the user-given mask (for example for 011011 output will be abbezz).
Any idea how to make this? I don't ask for code, but only for easiest way how to make this possible in c/c++.
Thanks
My understanding is that the requirements are to find all sequences of letters that match the mask.
Given the mask: 010 (consonant, vowel, consonant)
Here are the matches for a couple of words:
"are" - fail, first character is a vowel.
"mat" - pass, 'm' is a consonant, 'a' is a vowel, 't' is a consonant.
"mate" - fail, too many letters.
You will need to have some functions that test a letter for vowel or consonant.
Also consider using a state machine. If a test fails, you want to go back to the starting state.
Write down your algorithm first, step by step. Come up with a few test cases to verify your algorithm. After algorithm works, code it up. Use your same test cases for verifying the program.
Map the string to 0/1 according to the rules you have and search for substrings in the new string that match the mask.
I still do not know exactly what you are trying to accomplish, but maybe it helps to know that once you have your input in a std::string, you can iterate over the characters using iterators and any algorithm. Here is an example with std::for_each:
#include <algorithm>
#include <iostream>
#include <string>
void applyMask(std::string::reference aCharacter)
{
// apply your mask here, character by character
if ('a' == aCharacter) {
aCharacter = 'A';
}
}
int main()
{
std::string s("stackoverflow");
std::for_each(s.begin(), s.end(), &applyMask);
std::cout << s << std::endl;
}
You might want to have a look at std::transform, e.g. http://www.cplusplus.com/reference/algorithm/transform/