Thread: Need a lesson on input and output data

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Need a lesson on input and output data

    I need a lesson on HOW TO read input data, write input data, and write to the output file. Can anyone explain this to me? I ended up missing class yesterday because I was sick. I just need to know how to do it so I can do my homework. Any help would be greatly appreciated.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    check the tutorials. They are there exactly for that. http://www.cprogramming.com/
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by Mario F. View Post
    check the tutorials. They are there exactly for that. http://www.cprogramming.com/
    I read the tutorial and I figured out how to at least create the input file and the output file. My problem, however, is how to pull just certain information from the file. For example, I have 3 categories that are characters, then I have a float which represents money, then I have integers which represents a number of people. When I set it up in my file, it is set up like this:

    character, float, int
    character, float, int
    character, float, int

    How do I pull just the integers from each line?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dispatch4599 View Post
    character, float, int

    How do I pull just the integers from each line?
    1. You read line into string buffer (using getline for example)
    2. You parse the buffer, till you find what you need, ignoring the rest (using stringstream for example)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Quote Originally Posted by vart View Post
    1. You read line into string buffer (using getline for example)
    2. You parse the buffer, till you find what you need, ignoring the rest (using stringstream for example)
    This is my problem, I don't understand how to do that. I'm just a beginner. In fact, this was only my 4th class, which I missed because of being ill. My book is almost worthless to me as everything it uses as examples uses strings. I need to take these integers and average them so I don't think strings are an option.
    Last edited by dispatch4599; 02-05-2008 at 11:09 PM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You could start by writing at least SOME code like
    Code:
    #include <string>
    int main()
    {
       std::string input;
       // here I will read string from file into input
    
      // for now I will test it with hard-coded string
      input = "A, 10, 1.1";
    
     //And here I will try to extract int from string
    }
    And write what you are doing with comments step by-step
    If on some step - you fail to aachive your goal (compilation error you do not know how to solve, or output you do not expect, or you do not know how to perform a specific task) - post your code and ask specific question
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    coder
    Join Date
    Feb 2008
    Posts
    127
    vart is right:
    you should begin somewhere, then try to get closer to the solution, step by step.

    Quote Originally Posted by dispatch4599
    ... I need to take these integers and average them so I don't think strings are an option.
    http://www.cplusplus.com/reference/c...dlib/atoi.html
    atoi ()
    this c-standard function may help you, since it transforms a string into an integer.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better use strtol instead of atoi.
    When parsing the buffer, strtof might help in getting rid of float values.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    #include <iomanip>
    
    using namespace std;
    
    void main()
    {
    	
    	//Declare and open files
    	ifstream PayinFile;
    	ofstream PayoutFile;
    	PayinFile.open ("C: PayinFile.txt");
    	PayoutFile.open ("C: PayOut.txt");
    	
    	//Declare variables
    	char Cat1,Cat2,Cat3;
    	float Pay1,Pay2,Pay3;
    	int EmpNr1,EmpNr2,EmpNr3;
    	float avgNum;
    
    	//Read in data from file inData
    	PayinFile>> Cat1 >> Pay1 >>EmpNr1
    			>> Cat2 >> Pay2 >> EmpNr2
    			>> Cat3 >> Pay3 >> EmpNr3; 
    
    	//Calculations
    		
    	avgNum = (EmpNr1 + EmpNr2 + EmpNr3)/3;
    
    	PayoutFile << fixed << setprecision(2)
    			<< "The average number of Employees is " << avgNum << " ." << endl;
    
    	PayinFile.close();
    	PayoutFile.close();
    
    }
    Met with the professor today for a few minutes and got this far with the assignment. I am still having the problem, however, the results are not what they are supposed to be. What am I doing wrong???? I'm not looking for a solution to the problem just how do I get to the correct solution. Thank you much.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    PayinFile.open ("C:\\PayinFile.txt");
    Same for the other file.
    No, not void main. Did your professor teach you to use that?
    You should use int main. Void main is undefined. Bad.

    I'm thinking you get bad results due to the layout of your file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My Hat of Guessing thinks that you're actually expecting (EmpNr1 + EmpNr2 + EmpNr3)/3 to give you a floating-point value. If you actually want an accurate answer, you'll need to involve a floating-point number in the problem somewhere; the easiest way to do this is to change "3" to "3.0".

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Yes he taught us to use void main.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Gah! Tell your professor to use "int main" because "void main" is undefined and very bad.
    If he doesn't believe you, tell him to check cboard.cprogramming.com or http://cpwiki.sf.net/Void_main or http://faq.cprogramming.com/cgi-bin/...&id=1043284376 or http://cboard.cprogramming.com/showthread.php?t=96659.

    Just tell him not to teach students to use void main!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Code:
    #include <iostream>
    #include <fstream>
    #include <cmath>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	
    	//Declare and open files
    	ifstream PayinFile;
    	ofstream PayoutFile;
    	PayinFile.open ("C:\\PayinFile.txt");
    	PayoutFile.open ("C:\\PayOut.txt");
    	
    	//Declare variables
    	char Cat1,Cat2,Cat3;
    	float Pay1,Pay2,Pay3;
    	int EmpNr1,EmpNr2,EmpNr3;
    	float avgNum;
    
    	//Read in data from file inData
    	PayinFile>> Cat1 >> Pay1 >>EmpNr1
    			>> Cat2 >> Pay2 >> EmpNr2
    			>> Cat3 >> Pay3 >> EmpNr3; 
    
    	//Calculations
    		
    	avgNum = (EmpNr1 + EmpNr2 + EmpNr3)/3.0;
    
    	PayoutFile << fixed << setprecision(2)
    			<< "The average number of Employees is " << avgNum << " ." << endl;
    
    	PayinFile.close();
    	PayoutFile.close();
    	return 0;
    }







    The average number of Employees is 572662336.00 .
    This is impossible.


    X 19312.34 34
    Y 26825.12 56
    Z 28562.50 80
    This is my PayinFile.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dispatch4599 View Post
    This is impossible.
    Indeed. In fact, if you run the code you gave on the input file you gave, you get an output of 56.00. So either you did not, in fact, run that code, or (more likely) you did not use that input file. Are you sure your path is correct? You don't check that any of your file opens actually works -- print out what EmpNr1 (etc.) are because you might just be reading from nowhere.

    And edit to note: 56.00 isn't the right answer either; see my other post.

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. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM