Thread: Strcmp - Reading from File (Can't read?) - Beginner

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    Exclamation Strcmp - Reading from File (Can't read?) - Beginner

    I created a program for fun which its for Halloween. I wasn't able to get it working as comparing strings from text file (its in Choice 2 - if statement). It only returned 0.00 instead of outputting the total running sales of Candy, Decorations, and Costumes. I'm not sure where I went wrong here.

    Doing so by entering sales (choice 1) first which that'll create the file and appending the sales to the file. Then run the program again to enter the menu choice 2 of tallying the sales for the day. As you can see, it only reflect the 0.00 instead of 139.99 (for example).

    So, I'm assuming it can't read from the text file or something. Hope I'm clear... I'm using g++ compiler to run the program.

    Code:
    edit: remove code - personal reason
    Last edited by kryz; 10-16-2009 at 06:02 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You're opening the file for output in the case where you're trying to read.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Ive tried to remove the code down below in choice 2.
    Code:
    dataFile.open("sales.txt", ios::out | ios::app);
    It did the same thing to 0.00. What should it be changed to?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Just use an input stream:

    ifstream datafile;
    datafile.open("sales.txt");

    There is a similar stream for output, named ofstream. fstream is a bidirectional stream, and something that I honestly never had use for.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    Just use an input stream:

    ifstream datafile;
    datafile.open("sales.txt");

    There is a similar stream for output, named ofstream. fstream is a bidirectional stream, and something that I honestly never had use for.
    hmm.. I changed that and now it said no match for 'operator<<' in dataFile' ...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't write to an input file. ios::out --> ofstream.
    Last edited by tabstop; 10-16-2009 at 04:36 PM. Reason: smiley eradication

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Quote Originally Posted by tabstop View Post
    You can't write to an input file. ios::out --> ofstream.
    did change to ofstream dataFile; and to dataFile.open("sales.txt", ios:ut);

    which that created error again I mentioned above:

    119: error: no match for 'operator>>' in 'dataFile >> line'
    121: error: no match for 'operator>>' in 'dataFile >> totalCost'

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    nevermind... I figured it out. Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM