Hello OrestasX,
I have been working with your program, but before I go much farther I have some questions.
Were you given a file to use that requires the first line be a number? If you do not need this number there better ways to read a file of unknown length.
Given the input file of:
Country ???? ????
Lithuania 1426 460
Canada 44364 15469
|
What do the numbers mean. Replace the "????" with a proper heading.
I have the idea that you subtract the smaller number from the larger number. Is this correct?
Then for the output:
Country ????
--------------------
Lithuania 966
Canada 28895
India 19444
Russia 68338
Italy 132231
|
What would be the second column heading?
To the program.
1 2 3 4 5 6 7 8 9 10
|
#include<iostream>
#include<fstream>
#include<iomanip> // <--- OK, but not used yet. Maybe later.
#include<string>
//#include<cmath> // <--- Not needed for simple math like "* / + -".
//#include<cstring> // <--- C++ version of C header file "string.h". Not the same as the C++ "string".
using namespace std;
constexpr int MAXSIZE{ 5 };
| |
The last two header files you do not need. They include extra code that you are not using. Same some space in your final program.
The last line I will get to shortly.
In "main" you define
int A[10000];
. I am not sure about the translation, but you might consider
int mažasSkaičius[MAXSIZE]{};
. Using the constant variable "MAXSIZE" you only have one place to make a change and it will be used everywhere in the program.
The problem with this array is that it will only hold numbers. When you go to read the file the first thing you read is a string, so
fd >> A[i];
will cause the file stream to fail when you try to put a "string" into an array that only holds numbers after that you will be unable to read anything else.
Making a guess I gave this a try:
1 2 3
|
int mažasSkaičius[MAXSIZE]{};
int didelisSkaičius[MAXSIZE]{};
std::string šalis[MAXSIZE];
| |
Not knowing what the 2 numbers are for I am unsure what to call these arrays, but anything is better than "A" and "B".
This does mean you will have to change what you send to the functions and then change the function definitions.
For the function:
1 2 3 4 5 6 7 8 9 10
|
bool Skaitymas(int &n, std::string šalis[], int mažasSkaičius[], int didelisSkaičius[])
{
ifstream fd("U1.txt");
if (!fd)
{
std::cout << "\n File \"U1.txt\" did not open!";
return true;
}
| |
First change I made is the return value.
When you open a file stream for input it is a must to check that it worked. If it does not work you need a way to end the program and fix the problem. Otherwise the program would continue whether it can read the file or not. Back in "main" the function call is:
1 2
|
if (Skaitymas(n, šalis, mažasSkaičius, didelisSkaičius)) // <--- Read
return 1;
| |
Better to return to "main" and leave the program than to do it in the function. This way everything that needs to be closed or destroyed is.
Using the "close()" function in "main" or a function is not needed as the file stream will close when the function loosed scope. If you leave it that does not cause any problem.
I have started working on the "Isvedimas" function, but I am not sure what to do there yet.
Just so you know this is a problem:
if (A[i] == 0 && A[i + 1] == 1 && A[i - 1] == 0) a = a - 1;
When "i" = 0 "A[i]" works and "A[i + 1]" works, but "A[i -1]" does not work. The "i - 1" puts you into memory before the start of the arrays memory. The opposite is when "i" = 4 "i + 1" will put it past the end of the array. After that I do not grasp what the if statement is trying to do.
There is a start for now. And:
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
|
Andy