Thread: how to get proper inputs

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    7

    how to get proper inputs

    i have lately been running into lots of problems where i have a "string" and the items needs a "char" or vise versa, i have been trying to stay to "strings". I also think it has to do with the calls im using are old calls using the "char".
    any help on how to open the file correctly with what i am using or if im using the wrong call to open the file a replacement
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string fileName;
    	cout << "Please enter file to open:";
    	cin >> fileName;
    
    	ifstream fin;
    	string szLine = "";
    	fin.open(fileName);			// this is where my problem stems from
    	if (fin.fail())
    	{	
    		cout << " could not find filename\n";
    		return -1;
    	}
    	while (getline(fin, szLine))
    		cout << szLine;
    	
    	fin.close();
    	
    
    	system("pause");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    You can always use fileName.c_str(). It will return a const char*, I believe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. biggest proper problem
    By yogibear1 in forum C Programming
    Replies: 0
    Last Post: 12-16-2009, 02:39 PM
  2. How do I validate these inputs?
    By Kyeong in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 02:20 PM
  3. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  4. Taking inputs at Command Line
    By Stephenf in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2005, 02:33 AM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM