Thread Closed

[SOLVED] C++ problem

 
Share Thread Thread Tools
Mar8-07, 04:09 PM   #1
 

[SOLVED] C++ problem


I'm trying to make a structure of 4 elements to store an array of items. I'm trying to sort the lastName element in alphabetical, ascending order, but I keep getting errors around the last for loop. Can someone tell me what I'm doing wrong?


Code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct student {
	string firstName;
	string lastName;
	double GPA;
	int studentID;
};

int main()
{
	student s[10];

	ifstream in("people.txt");
	int numStudents = 0;

	const int size = 7;

	bool flag = true;
	for (int endIndex = size - 1; endIndex > 1 && flag; endIndex--) {
		flag = false;
		for (int startIndex = 0; startIndex < endIndex; startIndex++) {
			if (s[startIndex + 1].lastName < s[startIndex].lastName) {
				// adjacent elements in the array are out of order - swap them
				string temp = s[startIndex];
				s[startIndex] = s[startIndex + 1];
				s[startIndex + 1] = temp;
				flag = true; // set flag to show we have to continue...
				
			}
	}
	return 0;
}
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> King Richard III found in 'untidy lozenge-shaped grave'
>> Google Drive sports new view and scan enhancements
>> Researcher admits mistakes in stem cell study
Mar8-07, 04:35 PM   #2
 
You keep getting errors? Does that mean you have run the algorithm and it doesn't sort the list correctly, or does it mean that it doesn't run?

I can see an extremely elementary error, any good IDE would show it.
Mar8-07, 04:53 PM   #3
 
The program doesn't run at all. What is the error you found? Sometimes I got it to stop erring but then whenever I tried outputting I got more errors.
Mar10-07, 01:38 AM   #4
 

[SOLVED] C++ problem


Quote by z-component View Post
Code:
student s[10];

[...]

string temp = s[startIndex];
s[startIndex] = s[startIndex + 1];
s[startIndex + 1] = temp;
}
Can you do this? Do you want to?
Mar10-07, 05:04 PM   #5
 
I got it to work. Thanks everyone.
Thread Closed
Thread Tools


Similar Threads for: [SOLVED] C++ problem
Thread Forum Replies
[SOLVED] D.E. problem Calculus & Beyond Homework 1
[SOLVED] Gas Law Problem Biology, Chemistry & Other Homework 2
[SOLVED] Thermal Stress Problem + Tensions Problem Engineering, Comp Sci, & Technology Homework 4
a solved problem from Griffiths creats problem Classical Physics 1
I solved a problem but I need some insight into why it was solved this way. Introductory Physics Homework 4