Thread: Problems with declaration

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Talking Problems with declaration

    I've written a program that sorts a list of 188 movies based on, title, year, media type and length. I am having problems with the creation of a menu though. It seems I cannot create a
    working menu and here is the problem are of my code. The sort algorithms work but i cannot seem to pass the menu selection off the the int choice correctly. Can someone help me?
    Code:
    void selectionSort(const int, int, bool);
    int main()
    {    
    
    	int choice;
            string record;
    	int recordcount = 0;
    	ifstream dataIn;
    
    	//Open file
    	dataIn.open("Movies.dat");
    	if(dataIn.fail())
    	{
    		cout << "Unable to open the data file." << endl;
    		return 99;
    	}
    
    	//Read a record
    	getline(dataIn,record);
        
        //location in the record of field separator (,)
    	int firstHalf = 0;
    	int i;
    
        
        recordcount = 0;
    	
        while(dataIn.eof() == false)
    	{
            if(record == "")
    		{
               break;
            }
            i = record.find(',',0);
            title[recordcount] = record.substr(0,i); 
            firstHalf =  i+ 1;
            i = record.find(',',i+1);
            year[recordcount] = record.substr(firstHalf, i - firstHalf);
            firstHalf =  i+ 1;
            i = record.find(',',i+1);
            length[recordcount] = record.substr(firstHalf, i - firstHalf);
            firstHalf =  i+ 1;
            i = record.find(',',i+1);
            mediaType[recordcount] = record.substr(firstHalf, i - firstHalf);
    		recordcount++;	//point to next element
    		getline(dataIn,record);
    	}
        
        
        //Menu to select which type of sort
        cout << "Which way would you like to see these movies sorted?" << endl;
        cout << "A. By title. (via Bubble sort (Ascending)) " << endl;
        cout << "B. By year.(via Bubble sort (Ascending))" << endl;
        cout << "C. By media.(via Bubble sort (Ascending))" << endl;
        cout << "D. By length.(via Bubble sort (Asending))" << endl;
        cout << "E. By title. (via selection sort (Decending)) " << endl;
        cout << "F. By year.(via selection sort (Decending))" << endl;
        cout << "G. By media.(via selection sort (Decending))" << endl;
        cout << "H. By length.(via selection sort (Decending))" << endl;
        cout << "Enter your choice here:" << endl;
        cin >> choice >> endl;
        
        //Where we plug in choice
        if (choice = A)
        {
                   Sort(choice,188,true);
                   for (int i = 0; i < recordcount; i++)
                   {
                       if((i &#37; 5) == 0)
                       {
                             cout << "Press [Enter] to continue";
                             cin.get();
                             }
                             cout << "title:"<<title[i]<<"\n";
                             cout << "year:"<<year[i]<<"\n";
                             cout << "media:"<<mediaType[i]<<"\n";
                             cout << "length:"<<length[i]<<"\n";
                             }
                             return 0;
                       }
                   }
        }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When you got the warning about doing an assignment inside the conditional of an if statement, and the error about an undeclared variable A, on this line here:
    Code:
        if (choice = A)
    that should have told you something.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    so declare A?

    with int or float?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You've defined choice as an int.

    What is A? And why are you assigning it to choice?
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    24

    Heres why

    So that I can display based on the 8 different choices.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    //Constants - This is used inside sort function to determine 
    //which field to sort on
    
    const int MOVIE_TITLE = 0;
    const int YEAR = 1;
    const int MEDIA = 2;
    const int LENGTH =3;
    
    
    //Global Variables
    	string title[188];
    	string year[188];
    	string mediaType[188];
    	string length[188];
    
    void Sort(const int col, int howMany, bool dir)
    {
       switch(col)
       {
          case MOVIE_TITLE:	
          {
             string temp_title;
             string temp_year;
             string temp_mediaType;
             string temp_length;
             int limit = howMany;//The number of elements in the array to be searched
             bool swapped = true;
             int j = 0;
             while(swapped)
             {              
             
                swapped = false;//No swapp has taken place
                //Propagate through array
           
                for(int sub=0;sub<limit - 1; sub++)
                {
                   //dir - asc, or desc order, nothing to do with files
                   if(dir)
                   {
                      //cout << "IN DIR IF"<<endl;cin.get();
                      //ASCENDING
                      if(title[sub] > title[sub+1])
                      {
                            
                            //elements are out of order..swap them
                            //Write first to temp   
                            temp_title = title[sub];
                            temp_year = year[sub];
                            temp_mediaType = mediaType[sub];
                            temp_length = length[sub];
                            //Overwrite first with second
                            title[sub] = title[sub+1];
                            year[sub] = year[sub+1];    
    	 
                            mediaType[sub] = mediaType[sub+1];
                            length[sub] = length[sub+1];
                           
                            //Overwrite second with temp
                            title[sub+1] = temp_title;
                            year[sub+1] = temp_year;
                            mediaType[sub+1] = temp_mediaType;
                            length[sub+1] = temp_length;
                            swapped = true;
                      }
                   }//End If Level 7
                   else
                   {
                       //cout << "   DIR IS NOLONGER TRUE"<<endl;cin.get();
                       //DESCENDING
                       if(title[sub] < title[sub+1])
                       {
                          //cout << " IN IF1"<<endl;cin.get();
                          //elements are out of order..swap them
                          //Write first to temp   
                          temp_title = title[sub];
                          temp_year = year[sub];
                          temp_mediaType = mediaType[sub];
                          temp_length = length[sub];
                          //Overwrite first with second
                          title[sub] = title[sub+1];
                          year[sub] = year[sub+1];    
    	                        mediaType[sub] = mediaType[sub+1];
                          length[sub] = length[sub+1];
                         
                          //Overwrite second with temp
                          title[sub+1] = temp_title;
                          year[sub+1] = temp_year;
                          mediaType[sub+1] = temp_mediaType;
                          length[sub+1] = temp_length;
                          swapped = true;
                       }
                    }//End Else Level 7
                   //}//End If Level 6
                }//End For Level 5
             }//End While Level 4
          }//End Case Movie Level3
          break;
          case YEAR:	
          {
             string temp_title;
             string temp_year;
             string temp_mediaType;
             string temp_length;
             int limit = howMany;//The number of elements in the array to be searched
             bool swapped = true;
             int j = 0;
             while(swapped)
             {              
                //cout << "In While"<<endl;
                swapped = false;//No swapp has taken place
                //Propagate through array
           
                for(int sub=0;sub<limit - 1; sub++)
                {
                   //dir - asc, or desc order, nothing to do with files
                   if(dir)
                   {
                      //cout << "IN DIR IF"<<endl;cin.get();
                      //ASCENDING
                      if(year[sub] > year[sub+1])
                      {
                            
                            //elements are out of order..swap them
                            //Write first to temp   
                            temp_title = title[sub];
                            temp_year = year[sub];
                            temp_mediaType = mediaType[sub];
                            temp_length = length[sub];
                            //Overwrite first with second
                            title[sub] = title[sub+1];
                            year[sub] = year[sub+1];    
    	 
                            mediaType[sub] = mediaType[sub+1];
                            length[sub] = length[sub+1];
                           
                            //Overwrite second with temp
                            title[sub+1] = temp_title;
                            year[sub+1] = temp_year;
                            mediaType[sub+1] = temp_mediaType;
                            length[sub+1] = temp_length;
                            swapped = true;
                      }
                   }//End If Level 7
                   else
                   {
                       //cout << "   DIR IS NOLONGER TRUE"<<endl;cin.get();
                       //DESCENDING
                       if(year[sub] < year[sub+1])
                       {
                          //cout << " IN IF1"<<endl;cin.get();
                          //elements are out of order..swap them
                          //Write first to temp   
                          temp_title = title[sub];
                          temp_year = year[sub];
                          temp_mediaType = mediaType[sub];
                          temp_length = length[sub];
                          //Overwrite first with second
                          title[sub] = title[sub+1];
                          year[sub] = year[sub+1];    
    	                        mediaType[sub] = mediaType[sub+1];
                          length[sub] = length[sub+1];
                         
                          //Overwrite second with temp
                          title[sub+1] = temp_title;
                          year[sub+1] = temp_year;
                          mediaType[sub+1] = temp_mediaType;
                          length[sub+1] = temp_length;
                          swapped = true;
                       }
                    }//End Else Level 7
                   //}//End If Level 6
                }//End For Level 5
             }//End While Level 4
          }//End Case Movie Level3
          break;
          case MEDIA:	
          {
             string temp_title;
             string temp_year;
             string temp_mediaType;
             string temp_length;
             int limit = howMany;//The number of elements in the array to be searched
             bool swapped = true;
             int j = 0;
             while(swapped)
             {              
                //cout << "In While"<<endl;
                swapped = false;//No swapp has taken place
                //Propagate through array
           
                for(int sub=0;sub<limit - 1; sub++)
                {
                   //dir - asc, or desc order, nothing to do with files
                   if(dir)
                   {
                      //cout << "IN DIR IF"<<endl;cin.get();
                      //ASCENDING
                      if(mediaType[sub] > mediaType[sub+1])
                      {
                            
                            //elements are out of order..swap them
                            //Write first to temp   
                            temp_title = title[sub];
                            temp_year = year[sub];
                            temp_mediaType = mediaType[sub];
                            temp_length = length[sub];
                            //Overwrite first with second
                            title[sub] = title[sub+1];
                            year[sub] = year[sub+1];    
    	 
                            mediaType[sub] = mediaType[sub+1];
                            length[sub] = length[sub+1];
                           
                            //Overwrite second with temp
                            title[sub+1] = temp_title;
                            year[sub+1] = temp_year;
                            mediaType[sub+1] = temp_mediaType;
                            length[sub+1] = temp_length;
                            swapped = true;
                      }
                   }//End If Level 7
                   else
                   {
                       //cout << "   DIR IS NOLONGER TRUE"<<endl;cin.get();
                       //DESCENDING
                       if(mediaType[sub] < mediaType[sub+1])
                       {
                          //cout << " IN IF1"<<endl;cin.get();
                          //elements are out of order..swap them
                          //Write first to temp   
                          temp_title = title[sub];
                          temp_year = year[sub];
                          temp_mediaType = mediaType[sub];
                          temp_length = length[sub];
                          //Overwrite first with second
                          title[sub] = title[sub+1];
                          year[sub] = year[sub+1];    
                          mediaType[sub] = mediaType[sub+1];
                          length[sub] = length[sub+1];
                         
                          //Overwrite second with temp
                          title[sub+1] = temp_title;
                          year[sub+1] = temp_year;
                          mediaType[sub+1] = temp_mediaType;
                          length[sub+1] = temp_length;
                          swapped = true;
                       }
                    }//End Else Level 7
                   //}//End If Level 6
                }//End For Level 5
             }//End While Level 4
          }//End Case Movie Level3
          break;
          case LENGTH:	
          {
             string temp_title;
             string temp_year;
             string temp_mediaType;
             string temp_length;
             int limit = howMany;//The number of elements in the array to be searched
             bool swapped = true;
             int j = 0;
             while(swapped)
             {              
                //cout << "In While"<<endl;
                swapped = false;//No swapp has taken place
                //Propagate through array
           
                for(int sub=0;sub<limit - 1; sub++)
                {
                   //dir - asc, or desc order, nothing to do with files
                   if(dir)
                   {
                      //cout << "IN DIR IF"<<endl;cin.get();
                      //ASCENDING
                      if(length[sub] > length[sub+1])
                      {
                            
                            //elements are out of order..swap them
                            //Write first to temp   
                            temp_title = title[sub];
                            temp_year = year[sub];
                            temp_mediaType = mediaType[sub];
                            temp_length = length[sub];
                            //Overwrite first with second
                            title[sub] = title[sub+1];
                            year[sub] = year[sub+1];    
    	 
                            mediaType[sub] = mediaType[sub+1];
                            length[sub] = length[sub+1];
                           
                            //Overwrite second with temp
                            title[sub+1] = temp_title;
                            year[sub+1] = temp_year;
                            mediaType[sub+1] = temp_mediaType;
                            length[sub+1] = temp_length;
                            swapped = true;
                      }
                   }//End If Level 7
                   else
                   {
                       //cout << "   DIR IS NOLONGER TRUE"<<endl;cin.get();
                       //DESCENDING
                       if(length[sub] < length[sub+1])
                       {
                          //cout << " IN IF1"<<endl;cin.get();
                          //elements are out of order..swap them
                          //Write first to temp   
                          temp_title = title[sub];
                          temp_year = year[sub];
                          temp_mediaType = mediaType[sub];
                          temp_length = length[sub];
                          //Overwrite first with second
                          title[sub] = title[sub+1];
                          year[sub] = year[sub+1];    
    	                        mediaType[sub] = mediaType[sub+1];
                          length[sub] = length[sub+1];
                         
                          //Overwrite second with temp
                          title[sub+1] = temp_title;
                          year[sub+1] = temp_year;
                          mediaType[sub+1] = temp_mediaType;
                          length[sub+1] = temp_length;
                          swapped = true;
                       }
                    }//End Else Level 7
                   //}//End If Level 6
                }//End For Level 5
             }//End While Level 4
          }//End Case Movie Level3
          break;
       }//End Switch level2
    }//End Sort
    void selectionSort(const int, int, bool);
    int main()
    {    
    
    	string record;
    	int recordcount = 0;
    	ifstream dataIn;
    	float A, B, C, D, E, F, G, H;
    
    	//Open file
    	dataIn.open("Movies.dat");
    	if(dataIn.fail())
    	{
    		cout << "Unable to open the data file." << endl;
    		return 99;
    	}
    
    	//Read a record
    	getline(dataIn,record);
        
        //location in the record of field separator (,)
    	int firstHalf = 0;
    	int i;
    
        
        recordcount = 0;
    	
        while(dataIn.eof() == false)
    	{
            if(record == "")
    		{
               break;
            }
            i = record.find(',',0);
            title[recordcount] = record.substr(0,i); 
            firstHalf =  i+ 1;
            i = record.find(',',i+1);
            year[recordcount] = record.substr(firstHalf, i - firstHalf);
            firstHalf =  i+ 1;
            i = record.find(',',i+1);
            length[recordcount] = record.substr(firstHalf, i - firstHalf);
            firstHalf =  i+ 1;
            i = record.find(',',i+1);
            mediaType[recordcount] = record.substr(firstHalf, i - firstHalf);
    		recordcount++;	//point to next element
    		getline(dataIn,record);
    	}
        
        
        //Menu to select which type of sort
        cout << "Which way would you like to see these movies sorted?" << endl;
        cout << "A. By title. (via Bubble sort (Ascending)) " << endl;
        cout << "B. By year.(via Bubble sort (Ascending))" << endl;
        cout << "C. By media.(via Bubble sort (Ascending))" << endl;
        cout << "D. By length.(via Bubble sort (Asending))" << endl;
        cout << "E. By title. (via selection sort (Decending)) " << endl;
        cout << "F. By year.(via selection sort (Decending))" << endl;
        cout << "G. By media.(via selection sort (Decending))" << endl;
        cout << "H. By length.(via selection sort (Decending))" << endl;
        cout << "Enter your choice here:" << endl;
        cin >> int choice >> endl;
        
        //Where we plug in choice
        if (choice = A)
        {
                   Sort(choice,188,true);
                   for (int i = 0; i < recordcount; i++)
                   {
                       if((i % 5) == 0)
                       {
                             cout << "Press [Enter] to continue";
                             cin.get();
                             }
                             cout << "title:"<<title[i]<<"\n";
                             cout << "year:"<<year[i]<<"\n";
                             cout << "media:"<<mediaType[i]<<"\n";
                             cout << "length:"<<length[i]<<"\n";
                             }
                             return 0;
                       }
                   }
        }
                       
    void selectionSort(const int size, int col, bool dir)
    {
       int i;
       int currentIndex;
       //Array of numbers to sort
       
       string smallest;
       int smallestIndex;
       
       //Perform selection sort
       for(currentIndex = 0; currentIndex < size; currentIndex++)
       {
          //Look for the smallest number, and 
          //its index in the array.
          smallest = title[currentIndex];
          smallestIndex = currentIndex;
          for(i = currentIndex; i < size; i++)
          {
             if(title[i] < smallest)
             {
                smallest = title[i];//Store the smallest number
                smallestIndex = i;//store its index
             }
          }
          //swap the current number with
          //the smallest one
          title[smallestIndex] = title[currentIndex];
          title[currentIndex] = smallest;
       }
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sedvan View Post
    So that I can display based on the 8 different choices.
    Read the line again:
    Code:
    choice = A
    What does that line of code do? Right! It assigns the value of the variable A to the variable choice. Now, do we want A to be a variable? <Expletive of your choice> no! It's supposed to be the letter A -- a constant -- a constant that is a character -- specifically the character constant 'A'.

    Now: do we want to assign 'A' to the variable choice? No! We want to compare, so we should use the comparison operator instead.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In the future, may I suggest you try to narrow down to where your code isn't working? No one wants to look through a couple of hundreds of lines of code to find your problem.
    Also, you should turn up the warnings on your compiler because trying to assign when wanting to compare is a very subtle and easily caught problem by the compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Class declaration problems
    By scwizzo in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2008, 07:47 PM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  5. Class Declaration?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-27-2002, 09:09 PM