text file into array of structs

Pages: 12
If the file will not open it is usually because of an incorrect path. That is, the file is in one directory, but the program is looking in another. It may depend on how you are running the program, usually it will be the same as the executable program *.exe on windows, which could be in a subfolder.

If you are using windows, you can find out like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <windows.h>

using namespace std;

int main ()
{    
    char current_directory[256];
    GetCurrentDirectoryA(256, current_directory);
    cout << "Current directory: " << current_directory << '\n';
       
    return 0;
}

OMG YOU ARE MY HERO!!!!!!! I got it fixed it was I put it in the right spot thank you so much!!!!!!!
hey quick question can someone tell me why if you go through the menu and do #1 then #2 then go back to #1 it does the same thing as option 2
Sorting the numbers changes the contents of the array. If you want the original unsorted order, you need to make a copy of the array and sort the copy, not the original.
@ chervil can you give an example to go by don't do it for me I'm just learn better by seeing. thank you for helping me I really do appreciate it I have looked everywhere on the internet for videos and stuff and have found really great stuff but nothing this specific I'm taking this course online and its really kicking my butt but I had no choice since they don't offer the actual class at my college I'm going through a sister college.
Well, the instructions in the opening post say,
Note: you should probably have a duplicate array to keep the unsorted values in case the user enters 2 or 3 and then enters 1. Alternatively, you could simply re-read the file.


If you were to use a duplicate array, simply define a second array, something like this:
 
    info infodup[10];

Then after reading from the file, use a loop to copy each element from the existing array to the duplicate array
 
    infodup[i] = infoentry[i];


Finally, change the code for option 1 in the user selection to use array infodup instead of infoentry
okay I added a new struct infodup but my second question is do I put infodup[i] = infoentry[i]; on line 33 after the end of reading the file?
Last edited on
Yes that's where it could go, sometime after you finished reading the file and before the start of the while(selection!=4) loop.

The variable i is an integer, you need use a for loop or a while loop copy each of the elements.

ok so I put
1
2
3
4
 for (int infodup = 0; infodup < 10; infodup++)
    {
        infodup[i] = entry[i];
    } 
and its govong me these errors
  C:\Users\April\Documents\PROG03.cpp:48:17: error: 'i' was not declared in this scope
C:\Users\April\Documents\PROG03.cpp:64:23: warning: name lookup of 'infodup' changed [enabled by default]
C:\Users\April\Documents\PROG03.cpp:17:3: warning:   matches this 'infodup' under ISO standard rules [enabled by default]
C:\Users\April\Documents\PROG03.cpp:46:11: warning:   matches this 'infodup' under old rules [enabled by default]
Last edited on
You have examples of for loops in your own code which use either i or j as the loop counter.
(where you sort the array). Those should give you some idea of how to start.
yea I know after I posted that I'm like am I really that stupid lol! this is what I got it will run but nothing will happen for option 1 when you choose it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
myfile.close();

 for( int i = 0; i < count-1; i++)
    {
       for (int j =i+1; j < count; j++)
       {
           if(infodup[count2].member1 == entry[count2].member1);

       }
    }
    for( int i = 0; i < count-1; i++)
    {
       for (int j =i+1; j < count; j++)
       {
           if(infodup[count2].member2 == entry[count2].member2);
       }
    }
while(selection!=4)
{
    count2=0; 
That's not what I meant.

You need to combine the simplicity of this:
1
2
3
4
for (int infodup = 0; infodup < 10; infodup++)
    {
        infodup[i] = entry[i];
    }

with a valid use of i.
thanks for you help I'm done I'm to the point where I honestly don't care but thanks for you time.
Sorry about this - it's always a balancing act when trying to help, too little and it's no help at all, too much and it is also no help at all. I know I tend mostly towards the "too little" side of things, sorry it didn't help here.
its not your fault I'm just so upset because I'm paying my own money for this course and there is not enough info I have no book just a few pages over the assignment is what I get. I have to figure it on my basically my teacher is very kind and tryies to help but I would be better off learning this in a classroom enviorment.
Topic archived. No new replies allowed.
Pages: 12