Array inside a File

Hi there!

I am trying to write a code that asks which division the user wants and then they are asked to enter the quarter number (1-4) and each quarter's sales. However I am having a hard time figuring out how to get my cout<< "Please enter" << c.DivisionName[t]<< "quarter number:" << endl; to actual recall the division they picked.

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
  // including my header files

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// create a structure
struct company
    {
    //variable declaration

    string DivisionName[4];
    int Quarter[4];
    float QuarterltSales [4];
    };


int main()
{
    fstream file;
    company  c;     // Intializing Struct
    string DivisonName[]= {"North", "South", "East", "West"};


    //fstream("sales.txt", ios::out | ios::in);
    file.open("sales.txt",ios::out | ios::in);
    string ha;

    if(!file)
    {
        cout << "Error 0110111001101111: Cannot open the file" << endl;
    }

    for(int t = 0; t<4; t++)
    {
        {
       cout << "Here are our Divisons, please select which you'd like to start with: " << endl;
       cout << "(1) North" << endl;
       cout <<"(2) South" << endl;
       cout <<"(3) East" << endl;
       cout <<"(4) West" << endl;

       cin >>c.DivisionName[t];
}
    for(int l = 0; l<4; l++)
    {
        cout<< "Please enter" << c.DivisionName[t]<< "quarter number:" << endl;
        cin >> c.Quarter[l];
        cout<< "Please enter that quarter's sales:" << endl;
        cin >> c.DivisionName[l];
        if(c.QuarterltSales[l] < 0)
        {
            cout << "We are writing off your debt please enter a postive value" << endl;
            cin >> c.QuarterltSales[l];
        }
    }
    }


    return 0;
}
You have four divisions and each division has four quarters, so it should be a 2D, not 1D, array:
float QuarterItSales [4][4]

For actually recalling the division the user picked I'd suggest a switch-case method:

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

bool fQuit = false;
while(!fQuit)
{
	cout<<"Please select: \n1.North \n.2South \n3.East \n4.West \n5.Quit \n";
	int choice;
	cin>>choice;
	switch(choice)
	{
		case 1:
		{
			//here you can either accept data for one particular quarter for North or for all four quarters through a loop
			//remember North is row [1] of the array, so any entry would be of the form QuarterItSales[1][j] where j indexes the quarter
			break; 		
		}//you'll need the braces within the case statements to scope the local variables that will be required for accepting data
		...
		...
		...
		case 5: 
			fQuit = true;
			cout<<"You've decided to stop entering sales data \n";
			break;
		default:
			cout<<"Invalid entry, try again \n";
			break;
	}
}


Couple of comments: (a) you open a file in your program that you neither read from nor write to, maybe there are other parts of your program not posted here, if not this should be removed, (b) it is safer to initialize your array elements to 0 upon declaration so that there is no garbage data in any of the cells that can corrupt your readings
Topic archived. No new replies allowed.