#include<iostream>
#include <string>
#include <sstream>
using namespace std;

int main(){
int i=0, ii = 0, x;
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)>> x;

//sets up array of however many X is with a limit of 50 characters
char lines[x-1][50];
char liness[ii][50];

system("cls");


while(i<x){
cout <<"Enter Movie # "<<i+1<<" : ";
cin.getline(lines[i], 50);
cout <<"Enter Rating: ";
cin.getline(liness[ii], 50);
system("cls");

i++;
ii++;
}

system("cls");
cout << "Results Are As Follows :" << endl << endl;
i = -1 ;
ii = -1;
for ( int z = 0; z <= x - 1;)
{
i++;
ii++;
cout << lines[i] << endl << "Rating : " << liness[ii] << endl << endl;

z++;
}

   
system("pause");
return 0;
}

I am trying to get a user to enter the amount of movies they want to store, then take the name of the movies and sort them alphabetically, and i am trying to figure out how to sort it with placeholders but am still having a problem and i also want to be able to sort them via rating so if anyone could point me in the right direction or help this scrubbie i would be most excited and appreciative.

Recommended Answers

All 15 Replies

Sorry for not indenting for i copied it directly from my compiler.

But It does work properly, did you run it? I am using the very basic Dev-C++ and it compiles and runs fine.

Now what it does is asks the user how many movies they want to enter into the database.

Upon that it goes to the next part and asks for a movie name followed by a rating 1-5 from the user, upon reaching the user defined amount of movies the user wants to enter it ends the array/loop and then once you hit enter it displays the movie directly followed by the rating the user gave it. So i don't understand how it is not working properly.

but either way its established, takes in the information, repeats it to the user, now I just need to sort it alphabetically.

Now i am trying to use/ understand sort() but still cant seem to do this correctly for some odd reason..

char lines[x-1][50];
char liness[ii][50];
So why isn't the second one also x-1 ?

Also, if you're doing while(i<x), then the major size of both arrays should in fact be x.

You might want to pick some better variable names, like say maxStudents (and not x)

I can't see why you need i and ii as loop variables, as they work in lock-step with one another.

#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;
}

Now not to be rude, but i changed some of it to suit you as in the max movies, but i kept the II as a counter kind of thing, but instead of critiquing how i wrote this or why i am using this can you assist me in the fact of learning to sort my two sets of arrays.

the two arrays are:
char movies[maxMovies][50];
char ratings[ii][50];

so if you could assist I would be most appreciative, but i do know i should use words to define variables but i just want to get this working properly before i redo the variables so if you could assist i would appreciate it.

again please post someone if yo ucan teach me to sort the arrays or lean me in the right direction or something so i can sort alphabetically.

Like i understand i suck at this, but i am trying, its just for some reason i cant wrap my mind around how to do this.

> char ratings[ii][50];
Again, ii is ZERO when you do this.

Why isn't it
char ratings[maxMovies][50];

Okay so now my code is at the point:

#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[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++;
}

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;
}

so i changed it so i suppose that will make it more efficient in that sense, so it continually does what i need it to do, but what about the sorting when i try an if statement to create a sorting it causes issues and doesnt switch arrays...

#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

//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?

As Salem said in his first post

First, indent your code.
http://sourceforge.net/apps/mediawiki/cpwiki/index.php?title=Indentation

Or if you don't like his link, use this one. The reason is
1) you want our help
2) you post code we cannot read
3) if we can't read it, we can't help.
Think about it.

As for your sort, you have ratings defines as a character array. You can't say array1 = array2; so use strcpy()

hopefully this made it easier to read

#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++;
                       
}

//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;
}

Okay still trying to sort, now i figured out how to use strcpy without a problem now its just a matter of figuring out how to simply say

char placeHolder[]="place holder";
int z = 0;
if ( ratings[z] < ratings[z + 1] )
{
        strcpy (placeHolder[], ratings[z]);
        strcpy (ratings[z], ratings[z+1]);
        strcpy (ratings[z + 1], placeHolder[]);
        z++;
}

so that basically if ratings[z] is less than ratings[z + 1] or simply put if "ratings[0]" is less than "ratings[1]" then we take "ratings[z]" and move it to "placeHolder[]", after that we take "ratings[z + 1]" and move it to "[ratings[z]", and finally we take "placeHolder[]" and move it to "ratings[z+1]" and then increment "int z" by "1" to go through the array further, somewhere im not doing it right to create the ability to sort so any other guidance again? hopefully this time i made it easier to read.

Edit: The above code i didnt try and doubt works but that gives the idea of what i want so if its a "2 < 1" i want it to do that situation kinda thing so again sorry for the spammy posts and thanks!

okay now ive got this:

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++;
}

and the problem is it doesnt read it like

if ( 4 < 5 ) so it doesnt sort properly... hmmm halp?

You started off with std::string, why did you go over to messy char arrays?

For one thing, std::string allows you to do assignment, like you're expecting to be able to do.
Eg.

string movies[maxMovies];

///
string temp = movies[i];
movies[i] = movies[j];
movies[j] = temp;

Later on, explore the possibilities that vector<string> movies; has to offer.


Then there's this...

for ( int z = 0; z <= maxMovies - 1;)
{
      i++;
      ii++;
      cout << movies[i] 
           << endl 
           << "Rating : " 
           << ratings[ii] 
           << endl 
           << endl;

           z++;
}

Which should be written like this:

for ( int z = 0; z < maxMovies; z++)
{
      cout << movies[z] 
           << endl 
           << "Rating : " 
           << ratings[z] 
           << endl 
           << endl;
}

Simpler, no?

You started off with std::string, why did you go over to messy char arrays?

For one thing, std::string allows you to do assignment, like you're expecting to be able to do.
Eg.

string movies[maxMovies];

///
string temp = movies[i];
movies[i] = movies[j];
movies[j] = temp;

Later on, explore the possibilities that vector<string> movies; now i see that, but if i use string temp, and the user inputs like "Die Hard 2: Die Harder" isnt it just going to record "Die"?

by the way i am now using

int yu = 0;
     int zz = 0;
     int ily = 0;
     int zzd = 0;

while (vvv[zzd] < vvv[zzd + 1] )
{

        


       
       yu = vvv[zz];
       vvv[zz] = vvv[zz+1];
      vvv[zz + 1] = yu;

      zz++;
      ily++;
      zzd++;
}

to try and sort but it only sorts the first time then disregards the rest.

Is ratings a string, a char array, or an integer?
Therein lies most of your sort problem. You can't use chars to check > the way you are trying to.

yes, its a character array getting filled in by the first 50 characters so to sort it it would be as easy as switching the first character to ascii in each movie title to sort through.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.