problem with string class, ifstream, and getline

This is a discussion on problem with string class, ifstream, and getline within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm new to c++ (have a little VB .net) and strings are much different. I am trying to read ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    problem with string class, ifstream, and getline

    Hi, i'm new to c++ (have a little VB.net) and strings are much different.
    I am trying to read strings and numbers from a file, and am not having much success. specifically, i am trying to read one string and 3 different numbers into the appropriate members of a structure, and create an array of these structures, then display the array on screen.
    I am able to read the first string and 3 numbers fine, but then the whole thing falls apart, and i cannot understand why.
    As far as i can tell, it should work perfectly. Please help if you can. What i would like is an explanation of why my code fails, not just what changes i can make to fix it (but that'd do in a pinch).

    Code:
    #include <cstdlib>
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    char ch;
    ifstream fin;
    int flag = 2;
    int i = 0;
    struct share
           {
           string shareName;
           int noShares;
           float purchasePrice;
           float currentValue;
           };
    share portfolio[20];
    while (flag > 1)
           {
           cout<<"     MENU     \n"<<"Press L to Load \n"<<"Press D to Display\n"<<"Press Q to Quit\n";
           cin >> ch;
           int holder = ch;
    switch (holder)
           {
        case 76: fin.open("funds.txt");
        while (i < 8)
        {
    
         getline(fin, portfolio[i].shareName,'\n');
         fin>>portfolio[i].noShares;
         fin>>portfolio[i].purchasePrice;
         fin>>portfolio[i].currentValue;         
         ++i;
         }    
                 flag = 3;
                 break;
        case 68: if (flag != 3)
              {
                 cout<<"\nNo Data Loaded\n\n";
                 break;
              }
                 else if (flag ==3)
                {
                 i = 0; 
                 while (i<8)
                {
                    cout<<endl<<portfolio[i].shareName<<endl;
                    cout<<portfolio[i].noShares<<endl<<portfolio[i].purchasePrice;
                    cout<<endl<<portfolio[i].currentValue<<endl;
                 ++i;
                 }              
                } 
                 break;
        case 81: flag = 0;
                 break;
                 }
                 }
                 return 0;
    }
    here is the contents of the file i am trying to read (funds.txt).

    Sweet Money
    10
    2
    4
    Argonauts
    15
    4
    5
    Wiggles
    60
    1
    5
    Human Gills
    100
    .5
    333.33
    Brain Implants
    25
    4654.4
    456466.45
    Clone Dawgs
    66
    987
    789
    Oil Gas Co.
    100
    40
    61
    College Football
    100
    34
    519

    thanks again.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    cin >> ch;
           int holder = ch;
    switch (holder)
    Why not just make that
    Code:
    cin >> ch;
    switch(ch)
    What's the output? Does it work with only 3 lines?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    An appropriately placed fin.get(); will remove the newline from the input (after the last numeric input).

    And here:
    Code:
        case 68:
        case 81:
    Say what you mean and don't interpret too much.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    I think deathbob was using numbers because the value in the switch was a number.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    So are the values of 'L', 'D', and 'Q'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    I know, that's what deathbob should have used. But he didn't, and now we have to speculate on why not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    thank you so much,
    fin.get();
    solved it. and i changed it to switch (ch) also, i had been trying to use that before, but had put in things like
    case ("L")
    and it wasn't working. i left the case statements with the numbers and now it works perfectly. So what was happening? the getline(fin, portfolio[i].shareName,\n) was grabbing the newline character and that was what was messing everything up?
    Thanks again

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A newline is not a valid part of a number, so it is left in the stream. It is valid in a string, however.

    'L', 'D', and 'Q' are different from "L", "D", and "Q".
    Last edited by Dave_Sinkula; 09-17-2005 at 07:11 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    The newline (or whichever delimiter you pass to getline) is discarded, it is not added to the string you read in.

    getline and operator>> just work differently. The getline function is intended to read one section (usually a line) at a time from the input stream when each section is separated by a delimiter. The delimiter is not part of each section, so you don't add it to the string, but you don't want to add extra code to skip it so it is discarded. When you use operator>>, you are generally reading in only a single piece of data that is not necessarily separated by the next piece of data by a delimiter. That is why operator>> just reads until it can fill the variable and then stops. By default it does skip over whitespace, so you never notice that it is leaving the spaces or newlines or tabs that separate the data. It only causes a problem when you use getline immediately after operator>>.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    To fix that problem, I think you can use fin.getline().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. ifstream problem
    By Yuri in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2005, 02:55 PM
  3. File I/O Issue
    By The Brain in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2005, 12:21 PM
  4. getline problem
    By luckyboy23 in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2003, 05:40 AM
  5. Think I have a getline problem....
    By sunburnbyRA in forum C++ Programming
    Replies: 8
    Last Post: 04-08-2003, 03:44 PM

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