Sorting idea, need ideas

The program should count the value of N words (for example, A is 1, B is 2, C is 3 and so on .. , so if I enter AAA the value would be 3) and sort them by values. What I have right now:

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
        #include <iostream>
        #include <algorithm>
        using namespace std;
         int n;
        char azb[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        int vred[26];
        string a;
        int s=0;
    
    int getpos(char _find)
    {
        int pos=-1;
        for(int i=0; i<=25; i++) if(azb[i]==_find) pos=i;
        return pos;
    }
    int main()
    {
        cin >> n;
        string ime[n];
    //polnenje na vred
     for(int i=0; i<=25; i++) vred[i]=i+1;
    
    //main
    for(int j=0; j<=n-1; j++)
    { s=0;
        cin >> a;
    for(int i=0; i<=a.length()-1; i++)
    {
        int pos=getpos(a[i]);
        s=s+vred[pos];
    }
    }
    // do the sorting
    // print the sorted list
    return 0;
    }

Any ideas? Thanks a lot, any help is appreciated.
wiseRehan wrote:
http://libraryofcprograms.blogspot.com/2013/02/bubblesort.html

Blogs with poorly written code that won't compile. Great resource you found there.


1
2
        cin >> n;
        string ime[n];

This is illegal. An array's size must be a compile time constant. You may want to disable the compiler extension that allows it. Consider using a std::vector instead of an array.

I noticed you included <algorithm>. If you can put your code to compare strings according to their "value" into a function, you can use std::sort with your function to provide the comparisons.
cire, there's not a single error in that blog.
I take it, then, that you haven't tried to compile the code.
I've compiled the code from blog. 0 error, 0 warning.
I think there's something wrong in your compiler.
I've compiled the code from blog. 0 error, 0 warning.
I think there's something wrong in your compiler.

I think you're lying through your teeth.
> I've compiled the code from blog. 0 error, 0 warning.

Using g++ or clang++ with default options?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main( int argc, char*[] )
{
    int i = ( { typeof(i) j = 3 ; for( int k = 1 ; k < 10 ; ++k ) ++j ; j ?: j ; } ) ;

    int a[0] ;
    int b[argc] ;
    int c$ = i ;

    foo: void* p = &&foo ;

    if(i) { --i ; goto *p ; }

    if(i) main(-2,0) ;
}


This abomination will compile cleanly with > g++ my_linuxisms.cc
http://liveworkspace.org/code/4skyVw$1

Compile with -std=c++11 -Wall -Wextra -pedantic-errors and you would get a bunch of errors.
http://liveworkspace.org/code/4skyVw$0

Even with all that, g++ does not generate even a warning for the atrocity on line 7. (clang++ does).
On further inspection, the code in the link provided would compile if, for some reason, the compiler wasn't compliant and variables declared in for loops had lifetimes that extended beyond the loop.

I would imagine that would need to be a fairly old compiler that didn't get it right, but I retract the "lying through your teeth" comment, although the code is certainly not valid.
I'm working on Visual C++ 6.0, and I don't need to lie. And I again say 0 error 0 warning on my compiler.
> I'm working on Visual C++ 6.0

Strongly suggest that you switch to a more current compiler.
VC++6.0 was released 15 years ago. I would second that suggestion.
Topic archived. No new replies allowed.