Short & Sweet for a beginner

This is a discussion on Short & Sweet for a beginner within the C++ Programming forums, part of the General Programming Boards category; Part A of my assignment was to read a file from disk and write all of the integers to the ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Short & Sweet for a beginner

    Part A of my assignment was to read a file from disk and write all of the integers to the output file on one line. The input file was 4 lines long. My integers are all on one line with the following code, however, you don't know where one integer end and the other begins. What am I doing wrong or is there no other way?

    Code:
    #include<iostream>
    
    #include<fstream>
    
    using namespace std;
    
    int main()
    
    {
    
    //Declare and open files
    
    
    ifstream inFile;
    
    ofstream outFile;
    
    
    //Declare variables
    
    char inChar;
    
    
    inFile.open ("K:\\DATFILE1.TXT");
    
    outFile.open ("K:\\ANSWERS.TXT");
    
    
    
    if ( !inFile )
    
    {
    
    cout<< "**Can't open input file**" <<endl;
    
    return 1;
    
    }
    
    
    inFile >> inChar;
    while (inFile)
    
    {
    
    	outFile << inChar;
    	inFile >> inChar;
    
    inFile.close();
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You're reading in characters. That might work if all the integers are one digit, but if not, you'll want to read in integers instead. Change the inChar variable to be an int.

    Then, to put a space or any other character between the integers in the output file, just output a space or any other character. Put the single character in single quotes.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Ok, I tried that and it wouldn't compile..

    I got this error

    fatal error C1075: end of file found before the left brace '{'
    *** Never mind, I figured that part out.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    40
    Got it! Thank you. Now I can move forward with the next 7 steps... UGHHH!! LOL

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,434
    Quote Originally Posted by dispatch4599 View Post
    Ok, I tried that and it wouldn't compile..
    I got this error
    *** Never mind, I figured that part out.
    And do you know why this happens?
    Because you do not indent. You cannot see where the first { begins and where the } should be placed in such a manner that it should terminate the block.
    http://cpwiki.sf.net/Indentation
    See example.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    40

    Talking

    I did indent on the program itself, I was playing around with it on word and copied and pasted from there. Sorry for the confusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 02:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03:48 PM
  3. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM

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