getline??? how do I use something like it for an integer

This is a discussion on getline??? how do I use something like it for an integer within the C++ Programming forums, part of the General Programming Boards category; Code: #include <fstream.h> //header for cout and cin #include <string.h> // header for strings #include <windows.h> // required for system("cls") ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    getline??? how do I use something like it for an integer

    Code:
    #include <fstream.h> //header for cout and cin
    #include <string.h> // header for strings
    #include <windows.h> // required for system("cls")
    
    struct chem
    {
    	char symbol[2];
    	char name[25];
    	int numb;
    	long mass;
    }
    
    main()
    {
    	ifstream infile;
    
    	int i = 0;
    
    	char dummybuffer[25];
    	
    	chem element[112];
    
    	infile.open("CHEM.DAT",ios::in);
    
    	for (i=0; !infile.eof(); i++)
    	{
    		infile.getline(element[i].symbol, 256, ','||'\n');	//get data until new line
    		infile.getline(element[i].name, 256, ','||'\n');	
    		infile >> dummybuffer;
    		element[i].numb = atoi(dummybuffer);
    		infile >> dummybuffer;
    		element[i].mass = atoi(dummybuffer);
    //		infile.getline(element[i].numb, 256, ','||'\n');	
    //		infile.getline(element[i].mass, 256, ','||'\n');	
    
    	}
    
    	infile.close();
    
    	cout << element[0].symbol;
    
    	return 0;
    }
    Those commented out getlines won't work because the struct type is wrong.

    The non commented version would probably work, but I need it to stop as soon as it gets to a comma.

    How might I do this?

    Thanks.
    Last edited by Trauts; 10-30-2002 at 10:41 AM.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Try looking at this, see if these can explkain it:

    http://cboard.cprogramming.com/searc...der=descending


    If not...I'm sure soeone will be happy to show you.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You have a string in a file, then you want to convert it to an int?
    First, read it into a temporary buffer, then use atoi() or strtol() to convert the string to an int.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Read the line to a string, then parse the type from a stringstream.
    This is one suggestion, not compiled, but should work.
    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    #include <theheadersialwaysforget>
    
    class  __get
    {
      bool Success;
    } get;
    template<typename T>
    T operator= (T& value, __get g)
    {
      string s;
      getline(cin,s);
      istringstream sin(s);
      g.Success = sin >> value;
    }
    
    ...
    
    int main()
    {
      //Example of usage:
      int a,b;
      a = get;
      if (get.Success)
      {
        b = get;
      }
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Code:
    #include <fstream.h> //header for cout and cin
    #include <string.h> // header for strings
    #include <windows.h> // required for system("cls")
    
    struct chem
    {
    	char symbol[2];
    	char name[25];
    	int numb;
    	long mass;
    };
    
    main()
    {
    	ifstream infile;
    
    	int i = 0;
    
    	char dummybuffer[25];
    	
    	chem element[112];
    
    	infile.open("CHEM.DAT",ios::in);
    
    	for (i=0; !infile.eof(); i++)
    	{
    		infile.getline(element[i].symbol, 256, ','||'\n');	//get data until new line
    		infile.getline(element[i].name, 256, ','||'\n');	
    		infile.getline(dummybuffer, 256, ','||'\n');	
    		element[i].numb = atoi(dummybuffer);
    		infile.getline(dummybuffer, 256, ','||'\n');	
    		element[i].mass = atoi(dummybuffer);
    	}
    
    	infile.close();
    
    	cout << element[0].symbol;
    
    	return 0;
    }
    That doesn't seem to want to cout anything... any ideas?

    CHEM.DAT looks like this:

    Ne,Neon,11,11.1
    H,Hydrogen,1,1.001

    the numbers there aren't right, I didn't cut and paste it. And it goes in order.
    Last edited by Trauts; 10-30-2002 at 05:10 PM.

  6. #6
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    it works for me...
    oh and a couple of notes
    you might want to shorten these

    Code:
    infile.getline(element[i].symbol, 256, ','||'\n');
    infile.getline(element[i].name, 256, ','||'\n');
    to these

    Code:
    infile.getline(element[i].symbol, 2, ','||'\n');
    infile.getline(element[i].name, 25, ','||'\n');
    in order to avoid buffer overruns...


    and did you ever check if you actually opened the file successfully?
    do something like
    Code:
    if(!infile)
    {
        cout << "Error! could not load CHEM.DAT";
        return 1;
    }
    right after your infile.open statement.


    (edit)
    oh and one other thing... don't you want to use floats instead of ints for your weight variables?
    and use the atof() function I think it is, rather than atoi().
    Cause if you use int you will lose whatever is after the decimal point..
    (/edit)
    Last edited by Cobras2; 10-30-2002 at 05:39 PM.
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>infile.getline(dummybuffer, 256, ','||'\n');
    The last parameter to getline is a char, I don't believe you can OR two together, especially with a logical OR.

    >>char symbol[2];
    If the symbol is 2 characters long, you should have an array sized 3.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >This is one suggestion, not compiled, but should work.
    I hope it doesn't, overloaded assignment operators must be member functions. This is also overcomplicating things just a bit, why not just use a template function which returns the correct value and throws an exception otherwise? One possible implementation would be:
    Code:
    #include <stdexcept>
    #include <iostream>
    #include <sstream>
    #include <string>
    
    template <typename T>
    T string_cast ( std::string& s )
    {
      T value;
    
      std::istringstream sin ( s );
      sin>> value;
    
      if ( !sin )
        throw std::invalid_argument ( "Value must be an integer" );
    
      return value;
    }
    
    int main()
    {
      int a;
      std::string s = "12345";
    
      try {
        a = string_cast<int> ( s );
    
        std::cout<< a <<std::endl;
      }
      catch ( std::invalid_argument& ia ) {
        std::cerr<< ia.what();
      }
    
      std::cin.get();
    }
    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by Cobras2
    (edit)
    oh and one other thing... don't you want to use floats instead of ints for your weight variables?
    and use the atof() function I think it is, rather than atoi().
    Cause if you use int you will lose whatever is after the decimal point..
    (/edit)
    Yeah, I fixed that already, just didn't update the post

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by Prelude
    >-Prelude

    Ok... first of all, I'm NEVER going to use

    using namespace std;

    unless someone explains exactly what it does and how they're using it.
    If I just used your code, then I'd never get any better, I'd still have the same kind of problem

    Second, it DOESN'T work for me, I can't cout it at all.
    Last edited by Trauts; 10-30-2002 at 09:12 PM.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Ok... first of all, I'm NEVER going to use

    using namespace std;

    unless someone explains exactly what it does and how they're using it.
    I'm thoroughly confused. Is this addressed to me? I can't tell because none of my code opens the std namespace completely and I've never recommended it.

    >If I just used your code, then I'd never get any better, I'd still have the same kind of problem
    Once again I'm confused. My reply was to Sang-drax and his suggestion.

    >Second, it DOESN'T work for me, I can't cout it at all.
    What doesn't work? Can you be more specific?

    This is a very incoherent post, you apparently address me and then talk about topics that I haven't introduced or even mentioned in this thread.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    sorry about that... std::istringstream sin ( s ); to me is using namespace std... shows how little I know

    And I didn't know you were replying to sand drax.

    then the part about it not working was for cobra2

    I need to get more sleep.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >sorry about that... std::istringstream sin ( s ); to me is using namespace std... shows how little I know
    Rest assured that if I recommend polluting the global namespace, everyone will expect me to give a very good reason for it.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    All right, I figured out the problem without figuring it out...

    if I use

    infile.getline(dummybuffer, 2, ','||'\n'); //get data until new line
    cout << dummybuffer;

    it couts it just fine.

    But if I use

    infile.getline(element[i].symbol, 2, ','||'\n'); //get data until new line
    cout << element[i].symbol;

    then it won't work... so its something about my struct... any ideas?

    (edit)
    Oh, and by the way

    strcpy(element[0].symbol,"Ne");
    cout << element[0].symbol;

    works just fine. It outputs "Ne"
    Last edited by Trauts; 10-30-2002 at 09:36 PM.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >infile.getline(element[i].symbol, 2, ','||'\n'); //get data until new line

    //Should be:
    infile.getline(element[i].symbol, 3, ','||'\n'); //get data until new line

    And as pointed out earlier:
    > char symbol[2];
    //To store two characters, you need 3 elements (one for the string terminator)
    char symbol[3];

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 11:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM

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