Problum returning an array from nested loops

Ok, I am having a problem that my arrays will not come out of my nested loops. Only the last value will come out and if I try and print another value it comes up with a crazy small value.

Post your code.
you will have to forgive any thing it is quite long but I will post where the main problem is happening
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
 if(flag ==1)
  {

    const string productnumberfinal=userinputpro;

    string filename="c://Scale App//"+userinputpro+".txt";
    int flag2=0;
    ifstream myfile2;
    myfile2.open(filename.c_str());

        if(!myfile2)
            {
            cout<<"Error Code 2"<<endl;//error opening the scales file
            flag2=1;
            break;
            }


             else
             {
                while(!myfile2.eof())
                {

                  while (getline(myfile2,scale1[z]))
                  {
                   myfile2.ignore(numeric_limits<streamsize>::max(), ' ');
                   scale2=scale1[z];

                   double L =atof(scale2.c_str());
                      if(L!=0)
                      {
                     scalefile[z]=L;
                       ++k;
                        }
                      else
                      {
                        
                        }
	            cout<< scalefile[i] << endl;     

                  }
                 // cout<< k << endl;

                   ++z;

             }
            //below is what prints out  
            for (int i=0; i<k;i++)
	      {
                cout<< scalefile[i] << endl;
              }
             char date[9];
             _strdate(date);
             username;
             cout<< date <<endl;
             break;
  }



This is what it prints from scalefile:
15.2
//then 8 extreamly small numbers

What it should be:
94.7 g
53.0 g
6.5 g
89.5 g
89.5 g
5.1 g
61.4 g
224.2 g
15.2 g
Last edited on

What array are you referring to?, and what is username on line 50?
user name is up farther and not needed I just forgot to delete it


Have you debug stepped it to make sure the array is being populated correctly by the read routine?

Again, not all code is there.. i.e. I don't know how scalefile is defined etc., or where z or k is defined and are they initialised etc. Not much to go on sadly.
1
2
3
4
5
6
7
8
9
10
11
12
int errorcode;
  string userinputpro;
  string userinputnam;
  int scaleinput;
  int z=0;
  int x=0;
  int k=0;
  string scale1[z];
  string scale2;
  double scale3[k];
  double scalefile[z];
  

I did debug it and I updated my above code to show where it printed correctly

I am seeing a lot of problems and also the program fails to compile under Visual Studio.

What is the reason for declaring z and k to 0, then using those variables to set the size of scale1, scale3 and scalefile? - are you thinking those will automatically adjust in size as you adjust z and k?

Topic archived. No new replies allowed.