Homework Help: readSales.cpp

I have been stuck trying to understand how to properlly do this program for my class. The TA's and proffesor wont do anything for me except tell me to look at the slides.
Here is the asighnement:
Question 2(15pt): Sales data
A company sells software products.It keeps a record of the number of products that were sold by an employee for a particular month in a calendar year. Write a function, readSales to read the txt file and store the name and sales in two separate arrays. The function returns the number of employees stored.
Function specifications:
● The function name: readSales
● The function parameters (in this order):
○ A file name of type string
○ An array of type string to store names of employees
○ A 2D integer array with 12 columns to store sales of employees
○ The size of the array as an int
● The function returns an integer depending on the following conditions:
○ It returns -1 if the file cannot be opened.
○ It returns the number of employees stored in the array if the function can
open and read the file
○ It returns the size of the array (the capacity of the string array or the
number of rows in the 2D array) if the arrays are full.

Sample file ( sales.txt ):
Vipra
60, 80, 50, 70, 85, 32, 94, 81, 45, 65, 91, 82
Chandan
1, 25, 18, 22, 23, 16, 5, 33, 21, 27, 23, 102
Malvika
55, 54, 52, 56, 58, 52, 51, 59, 58, 50, 54, 55

Sample run 1 ( bold is user input)
Enter the file name:
sales.txt
Returned value (number of employees): 3
Names[0] = Vipra
sales[0][0] = 60
sales[0][1] = 80
sales[0][2] = 50
sales[0][3] = 70
sales[0][4] = 85
sales[0][5] = 32
sales[0][6] = 94
sales[0][7] = 81
sales[0][8] = 45
sales[0][9] = 65
sales[0][10] = 91
sales[0][11] = 82

Here is what I have made so far and I will be honest the part im struggling with I know is easy, but its the splitting the string properly and storing it in the array that I am struggling with
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
78
79
80
81
82
83
84
85
86
87
88
  #include <iostream>
#include <fstream>
#include <string>
#include <cmath> 		
using namespace std; 

int split(string str, char ch,string arr[],int size)
{
   int len = str.length();           // for getting the length of the passed string str
   int count_words = 1;           // for counting number of words in the str
   for(int i=0;i<len;i++)
   {
       if(str[i]==ch)
       count_words++;           // counting number of words in the str
   }
   if(count_words>size)       // if number of words are greater than the size of the array , then return -1
   return -1;
   else                       // otherwise store the words after splitting into the array
   {
       string temp="";               // it will store the current word
       int index = 0;               // for storing the words in the string array and managing the index of string array
       for(int i=0;i<len;i++)
       {
           // if we found the delimeter in the string str , we'll simply store the current word in string array and increment the index by 1
           // And make the temp empty for storing next word
           if(str[i]==ch)              
           {
               arr[index]=temp;
               index++;
               temp="";
           }
           else                       // if current character is not delimeter , add the character into temp
           temp+=str[i];
       }
       arr[index]=temp;           // for storing last remaining word in the temp
       return count_words;               // returning number of words after splitting the string str
   }
}

int readSales(string fileName, string employees[], int sales[][12], int saleSize)
{
	// open a file 
	ifstream myFile(fileName); 
	int count = 0;
	string line; //string variable for each line of text
	int score;
	string myArray[100];
	int sizeOf;
	// check if the file is open
	if(myFile.is_open())
	{
		while(getline(myFile, line))//while there is a line to read
		{
			if(line.length() == 0)
			{
				
			}
			else
			{
				split(line, ',', myArray, 12);
				int numSold = stoi(myArray[1]);
			}
		}
		//close the file
		myFile.close(); 
	}
	else
	{
		// the file cannot read 
		return -1;
	}
}

int main()
{
	string fileInput;
	string names[100];
	int sold[100][12];
	int soldSize;
	cout <<"Enter the file name:"<< endl;
	cin >> fileInput;
	
	readSales(fileInput,names,sold,soldSize);
	


	return 0;
}
Have you considered just using the stream extraction operator in conjunction with getline() to retrieve all the information?

For example use getline() to retrieve the name, then use the extraction operator to get the number followed by the pesky comma?

Something like (Pseudo code) :
1
2
3
4
5
6
7
8
9
getline(inputStream, name);
for(int i = 0; i < 10; ++i)
{
    char comma;
    inputStream >> number[i] >> comma;
}
inputStream >> number[i++];  // Get the last number.

Topic archived. No new replies allowed.