file out put

This is a discussion on file out put within the C++ Programming forums, part of the General Programming Boards category; ok im using the tutoral example and im tying to change it so i can get more tan justthis as ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    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

    Code:
    #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";
      a_file.close();
      ifstream b_file ( "example.txt" );
      b_file>> str;
      for(int x = 15;x != 0;x--)
      {
              cin>> quarry;
              cout<< str[quarry]<<endl;
      }
      cin.get();
    }

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    a_file.open()

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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, 08:35 AM
  3. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 04: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, 01:58 AM

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