Thursday, July 9, 2009

C++ Programming...?

I'm not sure what statements to write for the following comments in order to make this program executeable? I have written some, but I'm not sure what to write for the rest.





//1.Declares an input stream variable named inFile





//2.Opens "indata.txt"


inFile.open("indata.txt");








//3. If input file was not opened Prints "Input file not found"


if (inFile.fail())


{


cout %26lt;%26lt; "Could not open file!" %26lt;%26lt; endl;


return 1;


}





//4.Exits the program





//5.Declares a variable named number that holds whole numbers





//6.Reads first value from the file into number





//7.While the end of the file has not been reached





if number is greater than 0





prints the number followed by " is positive"





otherwise if number is less than 0





//8.prints the number followed by " is negative" otherwise prints the number followed by " is zero" reads the next number from the file





//9.Closes the input file


inFile.close();





return 0;

C++ Programming...?
Too easy


;)


Enjoy





- Hex








#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;sstream%26gt;





using namespace std;





int main()


{


string iStreamBuff;


ifstream inFile;


inFile.open("indata.txt");


int Number;





if( inFile.fail() ){


cout %26lt;%26lt; "Failed to open indata.txt";


return 0x00;


exit(1);


}





while( inFile ){


getline(inFile, iStreamBuff);


istringstream buffer(iStreamBuff);


buffer %26gt;%26gt; Number;





if( Number %26gt; 0 ){


cout %26lt;%26lt; Number %26lt;%26lt; " is positive" %26lt;%26lt; endl;


}else if(Number %26lt; 0 ){


cout %26lt;%26lt; Number %26lt;%26lt; " is negative" %26lt;%26lt; endl;


}else{


cout %26lt;%26lt; Number %26lt;%26lt; " is zero" %26lt;%26lt; endl;


}





}


inFile.close();





cout %26lt;%26lt; "\n\nPress any key to exit...";


getch();





return EXIT_SUCCESS;


}


No comments:

Post a Comment