Thread: structs and doubles?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    structs and doubles?

    I'm writing this program that reads a text file which lists an item, and then it's price, and on and on...

    Item
    5.67
    Item
    0.50
    This is part of the code... the problem I'm having is reading the file. With this code, I can read the first item, and it's price, but after that I get nothing for the next item, and an exponential number for the prices. menuPrice has to be double, so I can't read strings and then atoi() it.

    Code:
    struct menuItemType
    {
    string menuItem;
    double menuPrice;
    };
    
    const int ITEMS_ON_MENU = 8;
    
    void getData(menuItemType type[]);
    
    int main()
    {
    
    menuItemType menuList[ITEMS_ON_MENU];
    
    getData(menuList);
    
    
    return 0;
    
    }
    void getData(menuItemType type[])
    {
    
    ifstream readFile;
    readFile.open("Ch11_Ex3Data.txt");
    
    int i = 0;
    
    for (i = 0; i < ITEMS_ON_MENU; i++)
    {
    cout << endl << i << endl;
    getline(readFile, type[i].menuItem, '\n');
    readFile >> type[i].menuPrice;
    cout << type[i].menuItem << endl << type[i].menuPrice;
    }
    
    readFile.close();
    
    }
    is the getline/readFile combo some how throwing off which line is being read in the file?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes. The >> operator will leave the \n character in the input stream, meaning the next getline reads nothing.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    Quote Originally Posted by tabstop View Post
    Yes. The >> operator will leave the \n character in the input stream, meaning the next getline reads nothing.
    Thanks for the tip.

    readFile >> type[i].menuPrice >> ws;
    It's working fine with that.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of creating a dummy variable to ignore whitespace, you could just write:
    Code:
    readFile >> type[i].menuPrice;
    readFile.ignore(1, '\n');
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Instead of creating a dummy variable to ignore whitespace, you could just write:
    Code:
    readFile >> type[i].menuPrice;
    readFile.ignore(1, '\n');
    edit:

    WS isn't a variable, it's a stream manipulator. They both work, but now whenever I try to output the variables, i get a list of 8 items, and then when outputting the price, they overwrite whatever is already on the screen:

    1.45n Egg
    2.45n and Egg
    0.99in
    1.99ch Toast
    2.49t Basket
    0.69al
    0.5fee
    0.75
    How do I reset ... whatever it is I need to reset? It's supposed to be in two separate columns, but it doesn't seem to want to obey! :/
    Last edited by misterFry; 11-22-2008 at 04:54 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by misterFry
    WS isn't a variable, it's a stream manipulator.
    hmm... you are right, it is the one and only standard std::basic_istream manipulator, apparently.

    Quote Originally Posted by misterFry
    They both work, but now whenever I try to output the variables, i get a list of 8 items, and then when outputting the price, they overwrite whatever is already on the screen:
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    Quote Originally Posted by laserlight View Post
    hmm... you are right, it is the one and only standard std::basic_istream manipulator, apparently.

    What is your current code?
    I've solved the problem. In another program (the final for the course) where I was doing the same 'getline()', I discovered that the length of separate strings (getline vs. cin[ifstream]) were different. One was one more character than the other. getline() was 1 longer, apparently because of a null character (or something, I'm not even sure). Do you have any idea what could have caused it?

    I had to use this to get rid of the suspect final character, for the regular assignment, on the end:

    Code:
    type[i].menuItem.erase(type[i].menuItem.length() -1, 1)
    this was the function that included the above: (menuItem is string, menuPrice is double)

    Code:
    void getData(menuItemType type[])
    {
    
    ifstream readFile;
    readFile.open("Ch11_Ex3Data.txt");
    
    int i = 0;
    
    	for (i = 0; i < ITEMS_ON_MENU; i++)
    	{
    	getline(readFile, type[i].menuItem, '\n');
    	type[i].menuItem.erase(type[i].menuItem.length() -1, 1); 
    	readFile >> type[i].menuPrice;
    	readFile.ignore(1, '\n');
    	}
    readFile.close();
    
    }
    I can see that this characteristic (overwriting a line) might come in handy, but would also like to know exactly how to implement it. So far, I have no clue.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by misterFry
    One was one more character than the other. getline() was 1 longer, apparently because of a null character (or something, I'm not even sure). Do you have any idea what could have caused it?
    You could put a breakpoint there and examine what is in the string with your debugger, or you could cast that last char to int and print it out. On a hunch, it might be '\r' instead of '\0'. If my hunch is correct, then you should not have such a problem if you use getline(readFile, type[i].menuItem) instead since that version of std::getline() should translate '\n' appropriately.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random behaviour structs please explain
    By dezz101 in forum C Programming
    Replies: 1
    Last Post: 09-12-2008, 07:07 PM
  2. better way to scanf of a array of structs member?
    By stabu in forum C Programming
    Replies: 3
    Last Post: 07-17-2008, 04:51 PM
  3. Where do Unused function returns go?
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2006, 10:54 AM