How do I read a large input byte-wise into an array? - c++

I'm looking for a way to store a large input, character by character in an array.
For example think user types 324354545434erdfdrr.........6677. For the first part I need to have its length, (think I only wants to count its alphabets not numbers) then I want to create an array based on its length (number of its alphabets), (a[length]), then I need to store the input, character by character in the array.
What situation you will decide me?
I'm thinking about using
getch();
function but don't know how to start.

Why not use string in c++?
#include <string>
#include <iostream>
int main(void)
{
std::string str;
std::cin >> str;
std::cout << str << std::endl;
return 0;
}

Related

Replace each alphabetic character in passCode with '&'

new to programming here. I'm confused as to how I'm supposed to use "isalpha" to figure this out. I have no clue how to start it.
A 2-character string, passCode, is read from input. Replace each alphabetic character in passCode with '&'. Otherwise, passCode is not changed.
Ex: If the input is c4, then the output is:
&4
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string passCode;
getline(cin, passCode);
cout << passCode << endl;
return 0;
}
In many many programming languages, so called loops are used to execute or repeat blocks of code.
Or do iterate over "something". Therefore loops are also called Iteration statements
Also C++ has loops or iteration statements. The basic loop constructs are
for loops,
while loops,
do-while loops
Range-based for loops
Please click on the links and read the descriptions in the C++ reference. You can use any of them to solve your problem.
Additionally, you need to know that a string is a container. Container means that a varibale of such a type contains other elements from nearly any type.
A string for example, contains characters.
Example: If you have a string equivalent to "Hello", then it contains the characters 'h', 'e', 'l', 'l', 'o'.
Another nice property of some containers is, that they have an index operator or better said, a subscript operator []. So, if you want to access a charcter in your string, you may simply use your variable name with an index specified in the subscript operator.
Very important: Indices in C++ start with 0. So the first character in a string is at index 0. Example:
std::string test = "Hello";
std::cout << test[0];
will print H
With all the above gained know how, we can now solve your problem easily. We will iterate over all characters in your string, and then check, if a character is an alphabetic charcter. In that case, we will replace it.
One of many many possible implementations:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string passCode;
getline(cin, passCode);
for (unsigned int i = 0; i < passCode.length(); ++i) {
if (isalpha(passCode[i])) {
passCode[i] = '&';
}
}
cout << passCode << endl;
return 0;
}
I believe this is what you're looking for when using isalpha.
Looking at the 2 character input for passCode, check each place (0 and 1) if it is a an alpha and change it to &.
if (isalpha(passCode.at(0)) {
passCode.at(0) = '&';
}
if (isalpha(passCode.(1)) {
passCode.at(1) = '&';
}

How to store a single word in a vector ? (c++)

Idea:
I'm trying to create a program that searches for user-entered-word in a .txt file. Size of the word is not given. I want to find a way to dynamically store user's word to be able to compare it to the other words from file.
The entire program is huge so i only attach a part related to my queasions.
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
void vectorfill(vector<char>& newword) //filling char vector
{
char input;
scanf_s("%c", &input);
while (input != -1)
{
newword.push_back(input);
scanf_s("%c", &input);
}
}
int main (void)
{
vector<char> word;
printf("Enter a word: (-1 to finish)");
vectorfill(word);
}
Questions:
1) Is char vector a best idea in this case ?
2) (In case we're good with char vector)How to make compiller understand that user finished writing their word? Can we ask him to put (-1) at the end? Is there a better way to mark the end of the input?
1> No. Use a std::string
2> Yes. Use whitespace.
Example:
#include <iostream>
#include <string>
int main ()
{
std::string word;
std::cout << "Enter a word" << std::endl;
std::cin >> word;
// do something with word. For example,
std::cout << "You entered" << word << '\n';
}
As soon as the user types in at least one number, letter, or other non-whitespace character followed by a whitespace character a word will have been captured in word. If you have special requirements like this word can only contain letters (no numbers, bells, ASCII art characters, etc...) a simple loop with isalpha can sort that out in a few lines of code, but not as few as std::find_if and isalpha.
If the searching content is in txt file. Using std::vector<std::string> may be better. You can use the split char to split the words.
If the content is from user's keyboard input. you can also use std::string to store every word typed, and store it in std::vector<std::string>. Just like this:
std::string s;
std::vector<std::string> vec;
std::cout << "Please enter somestring" << std::endl;
while (cin >> s)
{
vec.push_back(s);
cout << "You have entered : " << s << endl;
}

Sort char array using sort function c++

I am trying to limit user input into alphabet only, then sort all the character in ascending order.
build messages
error: no matching function for call to 'std::__cxx11::basic_string::basic_string(char&)'
This is my header
#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <regex>
should i convert the char into string then convert back to char for my following code ?
string Sortstr (str[mlength]);
sort(Sortstr.begin(), Sortstr.end());
getting this 2 line error.
int mlength = 100;
int main() {
char str[mlength];
int length;
cout << "Please enter a c-string: ";
cin.getline(str,mlength,'\n');
regex pass1("^[a-zA-Z]+");
while(!regex_match(str,pass1)) {
cout<<"Error"<<endl;
cout << "Please enter a c-string: ";
cin.getline(str,mlength,'\n');
}
string Sortstr (str);
sort(str, str + strlen(str));
}
Why not just sort str?
sort(str, str + strlen(str));
There's no reason you can't sort an array directly. Just pass pointers to the first and one-past-the-end elements of your array to sort. In this case adding strlen gets a pointer to the effective end of your array.
In this line
string Sortstr (str[mlength]);
you are using the index operator on a char array which gives you one single char. So, you are passing one single char to the string constructor. This constructor does not exist, hence the error. Even if it existed, you do not want to pass one single char but the entire char array.
What you want is this:
string Sortstr (str);

How to read file with characters and integers c++

I am 90% done with a homework project of mine but this last step is kicking my butt.
I have a text file that I'm going to be reading from for my program with commands on each line.
Most of the commands are a single letter, but one of them is a letter with an integer behind it.
I ideally need to read the line, if it's just a char go right into a function I've already written for the "Command". If it has a specific character, "F" in this case, I need it to also read the integer that will be separated by a space and pass that into my other function for that command.
Example;
.txt file;
R
L
L
F 20
R
R
For those who are curious I'm mimicking the function of the Logo language that used the little "turtle" to make logo animations for my homework.
Edit
I did try researching some methods to do this but most that I came up with either grabbed just the one char, or involved strings with which I could pull each "line" but then have to read and convert what was in string to separate char and int. If that is truly the "best" way to do it I'll suck it up and do it but I wanted to see if there was something that wasn't initially obvious to me.
This would be my approach:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
ifstream readFromFile("test.txt");
vector<string> fileWords;
string word;
while (readFromFile >> word) {
try {
int number = stoi(word); // here is your number
cout << number << endl;
} catch (const invalid_argument& exception) {
cout << exception.what() << endl; // just for debug
}
fileWords.emplace_back(word);
}
for (const auto& word: fileWords) {
cout << word << ' ';
}
readFromFile.close();
}
It reads word by word, saves it on an array and it also checks if a word is an integer (using the std::stoi function).
Solution by OP.
Resolved Kinda.
I ended up changing my fstream input to;
integer = 0;
char ch;
while(infile >> ch)
if (ch == "F")
{
infile >> integer;
}
// do stuff with code, I used a switch
Then after the switch I put I put integer back to 0.
This pulls the data I needed and stored it in the correct variables.

How to display an integer literally as a character

I have an integer 1 and i want to display it as a character '1' in C++. So far I have only managed to convert it from say integer 65 to character 'A'.
How do you stop this ?
int theDigit = 1;
char ch = theDigit+'0';
This works because it's guaranteed1 that the sequence of characters '0'...'9' is contiguous, so if you add your number to '0' you get the corresponding character. Obviously this works only for single digits (if theDigit is e.g. 20 you'll get an unrelated character), if you need to convert to a string a whole number you'll need snprintf (in C) or string streams (in C++).
C++11, [lex.charset] ΒΆ3:
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
By the way, I suppose that they didn't mandate contiguity also in the alphabetical characters just because of EBCDIC.
Use the stringstream.
int blah = 356;
stringstream ss;
string text;
ss << blah;
ss >> text;
Now text contains "356"(without quotes). Make sure to include the header files and use the namespace if you are going to copy my code:
#include <sstream> //For stringstream
#include <string>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
int i = 3;
char buffer [25];
itoa (i, buffer, 10);
printf ("Integer: %s\n",buffer);
Integer: 3
You did just ask about printing an integer, so the really simple c++ answer is:
#include <iostream>
int main()
{
int value = 1;
std::cout << value << endl;
return 0;
}