Thursday, July 9, 2009

Need help with c++ programming please?

I am trying to bring in numbers from a file and it is inputting just a random series of numbers. Does anyone know what I need to do to get it to read my file


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





using namespace std;





int main()


{


int numbers[8]; //array of 8 items from input file


int counter;


int num0, num1, num2, num3, num4, num5, num6, num7; //variables of numbers in file


ifstream inData;


ofstream outData;


inData.open("c:\\week8.txt"); //data from file


outData.open("c:\\week8out.txt");





cout %26lt;%26lt; "The numbers from the file are: ";


cout %26lt;%26lt; numbers[8] %26lt;%26lt; endl; //display numbers from file


cout %26lt;%26lt; endl;





for (counter = 0; counter %26lt; 8; counter++) //loop to process list


cout %26lt;%26lt; numbers[8];





cout %26lt;%26lt; endl;


cout %26lt;%26lt; "The numbers in reverse order are: ";





for (counter = 7; counter %26gt;= 0; counter--) //loop for reading numbers in reverse order


cout %26lt;%26lt; numbers[counter] %26lt;%26lt; " ";


cout %26lt;%26lt; endl;





inData.close(); //close file


outData.close();





return 0;

Need help with c++ programming please?
You need to read the numbers from the file you opened. Try something like this. Also, unless you need to output the values to a file also, there is no need for the outData variable:





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





using namespace std;





int main()


{


int numbers[8]; //array of 8 items from input file


int counter;


int num0, num1, num2, num3, num4, num5, num6, num7; //variables of numbers in file


ifstream inData;


ofstream outData;


inData.open("week8.txt"); //data from file


outData.open("week8out.txt");





for(counter=0;counter%26lt;8;counter++)


inData %26gt;%26gt; numbers[i];





...
Reply:first of all





this line





for (counter = 0; counter %26lt; 8; counter++) //loop to process list


cout %26lt;%26lt; numbers[8];





will return value in numbers[8]; 8 times ... no need to use it...





and





i think... ur syntax to read a file is incomplete


and no were ur asigning the values from ur main file





--------------------------------------...





first get the values from the first file put it in variable....( u have not done that)


then put the values of that variable in the other file...








-----------------------





here ur just trying to print the values in number variable...


and no value is asigned to number variable..





hence it is returning a garbage (random) value


------------------------





i have pointed out ur mistake


n i wont be tell u the syntax... try it out urself...





---------------------


take care


bye..


logon to http://digitalbox.co.nr


No comments:

Post a Comment