Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ problem

  1. Mar 8, 2007 #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 (Text):
    #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;
  2. jcsd
  3. Mar 8, 2007 #2


    User Avatar
    Homework Helper

    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.
  4. Mar 8, 2007 #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.
  5. Mar 10, 2007 #4
    Can you do this? Do you want to?
    Last edited: Mar 10, 2007
  6. Mar 10, 2007 #5
    I got it to work. Thanks everyone.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: C++ problem
  1. C problem (Replies: 3)

  2. A C problem (Replies: 3)

  3. C++ problem (Replies: 11)

  4. Problems in C++ (Replies: 5)

  5. A problem with C (Replies: 4)