getting input to work

This is a discussion on getting input to work within the C++ Programming forums, part of the General Programming Boards category; Hi befor you start flaming me about my other one, for some reason it doesnt work at all, not my ...

  1. #1
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    getting input to work

    Hi befor you start flaming me about my other one, for some reason it doesnt work at all, not my fult, but I need help on my code a little. I want it to ask what the file the user want to name the txt file, but I cant get that half to work right for some reason.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
        char infor [1000];
        char name [100];
        
      cout<<"What is the name of the file? ";
      cin.getline (name,100);
      ofstream myfile;
      myfile.open ("name.txt"); //<~~here
      cout<<"Please enter what to save: ";
      cin.getline (infor,1000);
      myfile << infor;
      myfile.close();
      return 0;
    }
    I know I need the input but I never had to put something behind it from my work with input. This is not homework btw, i'm just trying out new codes.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main ()
    {
        char infor [1000];
        char name [100];
        
      cout<<"What is the name of the file? ";
      cin.getline (name,sizeof(name));
      ofstream myfile;
      myfile.open (name); //<~~here
      cout<<"Please enter what to save: ";
      cin.getline (infor,sizeof(infor));
      myfile << infor;
      myfile.close();
      return 0;
    }
    This assumes the file name entered by the user includes the extension. If not and you wish to add the ".txt" part you'll need to strcat that onto the existing string prior to the open call. You should also add code to check that the file gets opened successfully before writing to it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    First of all, it's okay to ask homework questions as long as we're not doing your homework for you. If you provide code of what you've tried before and ask a reasonable question, that's totally fine.

    Your problem is that when you supply "name.txt" as the argument to open(), you're telling it to open a file that is literally called "name.txt". You want it to open a file, the name of which is stored in the name variable. So instead of a string literal, you want to pass the string name as the parameter instead.

    Code:
    myfile.open(name);
    Your user will have to type the .txt in themselves. If you'd like your program to do it for them, you can add it with the strcat() function found in <cstring>. You should know that using arrays of chars is considered the C method for working with strings. If this is what you're learning in your book or class, that's totally fine, but be aware that other C++ users may be using the string class, which can be declared just like any other kind of variable. Ex.:

    Code:
    string my_string = "Some text";

  4. #4
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Oh ok I get it now. No its not for homework, I wish it was b/c I want to learn more from someone then looking at texts from websites that I dont get and cant ask on very much. Thanks alot you 2.

    I'll take a look at strcat(), I never really seen that befor so i'll try to finde out more on it.
    Last edited by adr; 12-29-2005 at 05:50 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you are just learning this on your own, you should really learn the C++ string class and use that for your programs. The strcat function and C style strings (that you were using before) are easier to mess up and less intuitive than the C++ string class. IMO you should look at C++ strings instead of strcat.

  6. #6
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Oh I am, np on that, I was just a little lost on it. I am looking at it right now on cprogramming/tutorial. Thanks tho, like I said its hard to understand sometimes on what I look at. I just learn new stuff that I didnt know so I think I am set on the right track again, WOOT! LOL.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Agreed - the only reason to learn C strings is if you're learning that way in a class, or if you're learning C. Maybe you'd want to learn them just for better understanding of the language, but I'd agree with Daved - you'll find the string class A LOT easier to use.

  8. #8
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Yea I'm trying to learn both some what. That way if I see something in c I can know how to change it into c++ and the other way around. Ok i'll take that up with you all.

  9. #9
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok I try trun my c class to a c++ class, but my code seems to mass up again in the same place><
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    using namespace std;
    
    int main ()
    {
        string name;
        string info;
        string my_string2 = ".txt";
        string my_string3 = name + my_string2;
        
      cout<<"What is the name of the file? ";
      getline(cin,name, '\n');
      ofstream myfile;
      myfile.open (my_string3); //<~~here
      cout<<"Please enter what to save: ";
      getline(cin,info, '\n');
      myfile << info;
      myfile.close();
      return 0;
    }
    Heres the error
    no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >:pen(std::string&)'

    Does the myfile.open change when using c++ class?
    Last edited by adr; 12-29-2005 at 08:48 PM.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
      myfile.open (my_string3.c_str()); //<~~here
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok I got what I was going to askXD I took some odd ideas and put them togather with it. I got it to work now, i'm so happy. LOL thanks again everyone.
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    using namespace std;
    
    int main ()
    {
        string name;
        string info;
        string my_string2 = ".txt";
        string my_string3;
        
      cout<<"What is the name of the file? ";
      getline(cin, name);
      ofstream myfile;
      my_string3 = name+my_string2;
      myfile.open (my_string3.c_str());
      cout<<"Please enter what to save: ";
      getline(cin,info);
      myfile << info;
      myfile.close();
      return 0;
    }
    Last edited by adr; 12-30-2005 at 01:53 AM.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Just so you know, it should be #include <string> instead of #include <cstring> in that code. It probably works for you because <string> is included by <iostream> or <fstream> with your compiler, but that is just luck and you shouldn't count on it, so go ahead and use <string>. The <cstring> header is not necessary here, it is used for character array manipulation methods that you don't need since you are using C++ strings.

  13. #13
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok, I didnt know that. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Determining if input is int, double, or char
    By Lucid003 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2005, 03:16 PM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. Input Problems and C# (CSharp) Tutorials
    By Grayson_Peddie in forum C# Programming
    Replies: 4
    Last Post: 02-27-2003, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21