Thread: where or how could i add a string?

  1. #1
    Unregistered
    Guest

    where or how could i add a string?

    I need to add a string in here somewhere...any ideas where or how to do it?

    [CODE]
    #include<fstream.h>
    #include<string.h>
    #include<stdlib.h>

    int main()
    {
    // Declare variables
    char band [80];
    char cd_name [80];
    int found;
    ifstream infile;
    found = 0;

    cout << "Sheila Blakely\n";
    cout << "Final 2002\n\n";
    cout << "Music Library";

    //Input from the user
    cout << "Enter a singer or band name: ";
    cin.get(band,80);
    cin.ignore(80, '\n');
    cout << '\n';

    infile.open ("LIBRARY.TXT", ios::in); // open the library file

    if(infile)
    {
    do
    {
    infile.get(band, 80); // get the band from the file
    infile.ignore(80, '\n');
    infile.get(cd_name, 80); // get the CD name from the file
    infile.ignore (80, '\n');

    if ((strcmp (cd_name, band)) == 0 )
    {
    found = 1;
    break;
    }

    }
    while (found != 1 && !infile.eof());
    }
    else
    {
    "An error occured while opening the file.\n";
    }

    infile.close(); // close the input file

    if (found == 1)
    {
    cout << "The book title that you requested is in our inventory.\n";
    cout << "***CD Information*** \n";
    cout << '\n';
    cout << "CD Title: " << cd_name << '\n'; // print the title of the CD
    cout << "Band/Singer: " << band << '\n'; // print the band or singer name
    }
    else
    {
    cout << "The band or singer you typed in must not be very good\n";
    cout << "we don't waste time with bad music.\n";
    }

    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    change over band and cd_name from char arrays to strings. Declare them like: string band; etc. Then change over your cin.getline statements to just 'cin >>'.

  3. #3
    Unregistered
    Guest
    so if i declare them as strings i dont need the array thing? sorry, im really new at this

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    No problem we were all there. Correct the string library actually goes through all the hassles of dealing with arrays and allocating the memory for them. You really are still using char arrays only you as the programmer no longer need to worry about it.

  5. #5
    Unregistered
    Guest
    now someone i know used this line:

    copies = atoi (string_copies);

    what does the atoi mean?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    317
    Ok, I apologoze my connection crashed when I went to edit my message. To get a word just use 'cin>>'. To get a line use 'getline(cin, StringName);'

    EDIT:

    atoi() function converts a string to a integer.
    atof() function converts a string to a floating point.
    atol() function converts a string to a long.
    Last edited by Traveller; 06-02-2002 at 07:31 PM.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    would it be a good idea to use any conversions in this program?

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    317
    I don't see a reason for it, you are just searching for the band name and/or cd name.

    EDIT:

    The only reason I've used them is if the input could be a number or characters and/or if I wasn't sure what kind of format the user was going to enter at compile time.
    Last edited by Traveller; 06-02-2002 at 07:42 PM.

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    alright, i think this is the last question...when i make my LIBRARY.TXT file, how will it know which one is the band name and which one is the cd name? do i have to do anything in particular for that?

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    317
    Well I would start by creating your file in a particular format, aka singer name or band name then CD name. Then when you search for the first string, skip the second search the third etc. When find a match input the following string as the CD name.

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    so would i need code for that? what would the code be?

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    317
    The easiest way to do that would be to store each string on a seperate line:

    band/singer
    cd name

    Then just use what you have to check for the name and if its not a match then input the next line into a temp string and continue looking. If it is a match then inpu the next line to the cd name string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  4. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM