Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ Text Out Put?


  • Please log in to reply
8 replies to this topic

#1 haun

haun

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:11:59 PM

Posted 04 January 2009 - 10:30 PM

Ok so i am working on the same programs but this time it just has to output to a txt file.

I got it to do that, but when it loops (i have it loop 10 times) it just overites the original arrays of mumbers.

Now i have been looking at my C++ for Dummies book (where i learned how to do that, skipping a few chapters) and it says that:

ofstream::ofstream(char *pszFileName, int mode = iso::app, int prot = filebuff::openrot)

is the constructer for that type of input mode. But when i place that in my code te debugger returns:

`ofstream' is not a member of `std::ofstream'
expected primary-expression before "char"
expected primary-expression before "int"
expected primary-expression before "int"

Am i reading this wrong? i plan on creating a large file that can be saved and looked over (note, i plan to loop anywhere from 1000 times to 1000000 times [permitting its possible]).

Here is the whole code, the piece in question is commented:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <string.h>

using namespace std;

const int full = 10; //max # of intergers
int next; //used as another portion to the rule
int cnt = 0; //Count
int pre; //Pre number (used to place the humber in the array
int array1[10]; //First line array (10 digets)
int array2[10]; //Second line array (10 digets)
int array3[10]; //third line array (10 digets)

int main(int argc, char *argv[])
{
while(cnt != full)
{
	pre = 1;
	while( pre <= 10 ) //ends loop when arrays are full
	{
			  next = ((rand() % full + 2) % -full)+1; //Next creator for array1 
					array1[pre] = (rand() % full + 2) % -full; //Array1 creator
			  next = (rand() % full + 2) % -full; //Next creator for array2
					array2[pre] = (rand() % full + 2) % -full; //Array2 Creator
			  next = (rand() % full + 2) % -full; //Next creator for array3
					array3[pre] = (rand() % full + 2) % -full; //Array3 Creator
			  pre = pre+1; //Moves pre one over 1 (places the next number one over)
   }
   //ofstream::ofstream(char *pszFileName, int mode = iso::app, int prot = filebuff::openrot); //Doesnt work 
   ofstream UM("Out.txt");
UM << array1[1] << " " << array1[2] << " " << array1[3] << " " << array1[4] << " " << array1[5] << " " << array1[6] << " " << array1[7] << " " << array1[8] << " " << array1[9] << " " << array1[10] << '\n'
   << array2[1] << " " << array2[2] << " " << array2[3] << " " << array2[4] << " " << array2[5] << " " << array2[6] << " " << array2[7] << " " << array2[8] << " " << array2[9] << " " << array2[10] << '\n'
   << array3[1] << " " << array3[2] << " " << array3[3] << " " << array3[4] << " " << array3[5] << " " << array3[6] << " " << array3[7] << " " << array3[8] << " " << array3[9] << " " << array3[10];
cnt = cnt+1;
}
	return 0;
}
Thanks!

Edited by haun, 04 January 2009 - 10:31 PM.

Annnnd you're, asking, me...

BC AdBot (Login to Remove)

 


#2 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:11:59 PM

Posted 05 January 2009 - 12:44 AM

OOOPS!!!! i just figured it out!!!

Its Ios not iso.

Hahahaha

NOW it works!!!!!

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <fstream>




using namespace std;

const int full = 10; //max # of intergers
int next; //used as another portion to the rule
int cnt = 0; //Count
int pre; //Pre number (used to place the humber in the array
int array1[10]; //First line array (10 digets)
int array2[10]; //Second line array (10 digets)
int array3[10]; //third line array (10 digets)

int main(int argc, char *argv[])
{
while(cnt != 1000) //When it has looped 1000 times (3000 lines) then it will end
{
	pre = 1; //retuns pre to 1 (first step in arrays)
	while( pre <= 10 ) //ends loop when arrays are full
	{
			  next = ((rand() % full + 2) % -full)+1; //Next creator for array1 
					array1[pre] = (rand() % full + 2) % -full; //Array1 creator
			  next = (rand() % full + 2) % -full; //Next creator for array2
					array2[pre] = (rand() % full + 2) % -full; //Array2 Creator
			  next = (rand() % full + 2) % -full; //Next creator for array3
					array3[pre] = (rand() % full + 2) % -full; //Array3 Creator
			  pre = pre+1; //Moves pre one over 1 (places the next number one over)
   }
   ofstream UM("Out.txt", ios::out | ios::app); //Dump arrays onto "Out.txt" at ent of file
UM << array1[1] << " " << array1[2] << " " << array1[3] << " " << array1[4] << " " << array1[5] << " " << array1[6] << " " << array1[7] << " " << array1[8] << " " << array1[9] << " " << array1[10] << '\n' // print array1
   << array2[1] << " " << array2[2] << " " << array2[3] << " " << array2[4] << " " << array2[5] << " " << array2[6] << " " << array2[7] << " " << array2[8] << " " << array2[9] << " " << array2[10] << '\n' //Print array2
   << array3[1] << " " << array3[2] << " " << array3[3] << " " << array3[4] << " " << array3[5] << " " << array3[6] << " " << array3[7] << " " << array3[8] << " " << array3[9] << " " << array3[10] << '\n'; //Print array3
cnt = cnt+1; //at one for count for loop break
}
	return 0;
}

