Simple coding

Dec 7, 2020 at 11:26am
Hello, I need to do a simple coding for a school project but the teacher says nothing and I got nothing so I thought maybe you can help me guys. Heres the assignment: In a text file there are positive and negative number put them in array (1 job) then positive in other array and negative numbers in another array (2nd job) Then both of positive and negative numbers we put into txt files one's for negative second for positive (3rd job). So can some of you help a fellow beginner coder?
Dec 7, 2020 at 11:33am
Dec 7, 2020 at 11:39am
What part do you need help with? Can you read numbers from a file? Can you define an array? What have you done so far - post your existing code.
Dec 7, 2020 at 11:42am
It's just the first step right now that im doing but not successfully trying to read the numbers out of a txt file
#include <iostream>
#include <ifstream>
#include <string>
using namespace std;

int main()
{
int a, b, c;
string line;

ifstream File("skaiciai.txt");

getline(File, line)
cout << line << endl
}

Last edited on Dec 7, 2020 at 12:05pm
Dec 7, 2020 at 12:07pm
This is 1). It will read in numbers from the file and put them in the array and then display them. MAX_SIZE specifies the maximum number of numbers that can be read.

Doing 2) should now be fairly easy - just use 2 arrays and a test to see which one the numbers are put.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	const size_t MAX_SIZE {20};
	int array[MAX_SIZE] {};

	ifstream File("skaiciai.txt");

	if (!File.is_open()) {
		cout << "Cannot open file\n";
		return 1;
	}

	size_t no_nums {};

	for (; no_nums < MAX_SIZE && File >> array[no_nums]; ++no_nums);

	for (int nn = 0; nn < no_nums; ++nn)
		cout << array[nn] << ' ';
}

Last edited on Dec 7, 2020 at 12:07pm
Topic archived. No new replies allowed.