Thread: i/o program

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    7

    i/o program

    I am currently working on a program where the user types in a student's name and it displays his information. The information about the student is saved into a text file.

    Is it possible for the program take the name the user entered and add ".txt" to it?

    For example:

    "Please enter the student's name:"
    The user types "aaron"
    The program would then look for a file called "aaron.txt" and display the information inside

    -Thanks

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Yes, this is possible. Read up on the std::string data type.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Read up on the std::string data type.
    Okay...to add a little more substance, using the std::string type, you can concatenate ".txt" onto the end of a string after reading the name:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string name;
    
      std::cout<<"Enter a name: ";
      std::getline ( std::cin, name );
    
      name += ".txt";
    
      std::ifstream in ( name.c_str() );
    
      if ( !in )
        std::cerr<<"File cannot be opened\n";
      else {
        //...
      }
    }
    Alternatively, because many students are generally not allowed to use the standard C++ classes to make life easier, a C based solution is also possible, though more error prone:
    Code:
    #include <cstring>
    #include <fstream>
    #include <iostream>
    
    int main()
    {
      char name[100] = {0};
    
      std::cout<<"Enter a name: ";
      std::cin.getline ( name, sizeof name );
    
      std::strcat ( name, ".txt" );
    
      std::ifstream in ( name );
    
      if ( !in )
        std::cerr<<"File cannot be opened\n";
      else {
        //...
      }
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Yes, this is possible. Read up on the std::string data type.

    Thanks for taking time to help.

    I'm able to get the name and add text with the following piece of code

    Code:
      string name = "";
         cout << "Name:";
        cin >> name;
        name += ".txt";
    However, I tried using this to make it

    Code:
    ofstream a_file (name);
      a_file<<name<<"\n";
      a_file<<age<<"\n";
      a_file<<program<<"\n";
      a_file.close();
    And it generates an error. Any ideas?

    -Thanks
    Last edited by jedo; 07-17-2005 at 12:58 PM.

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    ofstream's constructor expects a pointer to a raw array of characters, not a std::string object. Use

    Code:
    ofstream a_file (name.c_str());
    The c_str() member function returns the "C-style string" that is expected.

  6. #6
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    What is the error? It will be eaiser to help you if you say what it is.

    If the error your getting is the same as the one i'm getting, here is the problem.

    ofstream will not take a standard string as its input, and instead needs a char array.

    do
    Code:
    ofstream a_file (name.c_str());
    Sorry about that, looks like the solution was already posted while I was typing/testing mine.
    Last edited by CrazyNorman; 07-17-2005 at 01:16 PM. Reason: already posted

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Ok guys,

    The ofstream a_file (name.c_str()); worked out and now it works fine.

    Now onto when the user looks-up a student. The code looks like this

    Code:
     ifstream b_file ( name.c_str() );
    But I get an error that says:

    59 C:\Dev-Cpp\main.cpp `name' undeclared (first use this function)
    Does this have to do with the fact that one is in the funtcion add() and one is in find() ?

  8. #8
    Amazingly beautiful user.
    Join Date
    Jul 2005
    Location
    If you knew I'd have to kill you
    Posts
    254
    Yes, if a variable is declared in one function, it cannot be accessed by another function, unless passed as a parameter. BTW, any chance you can post your file?

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    7
    Yes, I fixed it.

    Thank you guys so much, everything is working fine now.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. My I/O Program crashes, I need some help
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 01:16 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM