Help simple string program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help simple string program

  1. #1
    Join Date
    Nov 2013
    Posts
    11

    Help simple string program

    I'm trying to write a simple console app, it reads input from two files, does some math with them, and outputs the results into a new file.

    The files have data that looks like this:

    5.96735 0.250075
    8.0087 0.249728
    9.97946 0.249728

    5.96735 0.167579
    8.0087 0.167579
    9.97946 0.200412

    The output I want will look like this:

    0.250075 4.25743

    Here's the program I've written which compiles, but when I try to use it, it crashes after inputting the filenames. It does generate an output file, but with only a couple lines. The lines appear to be from the end of the files.

    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    using namespace std;
    
    string arr[1];
    string arr2[1];
    double fast;
    double j;
    double value_from_4;
    string firstfile;
    
    double string_to_double( const std::string& s )
     {
       istringstream i(s);
       double x;
       if (!(i >> x))
         return 0;
       return x;
     }
    
    int main () {
    
      string filename;
      string filename2;
      string line;
      string line2;  
      ofstream outputfile ("output.txt", std::ios::out | std::ios::app);
    
      cout << "enter filename of normal data:" << endl;
      cin >> filename;
      cout << "enter filename of problem data:" << endl;
      cin >> filename2;
    
      ifstream myfile (filename);
      ifstream myfile2 (filename2);
      if (myfile.is_open())
      {
        	
    		while ( getline (myfile,line) ) {
            firstfile = line;
    
    		}
         	
      }
    
      else  {	  
    	  cout << "Unable to open file"; 
    	    }
    
      myfile.close();
    
    
      if (myfile2.is_open())
      {
    	  while ( getline (myfile2,line2) )
    	  {
    			int i = 0;
    			stringstream ssin(firstfile);
    
    			while (ssin.good() && i < 2) {
    				ssin >> arr[i];
    				++i;
    			}
    
    			int k = 0;
    			stringstream ssin2(line2);
    
    			while (ssin2.good() && k < 2) {
    			ssin >> arr2[k];
    			++k;
    			}
    
    			j = (25.4 * string_to_double(arr[1])) / 400;
    			fast = (25.4 * string_to_double(arr2[1])) / j;
    			value_from_4 = 0.01 * fast;
    
    			outputfile << arr[1] << " " << value_from_4 << std::endl;
    	  }
    	 
      }
      
      else {
    	  
    	  cout << "Unable to open file"; 
    
      }
      		
    	myfile2.close();
    	outputfile.close();
    	
      return 0;
    }
    I'll probably figure it out eventually just stressed out at the moment and can't think straight. Any help would be greatly appreciated.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,464

    Re: Help simple string program

    First, what debugging of the proram have you done using the debugger to find out where your code deviates from what you would expect from your design.

    You only ever process the last line of 'normal data' as your program first reads all the lines from this file then closes the file.

    Assuming each line of 'normal' pairs with a line of 'problem' then your calculations seems to suggest that the output for the first line would be

    0.250075 2.68046.

    Perhaps if you explain exactly what you are trying to achieve, we might be able to advise better.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Nov 2013
    Posts
    11

    Re: Help simple string program

    Quote Originally Posted by 2kaud View Post
    First, what debugging of the proram have you done using the debugger to find out where your code deviates from what you would expect from your design.
    I tried to run the MSVC debugger, but it just exists after inputting the filenames and gives me no information. I'm not familiar with debuggers so I was looking at some GDB commands but I'm still not sure how to use it yet.

    You only ever process the last line of 'normal data' as your program first reads all the lines from this file then closes the file.
    Ugh. I was trying to have it read each line, and save them into a large array. I tried to do something like string firstfile[700] and have a for loop just iterate each line into it, but "line" wouldn't assign to firstfile if it was an array. Also stringstream wanted nothing to do with it. I tried making 'firstfile' a string stream but that didn't work either. I just left it as a string since it compiled, but now it crashes. Probably because it's trying to shove a whole file into a single string.

    How am I supposed to give access to the numbers in file1 to the while file2.open other than saving them in some sort of variable though?

    Assuming each line of 'normal' pairs with a line of 'problem' then your calculations seems to suggest that the output for the first line would be

    0.250075 2.68046.
    Yep. The first line would be 0.250075 2.68045986204

    (first line of normal data file)
    5.96735 0.250075

    (first line of problem data file)
    5.96735 0.167579

    j = (25.4 * string_to_double(arr[1])) / 400;
    fast = (25.4 * string_to_double(arr2[1])) / j;
    value_from_4 = 0.01 * fast;

    j = (25.4 * 0.250075) / 400
    fast = (4.2565066) / 0.0158797625

    fast = 268.045986204
    value_from_4 = 0.01 * 268.045986204
    value_from_4 = 2.68045986204

    Perhaps if you explain exactly what you are trying to achieve, we might be able to advise better.
    I'm trying to read in two data files.

    The files have two numbers on each line, and many, many lines (600-1000+). Let's say the numbers are x and y.

    I want to take the y variables from each line, do some math on them to get z, and then output a new file of the format:

    y z

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,464

    Re: Help simple string program

    Try this. Note that your formula can be simplied! Also, it is assumed that one line of normal matches the corresponding line of prob. No checks are done for this.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const string outname = "output.txt";
    
    int main()
    {
    ofstream outputfile (outname.c_str(), ios::out | ios::app);
    
    	if (!outputfile.is_open()) {
    		cout << "Cannot open output file " << outname << endl;
    		return 1;
    	}
    
    string normname;
    
    	cout << "Enter filename of normal data: ";
    	cin >> normname;
    
    ifstream normfile(normname.c_str());
    
    	if (!normfile.is_open()) {
    		cout << "Cannot open normal file " << normname << endl;
    		return 2;
    	}
    
    string probname;
    
    	cout << "Enter filename of problem data: ";
    	cin >> probname;
    
    ifstream probfile(probname.c_str());
    
    	if (!probfile.is_open()) {
    		cout << "Cannot open problem file " << probname << endl;
    		return 3;
    	}
    
    double	n1, n2,
    	p1, p2;
    
    	while ((normfile >> n1 >> n2) && (probfile >> p1 >> p2))
    		outputfile << n2 << " " << 4.0 * p2 / n2 << endl;
      		
    	normfile.close();
    	probfile.close();
    	outputfile.close();
    	
    	return 0;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Nov 2013
    Posts
    11

    Re: Help simple string program

    Quote Originally Posted by 2kaud View Post
    Try this.
    Awesome, it works! Thanks a bunch! How very elegant. I will study + learn from it.

    Neat use of cin type space skipping + ifstream c.str.

    Note that your formula can be simplied!
    I'm surprised it could be simplified still further! What I had was already the result of much simplification.

    Also, it is assumed that one line of normal matches the corresponding line of prob. No checks are done for this.
    Understood. One of the reasons I had the first value printed was so that I could match these up. The data files have to go through some formatting before they can be used so I needed a way to keep track of each line.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Help simple string program

    Quote Originally Posted by Labyrinth View Post
    I tried to run the MSVC debugger, but it just exists after inputting the filenames and gives me no information.
    Did you hit F10 to single step through the program? Or did you hit F5 to run the program without stopping? You should be hitting F10.

    Second, take the program that you finally have and run it through the debugger. You must learn how to debug your programs -- you can't just fluff off using the debugger now that you have a working program. Take the time to learn it now, i.e. setting breakpoints, single stepping through the program, watching variables, etc.

    Third, GDB is not Visual C++ debugger. They are two totally different debuggers -- one for the gcc compiler (GDB), and the other used for Visual C++. Since you are using Visual C++, it makes no sense learning GDB commands. Also, the compiler that comes with Visual C++ is one of the best in the industry.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center