So I have written a code where I take three strings at each iteration and store all the combinations of length three(maintaining the order) taking each character from those three strings. I use a char array to make those combinations and then I convert that char array to a string and insert in an unordered_set. While doing so, I encountered a problem where I see an extra character being added at the end of some strings.
Here is my code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
char charArr[3];
string strArr[3];
unordered_set<string> aSet;
cin >> n;
while(n-- > 0) {
cin >> strArr[0] >> strArr[1] >> strArr[2];
for(int x=0; x<strArr[0].size(); x++) {
charArr[0]=strArr[0][x];
for(int y=0; y<strArr[1].size(); y++) {
charArr[1]=strArr[1][y];
for(int z=0; z<strArr[2].size(); z++) {
charArr[2]=strArr[2][z];
string tempStr(charArr);
cout << "Len = " << tempStr.length() << "\n";
aSet.insert(tempStr);
}
}
}
}
cout << "Check aaz = " << aSet.count("aaz") << "\n";
for(auto it=aSet.begin(); it!=aSet.end(); it++) {
cout << *it << "\n";
}
}
Here is a sample input:
2
a a z
c a c
Here is the output:
Len = 4
Len = 3
Check aaz = 0
cac
aaz
Here is a picture of how it looks in my pc:
However when I ran the same code for same input in ideone, it gave me the correct answer. Which is as follows:
Len = 3
Len = 3
Check aaz = 1
cac
aaz
Can anybody please explain what's going on here?
N.B: I'm using CodeBlocks and C++14
I need to copy numbers from one text file and input them in another but make them the next number for example 1->2 3->4 ... 9->0
I have gotten the copying part down but cant figure out how to make one number the next.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
string content = "";`
int i;`
for(i=0 ; infile.eof()!=true ; i++) // takes content
content += infile.get();
i--;
content.erase(content.end()-1); // erase last character
cout << i << " characters read...\n";
infile.close();
outfile << content; // output
outfile.close();
return 0;
}
I enter 1 2 3 4 5 and expect the output to be 2 3 4 5 6
You can check if an input char is a digit, then increase it, something like:
for (i = 0; infile.eof() != true; i++)// takes content
{
char currentChar = infile.get();
if (isdigit(currentChar))
{
currentChar++;
}
content += currentChar;
}
Expanding on the answer from Oded Radi,
If you want 9 to become 0 (as you described) you need to handle that, this is one way:
for (i = 0; infile.eof() != true; i++) // takes content
{
char currentChar = infile.get();
if (isdigit(currentChar))
{
currentChar = (((currentChar - '0') + 1) % 10) + '0';
}
content += currentChar;
}
If your input is separated by whitespace, your loop can be simple:
int value;
while (input_file >> value)
{
value = value + 1;
output_file << value << " ";
}
Another loop could be:
int value;
while (input_file >> value)
{
value = (value + 1) % 10;
output << value << " ";
}
The above loop restricts the numbers from 0 to 9.
So I have a custom decipher program that reads in an input file and deciphers it based on the keys that are input.
Text file:
23
11
Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2 computers, 0 not 0 the 0 open 2 car! 2 C, 0 lack 0 of 0 dog 1 green 2 C++ 0 bottle 2 wrong, 2 them. 0
5 1 10 21 9 6 21 11 13 16 20
This file is represented as:
[Number of Words]
[Number of Keys]
[Word] [Jump] [Word] [Jump] ... [Word] [Jump]
[Key] [Key] ... [Key]
I have managed for the program to read in the number of keys and amount of words, however I have having trouble reading in the words and the numbers next to them as well as the last numbers and registering those as keys. This is what I have so far:
#include <iostream>
#include <fstream>
using namespace std;
struct pieces {
char word;
int jump;
} ;
// Main Function
int main ()
{
// declare variables
int keyCount = 1;
int wordCount = 8;
int wordAmount[8];
int keyAmount[8];
pieces cipher[5];
char decoded[20][20];
char filename[10];
int keys[keyCount];
char tArray[20][20];
ifstream inData;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
inData.open(filename);
if(inData.is_open());
{
// read list of names into array
for ( int i = 0; i < keyCount; ++i){
inData >> wordAmount[i] >> keyAmount[i];
for(int j = 0; j < wordCount; j++){
inData >> cipher[j].word >> cipher[j].jump;
}
}
cout << " Key Count: " << keyCount << "\n";
// print out
for ( int i = 0; i < keyCount; ++i){
cout << " KeyAmount: ";
cout << keyAmount[i] << "\n";
cout << " WordAmount: ";
cout << wordAmount[i] << "\n";
for(int j = 0; j < wordCount; j++){
cout << cipher[j].word << " " << cipher[j].jump;
}
}
}
inData.close();
return 0;
}
I did try putting the char word as an array but then I got a segmentation file. Any advice would be appreciated!
IMHO, your project would be simpler if you used std::string and overloaded the formatted input operator >>:
struct Word_Jump
{
std::string word; // Using std::string because it has space for more than one character.
int jump;
friend std::istream& operator>>(std::istream& input, Word_Jump& wj);
};
std::istream&
operator>>(std::istream& input, Word_Jump& wj)
{
input >> wj.word;
input >> jump;
return input;
}
Your input could look something like this:
std::vector<Word_Jump> database;
for (unsigned int i = 0; i < number_of_words; ++i)
{
Word_Jump wj;
my_data_file >> wj;
database.push_back(wj);
}
Similarly, you input for reading keys:
std::vector<int> key_database;
for (unsigned int j = 0; j < number_of_keys; ++j)
{
int k = 0;
my_data_file >> k;
key_database.push_back(k);
}
So the above are two possible methods for reading in the word & jump pairs and also the keys.
The task: (somekind of a lotto comparing program) User inputs 7 numbers (from 1 to 39) and those numbers are compared to each line (of 7 numbers) in a text file.
if the user input (7 numbers) matches more than 3 times in any of the lines, it should say at the bottom that it's not ok.
Example:
Text file:
2,5,8,16,25,27,32
1,4,15,19,22,25,36
7,12,13,27,35,38,39
.
.
.
User input in the program:
1,5,16,21,25,32,36
Now the program should display:
2,5,8,16,25,27,32 matches: 4
1,4,15,19,22,25,36 matches: 3
7,12,13,27,35,38,39 matches: 0
.
.
.
There are more than 3 matches!
For now my code is this (but it only checks the first line and displayes the same amount of matches in all lines):
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <ctime>
using namespace std;
int main () {
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int z=0;
int rezultati;
while (z!=1) {
cout<<"Title of the program"<<endl;
//Odpri txt
string line;
ifstream myfile ("stevilke.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
line=rezultati;
}
myfile.close();
}
else cout << "Unable to open file";
//Glavni prog:
cout<<"Napisi stevilke (1 enter 2 enter 3 enter ...)"<<endl;
cin>>a>>b>>c>>d>>e>>f>>g;
if (a>0 && a<40 && b>0 && b<40 && c>0 && c<40 && d>0 && d<40 && f>0 && f<40 && g>0 && g<40){
// preveri enake
string line;
ifstream myfile ("stevilke.txt");
while (getline (myfile,line)){
//računam numom
const int n = 7 ;
std::srand( std::time(0) ) ;
int lottoresults [39] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39} ;
for( int i = 0 ; i < n ; ++i );
// numbers entered by the user
int userinput[n] = { a, b, c, d, e, f, g } ;
for( int i = 0 ; i < n ; ++i );
// check for matching numbers
int cnt_matches = 0 ; // #matches
for( int i = 0 ; i < n ; ++i ) // for each number entered by the user
{
const int user_num = userinput[i] ;
// check if the number entered by the user is a winning number
// (could use std::find() instead of writing a loop of out own)
for( int j = 0 ; j < n ; ++j ) // for each winning number
{
if( user_num == lottoresults[j] ) // is it the same as user_num?
{
// yes, we got a match
++cnt_matches ;
std::cout << a<<"," << b<<"," << c<<","<< d <<","<< e <<","<< f <<","<< g <<".";
}
}
}
std::cout << "Ujemanja : " << cnt_matches << '\n' ; //TUKI ! --- cnt_matches je v vsaki vrsti enak, spremenit da se spreminja za usako vrsto!
// numom do tu
cout << line <<'\n';
}
//Piši v txt
/*std::ofstream ofs;
ofs.open ("stevilke.txt", std::ofstream::out | std::ofstream::app);
ofs <<endl;
ofs <<a<<","<<b<<","<<c<<","<<d<<","<<e<<","<<f<<","<<g;
ofs.close(); */
cout<<"pritisni 1 za ponovitev programa, karkoli drugega program zapre."<<endl;
}
//konc if-a
else
cout<<"Dej se ne zajebavi no.. vpis prave stevilke"<<endl;
cout<<"Se enkrat zazeni program."<<endl;
system("pause");
}
return 0;
}
Sorry for long question, but I'm struggeling with this problem for 3 nights already and I can't solve it...
I'm just a begginer in c++ and I'd really appriciate ANY help you could give me!
Also sorry for my bad english (not native).
If you need any more informations or code or anything, please let me know.
Thank you!
I have two files, and I am supposed to input both of them into arrays.
One of them is:
2
3
4
5
7
8
The second one is
2
4
5
6
7
8
2
3
4
5
7
8
(it's much longer, but it doesn't matter). I need to have one array with the first 6 numbers, and then I am supposed to check if first six numbers from the second file are the same as the numbers in the second array, same with the next six numbers and so on (like checking for a lottery winner).
I guess that I am supposed to load numbers from one file into multiple arrays, but I have no idea how to do it, and I can't find it anywhere.
The code for the first array that I have so far is:
#include<iostream>
#include<fstream>
using namespace std;
int main(){
int numbers[6];
int count = 0;
ifstream inputFile;
inputFile.open("Numbers.txt");
while (count < 20 && inputFile >> numbers[6]){
count++;
inputFile.close();
for (count=0; count < 20; count++)
cout << numbers[count];}
return 0;
}
Another problem is that instead of displaying the numbers correctly, it displays "-858993460" 6 times - even though my code is basically copied from a book...
What is wrong with my code, and how do I input the second file?
Your main problem is doing stuff inside your loop that should not be there. The loop is supposed to run once for each value it reads in from the file. After it is finished is when you should close the file and print the results.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int numbers[6];
int count = 0;
ifstream inputFile;
inputFile.open("Numbers.txt");
if(!inputFile.is_open()) // always check for errors
{
std::cerr << "ERROR opening input file:" << std::endl;
return 1; // error
}
// make sure count < 6 so you don't overflow your array
while(count < 6 && inputFile >> numbers[count])
{
count++;
// inputFile.close(); // don't close the file yet!!
//for(count = 0; count < 20; count++) // don't output yet!!
// cout << numbers[count];
}
// now close your file and output what you have
inputFile.close();
for(count = 0; count < 6 /* not 20!! */; count++) // don't output yet!!
cout << numbers[count] << '\n';
return 0;
}
This is an example of reading numbers from your first file. This example can be added to, to read numbers from your second file and compare them with the numbers read from the first file.
Updated:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int numbers[6];
int count = 0;
ifstream inputFile;
inputFile.open("Numbers.txt");
// get each number from the file until the end-of-file bit
// is retrieved.
while ((inputFile >> numbers[count]) && (count < 6)){
// iterate count by one
count++;
}
inputFile.close();
// run through the array of numbers and ouput each index
for(int i = 0; i < 6; i++) {
cout << "numbers[" << i << "] = " << numbers[i] << "\n";
}
}
Input: "Numbers.txt"
2
3
4
5
7
8
Output: cout
numbers[0] = 2
numbers[1] = 3
numbers[2] = 4
numbers[3] = 5
numbers[4] = 7
numbers[5] = 8
Update:
Faulty Input:
d
3
4
5
7
8
Output
numbers[0] = 0
numbers[1] = 0
numbers[2] = -1527900896
numbers[3] = 32627
numbers[4] = -1527901752
numbers[5] = 32627