Writing a variable into a folder

Nov 30, 2016 at 5:06pm
Hi my savios :D

What I´m suposed to archiev wwith this Code is 1st: generate primal numbers, check; 2nd: stop the while Loop with Esc., check; 3rd Save all generated numbersnumbers in a folder, and this is the Point where I´m lost, I looked trough the internet tried a bunch of Things but I can´t help it :( would be cool if someone has an Idea.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <fstream>

using namespace std;

int main()
{
	int i, n, p;
	bool exit = false;
	system ("color 0A");
        
        n=2;		

	while( exit == false )
	{
		if (GetAsyncKeyState(VK_ESCAPE))
        {
            exit = true;
        }
        
		for (i = 2; n % i!= 0; i++);
		if (i == n)
		{
			cout<<n<<"\t";
			if(++i % 10 == 0);
		}
		n++;
	}


Please, I´m really new to this stuff, so include in your answers which librarys you´re using, thanks ppl :D
Nov 30, 2016 at 5:07pm
I know that a lot of librarys are not required for the Code I posted.
Nov 30, 2016 at 5:46pm
I'm not at all sure you have a proper grasp of your assignment: For example, your while-loop isn't very effective for the over-all functioning of your program.

I would suggest that the intent of the while-loop is to control the other parts of your program; i.e.,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

vector< int > Primals;

int number = 0;

while( exit == false )
    {
    /*    Generate primal (primal?) numbers:    */

    number = PrimalFunction();    /*    You have to write this one!    */

    /*    Save the number into a vector:    */

    Primals.push_back( number );

    if( GetAscynKeyState( VK_ESCAPE ) )
        exit = true;

    }    /*    while( exit == false )    */

    /*   The rest of this is just my pseudo code for you to fill in:    */

    /*    Now save your generated numbers into a folder (folder?):    */

    /*    Add code to check for the existence of your directory:    */

    /*    If the folder doesn't exist, create it:    */

    /*    Now create an output file in the directory:    */

    /*    Write the created numbers to the output file:    */

    vector< int >::iterator itr;

    for( itr = Primals.begin(); itr != Primals.end(); ++itr )
        {
        Output << *itr << endl;

        }    /* for( itr != Primals.end() )    */


Last edited on Nov 30, 2016 at 5:47pm
Nov 30, 2016 at 7:41pm
Now save your generated numbers into a folder (folder?):

That is the Problem, I don´t know how
Nov 30, 2016 at 8:08pm
Look at the functions that deal with directories; On what operating system are you writing?
Nov 30, 2016 at 10:13pm
atm. win32,

thanks first,
for what in my code should I replace with your code?

What I just noticed, may we have a lil´miss understanding, if you run my Code, the Screen will fill with a lot of numbers, I want to get them all into the folder, not only a single one, don´t ask me why^^ I don´t know either :D but the teacher want what he want and explains nothing^^
Dec 1, 2016 at 2:31pm
You will have to post the rest of your code - mind reading costs extra ;)
Dec 1, 2016 at 6:56pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <fstream>

using namespace std;

int main()
{
	int i, n, p;
	char seguir;
	bool exit = false;
	system ("color 0A");
	FILE* fichero;
	
	salto1:
	
	printf("\n %cQuiere generar n%cmeros primos?(s/n): ", 168, 163);
	scanf("%c", &seguir);
	switch(seguir)
	{
		case 's': printf("\n ");
				  break;
		case 'n': printf("\n Hasta luego. Pulsa ENTER ");
				  getch();
				  return 0;
		default: printf("\n Introduzca s o n. ");
				 goto salto1;
	}
	
	n=2;
	
	ofstream outfile ("/Users/Denix/Documents/numeros primos");		

	while( exit == false )
	{
		if (GetAsyncKeyState(VK_ESCAPE))
        {
            exit = true;
        }
        
		for (i = 2; n % i!= 0; i++);
		if (i == n)
		{
			cout<<n<<"\t";
			if(++i % 10 == 0);
		}
		n++;
	}
	outfile.close();
	
	cout << "\n" << endl;
}

this is all
Dec 5, 2016 at 4:18pm
If you have coded the earlier things I suggested, this part would be trivial. Since it looks like you are writing on Unix, look at the system functions opendir(), readdir() etc (man directory(3C). That will get you your directory (folder) creating code.

To write to a file, there are many examples out there.
Dec 8, 2016 at 3:14pm
I know there are many ways, but how do I write into the file every time "i" gets a new value?
Dec 8, 2016 at 5:13pm
See this link: http://www.cplusplus.com/reference/iolibrary/ and pick an output method.
Topic archived. No new replies allowed.