2 Replies - 139 Views - Last Post: 01 March 2018 - 08:29 AM Rate Topic: -----

#1 femrost   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 18

trying to convert string to char* to make it work with ifstream

Posted 28 February 2018 - 08:18 PM

this is from the old APCS marine biology case study for c++. i'm using eclipse and it's not c++11. i've been working on this with my teacher for a day and so i decided to see if i could get someone to at least point me in the right direction.
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include "apstring.h"
#include "environ.h"
#include "display.h"
#include "simulate.h"
#include "stdio.h"
using namespace std;
int main() {
	cout << "do you want to create a new file? if yes press Y if not press N"
			<< endl;
	char ans;
	cin >> ans;
	while (ans != 'Y' && ans != 'N') {
		cout << "dummy, put a capital Y or N-->";
		cin >> ans;
	}
	bool x;
	string r;
	ofstream outfile;
	if (ans == 'Y') {

		outfile.open("fish.dat", ios::out);
		int r, c, mc, mr, f;

		cout << "how many rows do you want:";
		cin >> mr;
		cout << endl << "how many columns do you want:";
		cin >> mc;
		cout << endl;
		cout << "how many fish do you want to create:";
		cin >> f;
		cout << endl;
		for (int n = 0; n < f; n++) {
			cout << "for your number " << n + 1 << " fish where do you want its row:";
			cin >> r;
			cout << "and what column:";
			cin >> c;
			outfile << mc << " " << mr << endl;
			outfile << c << " " << r << endl;
		}
		outfile.close();
		x = true;
// if this code runs the program crashes after here
	} else {
		cout << "what file do you want-->";
 // getline doesn't work but program still runs if this code runs.
		getline(cin, r);
		x = false;

	}

	char* file;


	if (!x ) {
		//assign the file to open

	file = new char[r.length()];
	strcpy(file,r.c_str());
	} else{
		r="fish.dat";
		strcpy(file,r.c_str());
	}

		ifstream input;

		input.open(file, ios::in);
		Environment env(input);
		// Display display(100,100); // for graphics display
			Display display; // for text display
			apstring s;

			Simulation sim;

			int step;
			int numSteps;

			display.Show(env);

			cout << "--- initialized --- " << endl;

			cout << "How many steps? ";
			cin >> numSteps;
			getline(cin, s);

			for (step = 0; step < numSteps; step++) {
				sim.Step(env);
				display.Show(env);
				cout << " step " << step << " (press return)";
				getline(cin, s);
			}

return 0;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: trying to convert string to char* to make it work with ifstream

#2 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: trying to convert string to char* to make it work with ifstream

Posted 01 March 2018 - 03:10 AM

Quote

i'm using eclipse and it's not c++11.

What does using Eclipse have to do with using C++11 or not? That IDE is perfectly capable of using a compiler set to the C++11 (or even the current standard C++17).

Quote

i've been working on this with my teacher for a day and so i decided to see if i could get someone to at least point me in the right direction.

My first suggestion is to start using one of the more current standards as a start. Check your IDE documentation to see how to set the standard to be used when compiling your code.

Next, with any C++ standard, you should prefer using std::string instead of the dangerous C-strings. It is very easy to convert C++ strings to C-strings when required.


	char* file;


	if (!x ) {
		//assign the file to open

	file = new char[r.length()];
	strcpy(file,r.c_str());
	} else{
		r="fish.dat";
		strcpy(file,r.c_str());
	}

		ifstream input;

		input.open(file, ios::in);



Horrible, just horrible. Try something more like:

And use meaningful variable and function names, this will help document the code and meaningful names are easier to remember.

	string file_name;

        // What the heck is x?
	if (!x ) {
		//assign the file to open

        // What the heck is r?

	//file = new char[r.length()];
	//strcpy(file,r.c_str());
        file_name = r;
	} else{
		r = "fish.dat";
		//strcpy(file,r.c_str());
	}
                // Just convert to a C-string when actually required, better yet use a C++11 or higher standard.
		ifstream input(file_name.c_str());



Avoid, like the plague, using C-strings and the associated C-string functions, prefer C++ strings!


Jim
Was This Post Helpful? 1
  • +
  • -

#3 femrost   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 18

Re: trying to convert string to char* to make it work with ifstream

Posted 01 March 2018 - 08:29 AM

thank you i was concerned about if r or file_name were modified or deleted then file_.cstr() wouldn't work, but it works within the boundaries of this project.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1