Reading txt file with semicolon and colon into array

Hi, so i have the assignment that i am doing,but i am stuck because i do not know how to separately read separated elements from file to array. This is the first part of the question i need help with:

Your program will need to read from a file called values.txt. This file has the following
format:
4;1,2,3,4
3;6,7,9
The above refers to a line with two components: size and the values of an array. The first
number before the semi-colon, refers to the size of the array. What follows afterwards are
the values that should go in that array. The number of lines you will have to read is not
specifed.
You will read from this file, line by line, each of the lines, and then store the values
in the array. Then the following operation should be done:


Please assist thank you.
What do you need assistance with?

Do you know how to read the file?

Do you know how to create a dynamically allocated array?

What are the following operations that should be done?

I do not know how to read the files in that way where 1 line has 2 functions, that is that the first part separated by a semicolon is for the array size and then further separated by commas for the values.


I just wanted help understanding that first part, but the rest of the question is this:


You will read from this file, line by line, each of the lines, and then store the values
in the array. Then the following operation should be done:
- If the largest element in the array is odd, square every value in the array and display
the array, as a single comma delimited line with a new line at the end.

- If the largest element in the array is even, multiply every value by the largest value
in the array and display the array, as a single comma delimited line with a new line
at the end.
Do you know how to open the file and read numbers from the file?

You need to show some code so we can see something about your level of experience.

Edit: Don't confuse the word "functions" with C++functions. This is just telling you that the first number is the size of the array, the rest of the numbers are the elements used to populate the array. Each line is basically a repeat (think loop) of the process.

Read the array size.
Create the array.
Read the numbers into your array.
Do your calculations.
Free the memory used to create the array.
Repeat until there are no further lines.

Last edited on
ok, so this is a code i recently did. Its the only one i have that works well with reading from a file,but it reads from a file that has values separated by space, not comma separated. basically this code reads a line of values and uses bubblesort to sort the values into ascending order. I am a beginner to c++ so this example of code took me a while to do. Note that the values are returned as string because that was required but the main part would be the file reading system. I'm only familiar with reading files in the way that i have in this programme. This code basically sums up whatever knowledge i have on this section






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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<fstream>
#include<sstream>

using namespace std;

string bubSort(double arr[8]);

void swapping(double &a, double &b) 
{ 
   int temp;
   temp = a;
   a = b;
   b = temp;
}

int main()
{
int i;
double arr[8];
ifstream list;
string output;
list.open("list.txt");

	while(!list.eof())
	{
		for(i=0;i<8;i++)
		{
			list>>arr[i];
		}
	
	for (i=0;i<8;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
	output=bubSort(arr);
	cout<<output<<endl;
}
return 0;
}

string bubSort(double arr[8])
{
	
	string s;
	stringstream ss;
	
	for(int i=0;i<8;i++)
	{
		int swaps=0;
		for(int j=0;j<8-i-1;j++)
		{
			if(arr[j]>arr[j+1])
			{
				swapping(arr[j], arr[j+1]);
				swaps=1;
				
			}
			
		}
		
		if(!swaps)
		{
			break;
		}
	}
	cout<<"swapped"<<endl;
	for(int q=0;q<8;q++)
	{
		ss<<arr[q];
	}
	cout<<endl;
	ss>>s;
	
	return s;
}
Topic archived. No new replies allowed.