Thread: C++ programming question

  1. #1
    Registered User
    Join Date
    Apr 2007
    Originally from New York

    Question C++ programming question

    I am tring to read contents from a file and pass it into array. Then take that array and put it into another array. Next read those values back out to an oupt stream. My code is below. However, I am getting subscript errors. What is wrong? Also, my input file contains float values.

    int main ()
    	//Declare Variables
    	ifstream inFile;  //input stream
                   ofstream outFile; //Output stream
         int increm=0;    //index for input stream
    	      float array[5];
    	   while (!inFile.eof())
                                    inFile >> array[increm];	          //Read contents of file 
              for(int temp= 0; temp <= array[increm];temp++) of    
    	for(int i=0; i= temp; i++)
    		array[increm] =temp[i];
    		 cout<< temp[i];	
                                    outFile>>	tester.txt;		      				
    	       increm++;                           //increment index	   
         inFile.close();//close output stream
    Last edited by jmartin; 04-23-2007 at 06:29 PM. Reason: left out code

  2. #2
    Registered User
    Join Date
    Apr 2007
    The way i've always done it (both array and linker) is when reading from the file, copy to somewhere else, this was for classes and structs however. The problem with yours is you're making some oversights about how it is reading in.

    When reading from the file:
    -read in the variable (you're doing this i think)
    -skip the '\n', ';', or whatever you have seperating the numbers (remember, it's not going to do that for you, you have to apply the logic that is needed to find the numbers. This would be reverse of the logic used to output the numbers)

    However whenever I do structs or classes (this might be helpful for simpler data types as well!) you want to read in your data into some variables, copy the data to smaller pieces, then change the class or struct data by copying those pieces into it, and then copy the data from this prototype into your linked list or array. This is good for dynamic sizes being that you don't need your new node or component set up until you have everything ready. You might be able to skip the step for having a prototype beforehand, I haven't done it this way, as I was taught the other, and didn't need to experiment at the time.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    You need to format your code properly. The way it is, many people won't even read it.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    C / C++
    Join Date
    Jan 2006
    The Netherlands
    Very strange code to me.

    - Don't use eof, it's bad

    for(int temp= 0; temp <= array[increm];temp++) of
    - What's this?

    array[increm] =temp[i];
    - You store an float into an int, think about what happens? Secondly, an int doesn't have an index []. Temp only exists within the scope of your first for loop.

    outFile>>	tester.txt;
    - What is this supposed to do? Outfile is an ostream so you must use << instead of >>, is tester.txt a variable? No it's a filename what are you trying to do?

    - Is this your entire code? If it is, you have forgotten some things. You should read the tutorials on this (or another) website / book.

    Keep trying!
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM