Thread: Counting and Arrays

  1. #1
    Registered User
    Join Date
    Jan 2005
    Location
    Illinois
    Posts
    6

    Counting and Arrays

    My head is literally about to explode with this...

    I'm trying to write a CD Collection program and I have an array where you enter an album title, artist name, and year released. My problem is, the information will not save into its array and the counter will not increase. Here is what I mean...

    struct CompactDisc
    {
    string title;
    string artist;
    string year;
    };

    int add(CompactDisc CDs[], int& count)
    {
    cout << "Enter the album title: "<< endl;
    getline(cin,CDs[count].title);
    cout << "Enter the artist: "<< endl;
    getline(cin,CDs[count].artist);
    cout << "Enter the year released: "<< endl;
    getline(cin,CDs[count].year);
    count++;
    cin.ignore(50,'\n');
    cout << "Your CDs have been added to the database." << endl;
    main();
    return 0;
    }

    Hope that's enough information.
    Last edited by Evan; 01-24-2005 at 09:36 PM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    First off please use code tags.

    Second you do not ever call main() it is a very bad idea

    Third you are going to need to post a bit more code.
    Woop?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Location
    Illinois
    Posts
    6
    Quote Originally Posted by prog-bman
    First off please use code tags.

    Second you do not ever call main() it is a very bad idea

    Third you are going to need to post a bit more code.
    Might as well post my whole program so far...

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    struct CompactDisc
    {
        string title;
        string artist;
        string year;
    };
    
    void welcome();
    void menu();
    void add(CompactDisc CDs[], int& count);
    int listDetails(CompactDisc CDs[], int count);
    int listTitles(CompactDisc CDs[], int count);
    int saveToFile(CompactDisc CDs[], int count);
    
    void welcome()
    {
        cout << left << setfill('*') << setw(15) << "*" << endl;
        cout << "CD Librarian" << endl;
        cout << left << setfill('*') << setw(15) << "*" << endl;
        cout << endl;
    }
    
    int main()
    {
        welcome();
        menu();
        return 0;
    }
    
    void menu()
    {
        int count = 0;
        CompactDisc CDs[20];
        char num;
        cout << "Here are your options:" << endl;
        cout << "\t 1.) Add a CD to your collection." << endl;
        cout << "\t 2.) List detailed information about your CDs." << endl;
        cout << "\t 3.) List just the titles of the CDs in your collection."
             << endl;
        cout << "\t 4.) Save detailed information about your CDs to a file."
             << endl;
        cout << "\t 5.) Quit." << endl;
        cout << "Enter a number: ";
        cin >> num;
        
        if (num == '1')
        {
            add(CDs, count);
        }
        else if (num == '2')
        {
            listDetails(CDs, count);
        }
        else if (num == '3')
        {
            listTitles(CDs, count);
        }
        else if (num == '4')
        {
            saveToFile(CDs, count);
        }
        else if (num == '5')
        {
            exit(1);
        }
        else
        {
            exit(1);
        }
    }
    
    void add(CompactDisc CDs[], int& count)
    {
        cout << "Enter the album title: "<< endl;
        getline(cin,CDs[count].title);
        cout << "Enter the artist: "<< endl;
        getline(cin,CDs[count].artist);
        cout << "Enter the year released: "<< endl;
        getline(cin,CDs[count].year);
        cin.ignore(50,'\n');
        count++;
        cout << "Your CDs have been added to the database." << endl;
        menu();
    }
    
    int listDetails(CompactDisc CDs[], int count)
    {
        cout << "The count is at: " << count << "." << endl;
        cout << "Here is your list of CDs: " << endl;
        while (count > 0 && count < 20)
        {
            cout << CDs[count].title << " "  << CDs[count].artist << " " << CDs[count].year << endl;
        }
        menu();
        return 0;
    }
    
    int listTitles(CompactDisc CDs[], int count)
    {
        while (count > 0 && count < 20)
        {
            cout << CDs[count].title << endl;
        }
        menu();
        return 0;
    }
    
    int saveToFile(CompactDisc CDs[], int count)
    {
        ofstream fout;
        fout.open("C:\\CDcollection.txt");
        fout << CDs[count].title << " " << CDs[count].artist << " " << CDs[count].year << endl;
        cout << "Your CD collection has been saved to your C drive." << endl;
        fout.close();
        menu();
        return 0;
    }

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Here I made some changes they are commented
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    struct CompactDisc
    {
        string title;
        string artist;
        string year;
    };
    
    void welcome();
    void menu();
    void add(CompactDisc CDs[], int& count);
    int listDetails(CompactDisc CDs[], int count);
    int listTitles(CompactDisc CDs[], int count);
    int saveToFile(CompactDisc CDs[], int count);
    
    void welcome()
    {
        cout << left << setfill('*') << setw(15) << "*" << endl;
        cout << "CD Librarian" << endl;
        cout << left << setfill('*') << setw(15) << "*" << endl;
        cout << endl;
    }
    
    int main()
    {
        welcome();
        menu();
        return 0;
    }
    
    void menu()
    {
      
      int count = 0;
      bool exit = false;
      CompactDisc CDs[20];
      char num;
      while(!exit)//Added a loop instead of recursive function calls
      {
        cout << "Here are your options:" << endl;
        cout << "\t 1.) Add a CD to your collection." << endl;
        cout << "\t 2.) List detailed information about your CDs." << endl;
        cout << "\t 3.) List just the titles of the CDs in your collection."
             << endl;
        cout << "\t 4.) Save detailed information about your CDs to a file."
             << endl;
        cout << "\t 5.) Quit." << endl;
        cout << "Enter a number: ";
        cin >> num;
        cin.ignore(80,'\n');//your cin>> is leaving a newline in the buffer that
        //the getline is picking up
        
        if (num == '1')
        {
          add(CDs, count);
        }
        else if (num == '2')
        {
          listDetails(CDs, count);
        }
        else if (num == '3')
        {
          listTitles(CDs, count);
        }
        else if (num == '4')
        {
          saveToFile(CDs, count);
        }
        else if (num == '5')
        {
          exit = true;
        }
        else
        {
          //add error checking        
        }
          
      }
          
    }
    
    void add(CompactDisc CDs[], int &count)
    {
        cout << "Enter the album title: "<< endl;
        getline(cin,CDs[count].title);
        cout << "Enter the artist: "<< endl;
        getline(cin,CDs[count].artist);
        cout << "Enter the year released: "<< endl;
        getline(cin,CDs[count].year);    
        count++;
        cout << "Your CDs have been added to the database." << endl;    
    }
    
    int listDetails(CompactDisc CDs[], int count)
    {
        int i;
        cout << "The count is at: " << count << "." << endl;
        cout << "Here is your list of CDs: " << endl;
        for(i = 0; i < count; i++)
        {
            cout << CDs[i].title << " "  << CDs[i].artist << " " << CDs[i].year << endl;
        }      
        return 0;
    }
    
    int listTitles(CompactDisc CDs[], int count)
    {
        int i;
        for(i = 0; i < count; i++)//only print out the stuff we have
        {
            cout << CDs[i].title << endl;
        }    
        return 0;
    }
    
    int saveToFile(CompactDisc CDs[], int count)
    {
        int i;
        ofstream fout;
        fout.open("CDcollection.txt",ios::out);
        
        for(i = 0; i < count; i++)//write all you cd info to the file
        {
          
          fout << CDs[i].title << " " << CDs[i].artist << " " << CDs[i].year << endl;
        
        }      
        cout << "Your CD collection has been saved to your C drive." << endl;
        fout.close();    
        return 0;
    }
    Woop?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Location
    Illinois
    Posts
    6
    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Help with number counting program
    By Turtal in forum C Programming
    Replies: 11
    Last Post: 04-25-2009, 02:40 PM
  3. Counting sorting operations
    By Cpro in forum C++ Programming
    Replies: 19
    Last Post: 01-25-2008, 10:46 AM
  4. counting and output, with arrays and functions
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 12-05-2005, 12:46 AM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM