Thread: file out put

  1. #1
    Registered User
    Join Date
    Jun 2005

    Post file out put

    ok im using the tutoral example and im tying to change it so i can get more tan justthis as an output and i want o make it like an array so i can use a cin to prompt for a number and have it go to that part of the string and output the charecter in that position

    #include <fstream>
    #include <iostream>
    using namespace std;
    int quarry;
    int main()
      char str[10];
      ofstream a_file ( "example.txt" );
      a_file<<"This text will now be inside of example.txt";
      ifstream b_file ( "example.txt" );
      b_file>> str;
      for(int x = 15;x != 0;x--)
              cin>> quarry;
              cout<< str[quarry]<<endl;

  2. #2
    Registered User
    Join Date
    Jun 2004

  3. #3
    Registered User
    Join Date
    Mar 2002
    What part of the program you posted isn't working as you want it to? If user input into quarry is an int less than 0 or greater than 4 you will get a bounds error problem with the following line:

    cout<< str[quarry]<<endl;

    since the word This will be read into str by the call to >> using b_file in the following line.

    b_file>> str;

    and valid indices str will only be 0 to 4. But other than that, the code looks legal and I would expect it to compile.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM