Array String Void IF FOR, I am stuck with COVID-19 program calculator

Hello,

I really need help, with COVID-19 program.
I have to use Array String Void For and IF.
I would be very grateful if something was do it, because I only learn everything with examples and when I learn from examples I am already able to do it myself even though it takes time.
If you try to explain it in comments it will be even harder for me to understand how to write a program
Sorry for grammar :)

I was starting making this program, but in my mind have millions questions it will be much easier to understand it if you finish this program
https://drive.google.com/file/d/1bjQ599bqPt7exM0HetGwqowcfrdiGY82/view?usp=sharing

My task:
Input .txt file is

5
Lithuania 1426 460
Canada 44364 15469
India 24942 5498
Russia 74588 6250
Italy 195351 63120

Output .txt have to be

Most: Italy 132231
At least: Lithuania 966
General list:
Lithuania 966
Canada 28895
India 19444
Russia 68338
Italy 132231

Thanks in advance

Last edited on
You haven't told us enough about the problem.

What is the format of the input?

What is the program supposed to do with the input to generate the output? Or put another way, how are the numbers in the output related to the numbers in the input?
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
Ohh, some how I put wrong file in drive. SORRY :(
https://drive.google.com/file/d/1bjQ599bqPt7exM0HetGwqowcfrdiGY82/view?usp=sharing

input file have to be data.txt (text file), output too.

data.txt (input)
5 //it meens there is five countries
Lithuania 1426 460 // 1426-460=966 subtract the larger number from the smaller
Canada 44364 15469
India 24942 5498
Russia 74588 6250
Italy 195351 63120

result.txt (output)
Most peoples have virus: Italy 132231
At least: Lithuania 966
General list:
Lithuania 966
Canada 28895
India 19444
Russia 68338
Italy 132231

You have to get like that


About include, I always like copy/paste all include it will be a little bit faster for me
If you have more question, please ask me.
Thank you for patience






//COVID-19

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>

using namespace std;

void Reading(int &n, int A[]);
void Comparison(int &n, int A[]);
void Output(int n, int A[]);

int main(){
int n;
int A[10000];

Reading(n, A);
Comparison(n, A);
Output(n, A);

return 0;
}

void Reading(int &n, int A[]){
ifstream fd ("data.txt");
fd >> n;
for (int i = 0; i < n; i++)
fd >> A[i];
fd.close();
}

void Comparison(int &n, int A[]){

}

void Output(int n, int A[]){
ofstream fr ("result.txt");
for(int i=0;i<n;i++){

}
}
(same code in drive now, I know missing a lot of code, like string IF and more :( )
Last edited on
Hello OrestasX,

You have not answered the questions that I have asked or satisfied what dhayden has asked.

Now you have uploaded a program the the only difference I see is a new function, but I have no idea what to do with this function.

It makes no difference because you still can not read the file and get anything to work with.

You should start with learning what include files you need and do not need. Then I would work on just this:
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
#include <algorithm>
#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 };  // <--- Or 10. Make it a small number to start with. You can increase it later.

void Reading(int &n, int A[]);

int main()
{
	int n;
	int A[MAXSIZE];

	Reading(n, A);
	//Comparison(n, A);
	//Output(n, A);

	return 0;
}

void Reading(int &n, int A[])
{
	ifstream fd("data.txt");
	// <--- How do you know it is open?

	fd >> n;

	for (int i = 0; i < n; i++)
		fd >> A[i];  // <--- The very first will put "fd" in a failed state and nothing else will be read.

	fd.close();
}

Get it to read the file first then work on the rest.

Work on this part first and remember what you have to read (string number number).

Lithuania 1426 460 // 1426-460=966 subtract the larger number from the smaller
This is nice, but does not tell me what the numbers are.


General list:
Lithuania 966

This is also nice, but does not tell me what the number means.

Andy
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
40
41
42
43
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;


struct Stats
{
   string country;
   int cases;
   int recovered;

   friend istream &operator >> ( istream &in, Stats &st ){ return in >> st.country >> st.cases >> st.recovered; }
   int live(){ return cases - recovered; }
};


int main()
{
   istringstream in( "5                  \n"
                     "Lithuania 1426 460 \n"
                     "Canada 44364 15469 \n"
                     "India 24942 5498   \n"
                     "Russia 74588 6250  \n"
                     "Italy 195351 63120 \n" );

   int n;
   in >> n;
   vector<Stats> countries(n);
   for ( int i = 0; i < n; i++ ) in >> countries[i];

   auto pr = minmax_element( countries.begin(), countries.end(), []( Stats a, Stats b ){ return a.live() < b.live(); } );
   Stats least = *(pr.first), greatest = *(pr.second);
   cout << "Greatest: " << greatest.country << ": " << greatest.live() << '\n';
   cout << "Least: "    << least.country    << ": " << least.live()    << '\n';

   cout << "\nFull list:\n";
   for ( Stats st : countries ) cout << st.country << "  " << st.live() << '\n';
}

Greatest: Italy: 132231
Least: Lithuania: 966

Full list:
Lithuania  966
Canada  28895
India  19444
Russia  68338
Italy  132231
Topic archived. No new replies allowed.