-
Sorting/Displaying
I'm a huge noob at programming.. but I'm suppose to be making a program that creates an array of structures... each structure contains the id, first name, last name, major, and gpa of a student... and my program is suppose to be able to sort by last name and display, sort by id and display, search for a specific structure by last name, and search for a specific structure by id...
this is what I have so far.... but I don't really know where to go from here.. I should probably work on finding a way to sort the structures by name and id.. but I don't really know where to start..
can anyone help?
Code:
int x=0;
while(x =! 5)
{
cout << "Enter a number from the list to choose an option";
cout << "1. List the students sorted by last name";
cout << "2. List the students sorted by student id";
cout << "3. Search the students by last name";
cout << "4. Search the students by student id";
cout << "5. Quit the program";
cin >> x;
if (x=1)
;
else if (x=2)
;
else if (x=3)
;
else if (x=4)
;
}
exit(1);
-
Note that = and == are not the same thing.
What do you know about sorting?
-
oh yea I'll fix that real fast -_-''
almost nothing lol..
-
You should also note that != and =! are not the same thing either.
You should look up sort in your C++ book then, or your favorite on-line C++ resource.
-
oh yea -_-''
ok I'll do that (:
-
bleh.. ok I know a little about sorting now....
I know how to sort by numbers of a normal int array..
but I need to sort by the char's of the last name that is included in a structure.. the structure being dynamically created........
this I don't have a clue how to approach.....
-
The operator < is defined for std::strings just like for ints.
Every built-in container (such as vector or list) knows its own size. If you're doing your own memory management with a home-brew dynamic array, then you know the size of the array. So the dynamic-ness can't possibly matter.
-
You can overload operators You need for sorting (for example <) and then sort it like an int array.
-
Ok.. this is what I have so far and I'm wondering if everything is right so far?
and I don't know what to pass through the functions.. I'm thinking a pointer pointing at where my array of structures begins.. but what would my pointer be?
Code:
#include <iostream>
#include <fstream>
using namespace std;
void choice1();
void choice2();
void choice3();
void choice4();
int main(int argL, char *argv[1])
{
int studentnumber=0, x=0;
struct My_struct
{
char id[4];
char fname[30];
char lname[30];
char major[10];
float gpa;
};
ifstream infile;
infile.open(argv[1]);
infile >> studentnumber;
My_struct* my_array = new My_struct[studentnumber];
while(x != 5)
{
cout << "Enter a number from the list to choose an option";
cout << "1. List the students sorted by last name";
cout << "2. List the students sorted by student id";
cout << "3. Search the students by last name";
cout << "4. Search the students by student id";
cout << "5. Quit the program";
if (x==1)
choice1();
else if (x==2)
choice2();
else if (x==3)
choice3();
else if (x==4)
choice4();
}
exit(1);
}
void choice1()
{
}
void choice2()
{
}
void choice3()
{
}
void choice4()
{
}
-
You might want to choose beter names that choice1 .. choice4. You shouldn't have to rename those functions if you later wanted to change which number did what thing.
Something like ListByID for choice2 perhaps? You'll think of something for the rest.
-
You need to define your structure outside of any function. It's char *argv[]; I don't know whether the compiler will ignore the 1 or not, but the 1 is surely wrong (since you're not expecting 0-character arguments). You never read in x. But you did create the array pointer correctly, so well done there.
-
I think You should pass pointer and array size through functions and struct shouldn't be inside main.
-
I'm a noob lol...
how do I pass the pointer through the function?
I already declared my struct outside any function..
-
If you refer to the code above, you most certainly did not declare your struct outside any function.
The input to a function is given inside the ().
-
no I'm not referring to the above code..
in any case I don't have time to fix all the errors I have in this program lol.. its due in 35 minutes..... but thanks for the help anyways