Help A Newbie Sort, Please :)

This is a discussion on Help A Newbie Sort, Please :) within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> #include <string> #include <sstream> using namespace std; int main(){ int i=0, ii = 0, maxMovies; string input; //command ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    Help A Newbie Sort, Please :)

    Code:
    #include<iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main(){
    int i=0, ii = 0, maxMovies;
    string input;
    
    //command to prompt user for amount of movies
    cout<< "How many movies do you want to add? ";
    getline(cin,input);
    //end command to prompt user for amount of movies
    
    //set up loop situation to take the input from user and transfer it to X
    stringstream(input)>> maxMovies;
    
    //sets up array of however many X is with a limit of 50 characters
    char movies[maxMovies][50];
    char ratings[ii][50];
    
    system("cls");
    
    
    while(i< maxMovies){
    cout <<"Enter Movie # "<<i+1<<" : ";
    cin.getline(movies[i], 50);
    cout <<"Enter Rating: ";
    cin.getline(ratings[ii], 50);
    system("cls");
    
    i++;
    ii++;
    }
    
    system("cls");
    
                  cout << "Results Are As Follows :" << endl << endl;
                       i = -1 ;
                       ii = -1;
                  for ( int z = 0; z <= maxMovies - 1;)
                  {
                        i++;
                       ii++;
                       cout << movies[i] << endl << "Rating : " << ratings[ii] << endl << endl;
                       z++;
                       }
    
       
    system("pause");
    return 0;
    }
    is my actual code and what i am trying to do is get a user to enter a number for how many movies they want to add, then ask them the movie name followed by what they would rate it, it does that and prints it back to the user but now i need to figure out how to get the program to sort the movies alphabetically if the user wants or by ratings in descending order, so if someone could show me a newbie friendly way to do that i would be most appreciative.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Since variable length arrays are not part of C++, you cannot actually do this, at least not without a compiler extension:
    Code:
    char movies[maxMovies][50];
    So, instead of trying to use a variable length array, use a std::vector<std::string>. Besides this, you should use this rule of thumb: declare variables near first use. Oh, and you also need to indent your code properly. In summary:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    
    using namespace std;
    
    int main() {
        cout << "How many movies do you want to add? ";
        string input;
        getline(cin, input);
    
        int maxMovies;
        stringstream(input) >> maxMovies;
    
        // create maxMovies number of movies and corresponding ratings
        vector<string> movies(maxMovies);
        vector<string> ratings(maxMovies);
    
        for (vector<string>::size_type i = 0; i < movies.size(); ++i) {
            cout << "Enter Movie # " << i + 1 << " : ";
            getline(cin, movies[i]);
            cout << "Enter Rating: ";
            getline(cin, ratings[i]);
        }
    
        cout << "Results Are As Follows :" << endl << endl;
        for (vector<string>::size_type i = 0; i < movies.size(); ++i) {
            cout << movies[i] << endl << "Rating : " << ratings[i] << endl << endl;
        }
    
        cin.get(); // pause
        return 0;
    }
    Oh, and about sorting: #include <algorithm> and use std::sort.

    EDIT:
    Actually, it is more involved than that. A better solution is to create a Movie class with name and rating member variables, then you provide std::sort with a comparison function, as demonstrated by the article that I linked to. The two std::vector<std::string> would be replaced by a single std::vector<Movie>.
    Last edited by laserlight; 04-08-2010 at 02:26 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    7

    #3

    Yeah i am trying to understand that site and am trying to implement my array ( ratings ) where it has sort( movies, movies + array_size); and still am getting errors hah oh well thanks for the help suppose ima have to look towards getting a c++ for dummies book hah.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Don't discourage yourself. Slow and steady wins the race.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Not really discouraged just sad that I am unable to find a simple way to do this then when I finally get it I will be kicking myself lol.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Code:
    #include<iostream>
    #include <string>
    #include <sstream>
    #include <algorithm>
    
    
    using namespace std;
    
    int main(){
    
        
    int i=0, ii = 0, maxMovies;
    string input;
    
    //command to prompt user for amount of movies
    cout<< "How many movies do you want to add? ";
    getline(cin,input);
    //end command to prompt user for amount of movies
    
    //set up loop situation to take the input from user and transfer it to X
    stringstream(input)>> maxMovies;
    
    //sets up array of however many X is with a limit of 50 characters
    char movies[maxMovies][50];
    char ratings[maxMovies][50];
    
    
    system("cls");
    
    
    while(i < maxMovies){
    cout <<"Enter Movie # "<<i+1<<" : ";
    cin.getline(movies[i], 50);
    cout <<"Enter Rating: ";
    cin.getline(ratings[ii], 50);
    system("cls");
    
    i++;
    ii++;
    }
    
    //if ( ratings[maxMovies] > ratings[maxMovies + 1])
    //{
         
         // int temp;
    //temp = ratings[maxMovies];
       //  ratings[maxMovies] = ratings[maxMovies + 1];
       //  ratings[maxMovies + 1] = temp;
    // }
    
    system("cls");
    cout << "Results Are As Follows :" << endl << endl;
    i = -1 ;
    ii = -1;
    for ( int z = 0; z <= maxMovies - 1;)
    {
    i++;
    ii++;
    cout << movies[i] << endl << "Rating : " << ratings[ii] << endl << endl;
    
    z++;
    }
    
       
    cin.get();
    return 0;
    }
    now i am trying to sort by using

    Code:
    //if ( ratings[maxMovies] > ratings[maxMovies + 1])
    //{
         
         // int temp;
    //temp = ratings[maxMovies];
       //  ratings[maxMovies] = ratings[maxMovies + 1];
       //  ratings[maxMovies + 1] = temp;
    // }
    but i keep getting an error:

    ISO C++ forbids assignment of arrays

    hints anyone?
    Last edited by method4ever2010; 04-08-2010 at 07:03 PM.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    Code:
    #include<iostream>
    #include <string>
    #include <sstream>
    #include <algorithm>
    
    
    using namespace std;
    
    int main(){
    
        
         
    int i=0, ii = 0, maxMovies;
    string input;
    
    //command to prompt user for amount of movies
    cout<< "How many movies do you want to add? ";
    getline(cin,input);
    //end command to prompt user for amount of movies
    
    //set up loop situation to take the input from user and transfer it to X
    stringstream(input)>> maxMovies;
    
    //sets up array of however many X is with a limit of 50 characters
    char movies[maxMovies][50];
    char ratings[maxMovies][50];
    int count[maxMovies];
    
    system("cls");
    
    
    while( i < maxMovies ){
                          
                           cout <<"Enter Movie # "<<i+1<<" : ";
                         
                           cin.getline(movies[i], 50);
                         
                           cout <<"Enter Rating: ";
                          
                           cin.getline(ratings[ii], 50);
    
                           system("cls");
    
    
                           i++;
                           ii++;
                           
    }
    
    
    
    int z = 0;
    char placeHolder[]="Place Holder";
    if ( ratings[z] < ratings[z + 1] )
    {
         
            strcpy ( placeHolder[] , ratings[z] );
            strcpy ( ratings[z] , ratings[z+1] );
            strcpy ( ratings[z+1] , placeHolder[] );
            z++;
    }
    //test string copies from ratings[z] to ratings[z+1]
    //int z = 0;
    //strcpy (ratings[z + 1], ratings[z]);
    
    
    
    
    
    system("pause");
    
    
    system("cls");
    cout << "Results Are As Follows :" << endl << endl;
    
    i = -1 ;
    ii = -1;
    
    
    for ( int z = 0; z <= maxMovies - 1;)
    {
          i++;
          ii++;
          cout << movies[i] 
               << endl 
               << "Rating : " 
               << ratings[ii] 
               << endl 
               << endl;
    
               z++;
    }
    
       
    cin.get();
    return 0;
    }
    heres my code and as you can see i am struggling with strcpy so any assistance please

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Err... why do you refuse to use std::string?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    well now i have it as

    Code:
    char string[50]="place holder";
         int zz = 0;
    if ( ratings[zz] < ratings[zz + 1] )
    {
    
            strcpy (string, ratings[zz]);
            strcpy (ratings[zz], ratings[zz+1]);
            strcpy (ratings[zz + 1], string);
            zz++;
    }

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Well, I do not really see the point of helping when my suggestions are ignored. I demonstrated code in post #2 that is a step forward, and provided a suggestion of a Movie class.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    7
    well if i understood your suggestion and when i change code in mine if it worked id be happy but i am simply a newber with a book learning and that is way beyond me at the second but i have got my code to semi-work by using the atoi to convert the string to an integer for me but i still cant get it to do its job properly.

    Code:
    #include<iostream>
    #include <string>
    #include <sstream>
    #include <algorithm>
    
    
    using namespace std;
    
    int main(){
    
        
         
    int i=0, ii = 0, maxMovies;
    int vvv[maxMovies];
    
    string input;
    
    
    char string[50]="place holder";
    
    //command to prompt user for amount of movies
    cout<< "How many movies do you want to add? ";
    getline(cin,input);
    //end command to prompt user for amount of movies
    
    //set up loop situation to take the input from user and transfer it to X
    stringstream(input)>> maxMovies;
    
    //sets up array of however many X is with a limit of 50 characters
    char movies[maxMovies][50];
    char ratings[maxMovies][50];
    int count[maxMovies];
    
    system("cls");
    
    
    while( i < maxMovies ){
                          
                           cout <<"Enter Movie # "<<i+1<<" : ";
                         
                           cin.getline(movies[i], 50);
                         
                           cout <<"Enter Rating: ";
                          
                           cin.getline(ratings[ii], 50);
                
                           system("cls");
    
                         
                           i++;
                           ii++;
                           
    }
    
    
    //converting rating in integer from string
    int lo = 0;
    int zztop = 0;
    int zzjr = 0;
    do  
    {
     vvv[zztop] = atoi (ratings[zzjr]);
     zztop++;
     zzjr++;
     lo++;
                           
                           }
                           while ( lo <= maxMovies );
         //end conversion of integer from string
         
         
         int yu = 0;
         int zz = 0;
         int ily = 0;
         int zzd = 0;
    
    while (vvv[zzd] < vvv[zzd + 1] )
    {
    
            
    
    
           //strcpy (yu, vvv[zz]);
           yu = vvv[zz];
           vvv[zz] = vvv[zz+1];
           vvv[zz + 1] = yu;
            //strcpy (vvv[zz], vvv[zz+1]);
            //strcpy (vvv[zz + 1], yu);
          zz++;
          ily++;
          zzd++;
    }
    
    
    //test string copies from ratings[z] to ratings[z+1]
    //int z = 0;
    //strcpy (ratings[z + 1], ratings[z]);
    
    
    
    
    system("pause");
    
    
    system("cls");
    cout << "Results Are As Follows :" << endl << endl;
    
    i = -1 ;
    ii = -1;
    
    
    for ( int z = 0; z <= maxMovies - 1;)
    {
          i++;
          ii++;
          cout << movies[i] 
               << endl 
               << "Rating : " 
               << vvv[z] 
               << endl 
               << endl;
    
               z++;
    }
    
       
    cin.get();
    return 0;
    }
    because now i am using

    Code:
    while (vvv[zzd] < vvv[zzd + 1] )
    {
    
            
    
    
           //strcpy (yu, vvv[zz]);
           yu = vvv[zz];
           vvv[zz] = vvv[zz+1];
           vvv[zz + 1] = yu;
            //strcpy (vvv[zz], vvv[zz+1]);
            //strcpy (vvv[zz + 1], yu);
          zz++;
          ily++;
          zzd++;
    }
    and still it only swaps the first 2 variables if needed but disregards the rest, and sorry but i dont understand your post it looks swell and im sure it works better but i cant understand it to the fullest so it doesnt help me as much sadly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21