Hello Qikee,
Thank you for the file.
I have yet to run the program, but what I see first is:
The header files:
1 2 3 4 5 6 7 8 9 10 11 12
|
//#include<bits/stdc++.h> // <--- Duplicates what you have used below and adds more header files that you may not need.
#include <fstream>
#include <iostream>
#include <iomanip> // <--- Added. Used for "std::quoted()".
#include <string>
//#include <sstream>
//#include <map>
#include <cctype> // <--- Added.
#include <chrono> // <--- Added.
#include <thread> // <--- Added.
using namespace std; // <--- Best not to use.
|
I have found that "iostream" should be followed by "iomanip" as I quite often use it in most programs. These are manipulators for "cin", "cout" anf file streams that can be very handy.
The header files "sstream", which I do not see used, and "map" can be commented out for now. At the moment I am not sure why "map" was suggested or where it would be useful.
In my setup I believe that "cctype" may have been included through "iostream", but do not count on this as your compiler and header files may be different. And it does not cause any problem if you try to include a header file a second time.
"chrono" and "thread" I will talk about shortly.
The last line the comment says all I am going to say for now. There have been many posts here the cover this if you want to do a search and
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/ is always a good read.
Line 12 of your code should be
int main()
. "main" always returns an "int", usually zero (0), to say how the program ended.
On line 17 you open a file stream, but how do you know it is open? If the file did not open the program would continue, but would not read anything.
When dealing with a file stream I like to use this:
1 2 3 4 5 6 7 8 9 10
|
const std::string inFileName{ "text.txt" };
std::ifstream inFile(inFileName);
if (!inFile)
{
std::cout << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread". Optional as is the header files.
return 1; //exit(1); // If not in "main".
}
|
I put the file name in a variable to make it easier to use.
Line 3 not only defines the name of the file stream, but also tries to open the file.
The if statement is checking that the file stream is open and ready to use.
Line 8 is optional. The way my IDE is setup when a "return" or "exit" is executed the window that the program was running in closes and I do not have enough time to read any last messages.
The line optional and if you choose not to use it you will not need the header files "chrono" and "thread".
For now the only part you have to worry about is the number in (). This is the whole number of seconds that the program will pause or wait for.
For your variables:
1 2 3
|
char sim{}, letters[60]{};
int n{ -1 }, which{}, content[60]{}, i{}, j{};
bool fit{}, is{};
|
From C++11 standards on the use of empty {}s makes it easy to initialize your variables. I feel that it is a good idea to initialize the variables when they are defined. If for no other reason I like to know that the variables contain something other than garbage. And sometimes using an uninitialized variable can cause an error at compile time.
Sorry for the variable names. I tried to translate most of the program all at once and it did not work as well as I had hoped for.
Using the {}s will initialize the "char"s to "\0" and the array to all "\0"s. The "int"s will be zero(0) and the array will be all zeros. The bool variables will be zero of "false" or you could put "true" between the {}s.
Defining
n{ -1 }
and
content[60]{}
, what you have as "kiekis" would eliminate the need for lines 21 - 26
Lines 30 and 31:
Line 30 will set a value for "tinka" and line 31 could change that value and that may be a problem. I will know more when I run the program.
After that I will need to see the program while its running to understand more.
At the end of the program you open a file for output. You could check it like I did for the input file, but it is not as necessary because if the file does not exist it will create it before it writes to the file.
I started to mention this earlier before I I got into something else. Take a look at
http://www.cplusplus.com/reference/cctype/ It can give you some ideas of what and how you can use the functions.
I am curious about where this program came from. Was it from a class or a book and if a book what is the title.
Hope that helps,
Andy