Now i was also was wondering if there was a way to reference each part of the array (array1[1], array1[2], ect) without having to say array1[1], array1[2], ect? That would make it easier for larger arrays or matricies.

Also if you have a good idea for a Rule then post it. Good ideas are always welcome!
Annnnd you're, asking, me...

#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:59 PM

Posted 05 January 2009 - 07:02 AM

Yes. You use a for loop like I showed sausage here.

#4 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:11:59 PM

Posted 05 January 2009 - 11:26 AM

Why for()? while works just as fine.
Annnnd you're, asking, me...

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:59 PM

Posted 05 January 2009 - 11:54 AM

sigh..the for loop is not meant to replace the while loop (did you look at the example). It is meant to be used as a string builder so that you didn't have to do what you are doing now. That is what you asked, isn't it?

Additionally, you should consider not hard coding the array sizes into the array constructors. What happens if you want to use a bigger arrays? You would have to make multiple changes. Since you already have a variable holding the size, you should use any place that you used '10' in the code. For example:
int array3[full];
Yeah, on a small program, no big deal.However, it is a good habit to get into for sometime that you decide to create more complex programs.

EDIT: Just FYI, while loops are almost exclusively used when the evaluating condition is a Boolean. For loops are used when the amount of times an operation must execute is known.

#6 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:11:59 PM

Posted 05 January 2009 - 12:25 PM

Ok for the array sizes, how would i out put that? im guessing that id use a for() loop right?
Annnnd you're, asking, me...

#7 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:11:59 PM

Posted 05 January 2009 - 12:34 PM

Tried this:

Edit!!! i Forgot something, still the same errors appear

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <fstream>
using namespace std;
//varibles
const int full = 10; //max # of intergers
const int array_size = 20;
int next; //used as another portion to the rule
int cnt = 0; //Count
int pre; //Pre number (used to place the humber in the array
int array1[array_size]; //First line array (10 digets)
int array2[array_size]; //Second line array (10 digets)
int array3[array_size]; //third line array (10 digets)
const int loop = 1000; //number of loops that will be compleated


//program
int main(int argc, char *argv[])
{
	ofstream UM("Out.txt", ios::out); //Opens Out.txt for clean
	UM << ""; //Cleans it out
while(cnt != loop) //When it has looped, loop number of times will break
{
//array1[1] = (rand() % full + 2) % -full;
	 //retuns pre to 1 (first step in arrays)
	while( pre <= array_size ) //ends loop when arrays are full
	{
			  next = (rand() % full + 2) % -full; //Next creator while array1 [none of these seem to work.
					array1[pre] = (rand() % full + 2) % -full; //Array1 creator
			  next = (rand() % full + 2) % -full; //Next creator while array2
					array2[pre] = (rand() % full + 2) % -full; //Array2 Creator
			  next = (rand() % full + 2) % -full; //Next creator while array3
					array3[pre] = (rand() % full + 2) % -full; //Array3 Creator
			  pre = pre+1; //Moves pre one over 1 (places the next number one over)
   }
   pre = 1; //retuns pre to 1 (first step in arrays), and to place them in the array print
   ofstream UM("Out.txt", ios::out | ios::app); //Dump arrays onto "Out.txt" at ent of fil
for( pre = 1, pre != array_size ) //Print out all array1[] vals without having to referance all, loops when array dump done
{
	 UM << array1[pre]; //Dumps array1[] values at the pre marker
	 pre = pre+1; //moves pre up 1
}
UM << '\n'; //move down line
pre = 1; // reset pre
for( pre = 1, pre != array_size )//Print out all array1[] vals without having to referance all, loops when array dump done
{
	 UM << array2[pre]; //Dumps array1[] values at the pre marker
	 pre = pre+1; //moves pre up 1
}
UM << '\n'; //move down line 
pre = 1; //reset pree
for( pre = 1, pre != array_size )//Print out all array1[] vals without having to referance all, loops when array dump done
{
	 UM << array3[pre]; //Dumps array1[] values at the pre marker
	 pre = pre+1; //moves pre up 1
}

cnt = cnt+1; //at one while count for loop break
}
	return 0;
}

