Sorting loop won't execute

I'm trying to sort a list of words alphabetically. I put in several nested loops, but I can tell none of them are running because there should be an output each time they do.
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
//  main.cpp
//  Assignment 3
//
//  Created by Roark Burney on 10/24/14.
//  Copyright (c) 2014 Roark Burney. All rights reserved.
//  Yes I'm stupid enough to put my real name on there internet for google to find

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;
int size = 23907;
string* words = new string[size];
char gettysburg[100][100];
void sort();
void parse();
void check();

int main()
{
    ifstream dictionary;
    ifstream fourscore;
    dictionary.open("/Users/roarkburney/Documents/C++ 2014/Assignment 3/Assignment 3/unsorted_words copy.txt"); //change when sort function is finished
    fourscore.open("/Users/roarkburney/Documents/C++ 2014/Assignment 3/Assignment 3/gettysburg.txt");
    dictionary >> words[0];
    for(int i = 0; i < size;)
    {
        dictionary >> words[i];
        i++;
    }
    sort();
    return 0;
}
void sort()
{
    int counter=0;
    int counter2=1;
    char hold1[20]="Z";
    char hold2[20]="Z";
    string holder;
    while(counter<size)
    {
        sprintf(hold1, "%.19s", words[counter].c_str());
        sprintf(hold2, "%.19s", words[counter2].c_str());
        while(counter2<counter)
        {
            if(strcmp(hold1, hold2)>0)
            {
                counter2++;
                cout << "foop";
            }
            else if(strcmp(hold1,hold2)<0)
            {
                for (int frop=counter2; frop==counter; frop++)
                {
                    holder=words[counter];
                    words[counter]=words[counter2];
                    words[counter2]=holder;
                    cout << "frop";
                }
            }
        }
        counter++;
    }
    for (int vamp=0; vamp==size; vamp++) {
        cout << words[vamp];
    }
}




this is the contents of the data file I am sorting

abnormal
cinch
dictate
discussion
Irish
pampa
Gustav
vii
abysmal
chamberlain
car
clairvoyant
thing
porpoise
cornstarch
incorrigible
jurisprudent
helmet
upbraid
veneer
Gerard
convertible
character
Koenig
scarlet
eureka
bless
corny
McDaniel
Andean
northeast
bedfast
Neapolitan
solitaire
berth
Alabama
balletic
pyramid
Upton
farfetched
Corinthian
fishery
corrigible
extralegal
radiography
allergic
Christendom
general
mandarin
twosome
axiom
Last edited on
Hi,

A few comments:

Are you allowed to use std::string ? If so, then try to avoid writing C code in your C++.

Line 15 is bad practise - google to see why that is.

Also don't have global variables.

When you open files, check to see that it worked.

Always initialise your variables to something when you declare them - not doing this is one of the biggest traps.

Line 30: Usually there is an increment in the for loop statement, as in :

30
31
32
33
34
for(int i = 0; i < size; i++;)
    {
        dictionary >> words[i];
        i++;
    }


Line 49 : what are the values of those 2 variables - does this give a clue as to why that whole loop doesn't run?

Line 58 & 69: the usual idiom for a for loop is:

58
59
60
for (int frop=counter2; frop < counter; frop++) {

}


You can investigate using a debugger - hopefully your IDE has a built in one. You can step through code 1 line at a time & keep an eye on the values of variables. You can do break points & watch lists. Other wise there is poor man's debugging which uses std::cout to print out the values of variables everywhere.

hope this helps :+)

Edit:

We look forward to seeing your new code. Hopefully you can get really good marks if you implement the things that I mentioned.

Also, can you come with better variable names, meaningful names help self document the code & aid a great deal in understanding by various people - even yourself if you look at this code in a year's time.
Last edited on
The loop at line 58 will never execute. Line 49 guarantees that counter2<counter, so the first time through the loop frop == counter2 and we know that counter2 < counter, so the condition fails.

What is the point of hold1 and hold2? Why not compare the words[counter] and words[counter2] directly? Especially since you aren't updated hold1 and hold2 inside the loop (should you?)


Rebuilt my code, tried to do a sort while I'm populating the array, now I can't tell if the sort is working when I run the while loop using the integer 'vapid'.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
//  main.cpp
//  Assignment 3
//
//  Created by Roark Burney on 10/24/14.
//  Copyright (c) 2014 Roark Burney. All rights reserved.
//

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstring>

void dictionary(int size, std::string* word);

int main()
{
    int size = 239;
    std::string* word = new std::string[size];
    char gettysburg[100][100];
    dictionary(size, word);
    return 0;
}
void dictionary(int size, std::string* word)
{
    char hold1[20]=" ";
    char hold2[20]=" ";
    std::string holder;
    std::string twentyblank="                    ";
    std::fstream unsorted;
    unsorted.open("/Users/roarkburney/Documents/C++ 2014/Assignment 3/Assignment 3/unsorted_words copy.txt");
    if (unsorted.is_open())
    {
        std::cout << "File open";
    }
    else{std::cout << "File did not open";}
    unsorted >> word[0];
    for (int x=1; x<size; x++)
    {
        unsorted >> word[x];
//        std::cout << word[x]<< " ";//check for array being populated
        for (int y=x-1; y>0; y--)
        {
            sprintf(hold1, "%.19s", word[x].c_str());
            sprintf(hold2, "%.19s", word[y].c_str());
            if (strcmp(hold1, hold2)<0)
            {
                for (int z=x+1; z==y; z--)
                {
                    word[z]=word[z-1];
                }
                word[y]=word[x];
            }
            sprintf(hold1, "%.19s", twentyblank.c_str());
            sprintf(hold2, "%.19s", twentyblank.c_str());
        }
    }
    for(int vapid =0; vapid<size;vapid++) //WHY DOESN'T THIS WORK FOR ME
    {
        std::cout << word[vapid] << " ";
        
    }
}

/*void parse()
{
    std::ifstream fourscore;
    fourscore.open("/Users/roarkburney/Documents/C++ 2014/Assignment 3/Assignment 3/gettysburg.txt");
}*/
Ok, so why are you still using constructs like this:

50
51
52
53
for (int z=x+1; z==y; z--)
{
                    word[z]=word[z-1];
                }


I thought that the reason == doesn't work in a for loop had been explained already? Do you realise that part is the end condition ?

Either try using a debugger, or put std::cout calls in to see what the values are, like after line 51 for example.

Hope all goes well :+)
Last edited on
Topic archived. No new replies allowed.