But it didnt like the for()'s!

Heres the debug out:


main.cpp: In function `int main(int, char**)':

main.cpp:40: error: expected `;' before ')' token
main.cpp:45: error: expected `)' before ';' token
main.cpp:46: error: expected `;' before ')' token
main.cpp:51: error: expected `)' before ';' token
main.cpp:52: error: expected `;' before ')' token
main.cpp:58: error: expected primary-expression before '}' token
main.cpp:58: error: expected `)' before '}' token
main.cpp:58: error: expected primary-expression before '}' token
main.cpp:58: error: expected `;' before '}' token


I have no idea whats wrong here. I have never used a for() in my life.

Edited by haun, 05 January 2009 - 12:39 PM.

Annnnd you're, asking, me...

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:59 PM

Posted 05 January 2009 - 03:36 PM

Your for loop looks nothing like the sample I linked you to; Look at the syntax in my example, and then look at the syntax in your example, and then if you still do not understand, then plug the following term into your favorite search engine:
c++ for loop

:thumbsup:

#9 haun

haun
  • Topic Starter

  • Members
  • 156 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Denver, CO
  • Local time:11:59 PM

Posted 05 January 2009 - 05:07 PM

AH YES!!!! Did it. Thanks for reccomending the For() loops, really kills a lot of code!!!

here it is
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <fstream>
using namespace std;
//varibles
const int full = 10; //Range of intergers (note 2=[0,1] 3=[0,1,2] 10=[1-9])
const int array_size = 20; //Defines the number of 
int next; //used as another portion to the rule
int cnt; //Count (used to break loops)
int pre; //Pre number (used to place the humber in the array)
int array1[array_size]; //First line array 
int array2[array_size]; //Second line array 
int array3[array_size]; //Third line array 
const int loop = 1000; //Number of loops that will be compleated (for lines multiply by 3)
//program
int main(int argc, char *argv[])
{
	ofstream UM("Out.txt", ios::out); //Opens "Out.txt" for clean, overwtiting
	UM << ""; //Clears all existing txt
for( cnt = 0; cnt != loop; cnt++) //Return Count to 0; When it has looped, loop number of times break; add one to cnt each loop
{
	for( pre = 0; pre <= array_size; pre++ ) //Return pre to 0; When it has looped, through the array_size break; add one to pre each loop
	{
			  next = (rand() % full + 2) % -full; //Next creator while array1 [none of these seem to work.
					array1[pre] = (rand() % full + 2) % -full; //Array1 creator
			  next = (rand() % full + 2) % -full; //Next creator while array2
					array2[pre] = (rand() % full + 2) % -full; //Array2 Creator
			  next = (rand() % full + 2) % -full; //Next creator while array3
					array3[pre] = (rand() % full + 2) % -full; //Array3 Creator
   }
   ofstream UM("Out.txt", ios::out | ios::app); //Dump arrays onto "Out.txt" at ent of file
for(  pre = 0; pre != array_size; pre++  ) //Reset pre to 0; break when pre is = array_size; add 1 to pre each loop
{
	 UM << array1[pre]; //Dumps array1[] values at the pre marker
}
UM << '\n'; //move down line
for( pre = 0; pre != array_size; pre++ ) //Reset pre to 0; break when pre is = array_size; add 1 to pre each loop
{
	 UM << array2[pre]; //Dumps array2[] values at the pre marker
}
UM << '\n'; //move down line 

for(  pre =0; pre != array_size; pre++  ) //Reset pre to 0; break when pre is = array_size; add 1 to pre each loop
{
	 UM << array3[pre]; //Dumps array3[] values at the pre marker
}
UM << '\n'; //next line
}
	return 0;
}

here is the return of only 10 loops, and 20 integers each array:

90984815017410214081
26368066634025853275
67457433908736007844
66918392360561075891
42902758119388152349
23500132605935490043
75082174115174358621
84311586445986500486
88140791728173774312
07690964425278689894
76197145862665490908
36222733229646971623
82301006794346503206
36192968834174507635
95526222938741747567
70540703939369544460
45472611825185357974
95231270964025261047
29955229018858430324
55103055187781567364
45794028073447843856
50805140317061422912
07151552527670157701
26321316161230063750
88744250010402292248
94539144658491797961
66304876485720931785
26501578171204379217
61287254378531661645
45866062126384637496



Any good ideas for a rule?
Annnnd you're, asking, me...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users