Jump to content
Forumu Destekleyenlere Katılın ×
Paticik Forumları
2000 lerden beri faal olan, çok şukela bir paylaşım platformuyuz. Hoşgeldiniz.

C++ ifstream, array problemi


aquila

Öne çıkan mesajlar

unutmusum hep c++ filan, salak salak seyleri yapamiyorum, kafayi yicem. simdi dosyadan biseyler okuyup arraya yaziyorum, yazarken cout yapinca normal cikiyo, ama disarida arrayi yazdirdigim zaman cikmiyo, bi bakiverin su koda.

burda dosyadan okuyup yazdiriyorum, normal.

if (!world.fail())
{
world>>row>>column;

while(!world.eof())
{

for (int a = 1; a < row+1; a++)
{
for (int b = 1; b < column+1; b++)
{
world.get(buffer);

//if(buffer == '*')
//{
current_world[a][b] = buffer;
cout<<current_world[a][b];
//}
//else
//{
// current_world[a][b] = 'o';

//}
}
}




}
}


ama sonra bunun disinda, su sekilde yazdirmak istedigimde olmuyo.


for (int x = 0; x <= row+1; x++)
{
for (int y = 0; y <= row+1; y++)
{
cout << current_world[x][y];
}
cout<<'n';
}
Link to comment
Sosyal ağlarda paylaş

alin kodun tamami, lazim olur belki birine, dosyayi duzgun okumuyo ama heralde, anlamadim tam sorununu.

game of life


#include <iostream>
#include <fstream>
using namespace std;
const int CROW = 100, CCOL = 100;

//Checks the neighbors and applies the rules to the cells
void create_generation(char current_world[][CCOL], char next_gen[][CCOL], int row, int column)
{
int neighbors = 0;
for (int m = 1; m <= row; m++)
{
for (int n = 1; n <= column; n++)
{
neighbors = 0;
//Checking neighbors
if (current_world[m-1][n-1] == 'x') neighbors += 1;
if (current_world[m-1][n] == 'x') neighbors += 1;
if (current_world[m-1][n+1] == 'x') neighbors += 1;
if (current_world[m][n-1] == 'x') neighbors += 1;
if (current_world[m][n+1] == 'x') neighbors += 1;
if (current_world[m+1][n-1] == 'x') neighbors += 1;
if (current_world[m+1][n] == 'x') neighbors += 1;
if (current_world[m+1][n+1] == 'x') neighbors += 1;

//Applying rules
if (current_world[m][n] == 'x' && (neighbors == 2 || neighbors == 3))
next_gen[m][n] = 'x';
else if (current_world[m][n] == 'x' && neighbors >= 4)
next_gen[m][n] = 'o';
else if (current_world[m][n] == 'x' && neighbors<= 1)
next_gen[m][n] = 'o';
else if (current_world[m][n] == 'o' && neighbors == 3)
next_gen[m][n] = 'x';
}
}
}

//Swaps the current world with the next generation
void swap(char current_world[][CCOL], char next_gen[][CCOL],int row, int column)
{
//swap
for (int e = 1; e <= row; e++)
{
for (int f = 1; f <= column; f++)
{
current_world[e][f] = next_gen[e][f];
}
}
}


//Prints the new generation
void print(char next_gen[][CCOL], int row, int column)
{
for (int c = 1; c <= row; c++)
{
for (int d = 1; d <= column; d++)
{
cout << next_gen[c][d];
}
cout<<'n';
}
}

int main()
{
int row, column, line=0, generation = 0;
char current_world[CROW][CCOL], next_gen[CROW][CCOL];
char buffer,next;


ifstream world;
world.open("world.txt");

//set everything to dead
for(int i = 0;i < CROW; i++)
{
for(int j = 0;j < CCOL; j++)
{
current_world[i][j] = 'o';
next_gen[i][j] = 'o';
}
}


//open the file and read the lines
if (!world.fail())
{
world>>row>>column;

while(!world.eof())
{

for (int a = 1; a <= row; a++)
{
for (int b = 2; b <= column; b++)
{
world.get(buffer);

//switch '*' with 'x' and empty space with 'o'
if (buffer == 'n')
{
line++;
}

else if(buffer == '*')
{
current_world[a][b] = 'x';

}
else if(buffer == 'null')
{
current_world[a][b] = 'o';

}
}
}
}
}
else cout << "Unable to open file";
world.close();

//Print out the world read from the file
cout<<"This is the starting world"<<'n';
for (int x = 1; x <= row; x++)
{
for (int y = 1; y <= column; y++)
{
cout << current_world[x][y];
}
cout<<'n';
}

cout<<'n';

//create a generation as long as the user wants
do
{
cout<<"Enter n or N to create a new generation"<<'n'<<'n';
cin>>next;
create_generation(current_world, next_gen, row, column);
swap(current_world, next_gen, row, column);
generation++;
cout<<"Generation: "<<generation<<'n';
print(current_world, row, column);
}while(next == 'n' || next == 'N');


return 0;
}

Link to comment
Sosyal ağlarda paylaş

aquila said:

aynen oyle dusundum, yani daha cok soyle.

5 5
*****
* *
** *
****
*

gibi mesela.

vector yapicaktim da, onu da unutmusum, 2d falan filan bi de onun problemi cikmasin diye.


2d vector c++ diye arat googleda basit zor birşey değil
hatta

vector< vector > V; gibi birşey olmalı

push_back push_back o kadar dfgdf
Link to comment
Sosyal ağlarda paylaş




txt dosyasından gelen enterlar yuzunden looplar indexi kaciriyo
soyle yapica duzelmesi gerek adam gibi denemedim ama ilk bakista duzgundu



if (!world.fail())
{
world>>row>>column;

while(!world.eof())
{
world.get(buffer); // row columndan sonraki ilk enter icin
for (int a = 1; a <= row; a++)
{

for (int b = 1; b <= column+1; b++)
{
world.get(buffer);

//switch '*' with 'x' and empty space with 'o'
if (buffer == 'n')
{
break;
}

else if(buffer == '*')
{
current_world[a][b] = 'x';

}
else if(buffer == ' ')
{
current_world[a][b] = 'o';

}
else current_world[a][b] = 'a'; // beklenmedik bi char gelirse diye
}
}
}
}
else cout << "Unable to open file";
world.close();




bu arada butun looplar 0 danda baslayabilirdi ama hepsi 1 den basliyosa sorun yok
Link to comment
Sosyal ağlarda paylaş

simdi ayni seyi javada yaziyorum, sanirim duzelttim input davasini, daha efektif hali vardir kesin ama soyle oldu.



if (!world.fail())
{
world>>row>>column;


world.get(buffer);
for (int a = 1; a <= row; a++)
{
for (int b = 1; b <= column; b++)
{
world.get(buffer);

//switch '*' with 'x' and empty space with 'o'
if (buffer == 'n')
{
line++;
}

else if(buffer == '*')
{
current_world[a][b] = 'x';

}
else if(buffer == 'null')
{
current_world[a][b] = 'o';

}
}world.get(buffer);
}
}


Link to comment
Sosyal ağlarda paylaş

  • 3 hafta sonra ...
alin bu da javasi



import java.util.*;
import java.io.*;

class GameOfLife{

public static void CreateGeneration(char[][] currentWorld, char[][] nextGen, int row, int column)
{
int neighbors = 0;
for (int m = 1; m <= row; ++m)
{
for (int n = 1; n <= column; ++n)
{
neighbors = 0;
//Checking neighbors
if (currentWorld[m-1][n-1] == 'x') neighbors += 1;
if (currentWorld[m-1][n] == 'x') neighbors += 1;
if (currentWorld[m-1][n+1] == 'x') neighbors += 1;
if (currentWorld[m][n-1] == 'x') neighbors += 1;
if (currentWorld[m][n+1] == 'x') neighbors += 1;
if (currentWorld[m+1][n-1] == 'x') neighbors += 1;
if (currentWorld[m+1][n] == 'x') neighbors += 1;
if (currentWorld[m+1][n+1] == 'x') neighbors += 1;

//Applying rules
if (currentWorld[m][n] == 'x' && (neighbors == 2 || neighbors == 3))
nextGen[m][n] = 'x';
else if (currentWorld[m][n] == 'x' && neighbors >= 4)
nextGen[m][n] = 'o';
else if (currentWorld[m][n] == 'x' && neighbors<= 1)
nextGen[m][n] = 'o';
else if (currentWorld[m][n] == 'o' && neighbors == 3)
nextGen[m][n] = 'x';
}
}
}

//Swaps the current world with the next generation
public static void swap(char[][] currentWorld, char[][] nextGen,int row, int column)
{
//swap
for (int e = 1; e <= row; e++)
{
for (int f = 1; f <= column; f++)
{
currentWorld[e][f] = nextGen[e][f];
}
}
}

//Prints the new generation
public static void print(char[][] currentWorld, int row, int column)
{
for (int c = 1; c <= row; c++)
{
for (int d = 1; d <= column; d++)
{
System.out.print(currentWorld[c][d]);
}
System.out.println();
}
System.out.println();
}

public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(new File("c:JAVAworld.txt"));
BufferedReader bin = new BufferedReader(new FileReader("c:JAVAworld.txt"));
Scanner userin = new Scanner(System.in);

int row = in.nextInt();
int column = in.nextInt();
int c = 0, next = 0, gen = 0;
char[][] currentWorld = new char [row+2][column+2];
char[][] nextGen = new char [row+2][column+2];

//Set the world to all dead
for(int i = 0; i<=row+1;++i)
{
for(int j = 0; j<=column+1;++j)
{
currentWorld[i][j] = 'o';
nextGen[i][j] = 'o';
}
}

//Read the file into the array
String line1 = bin.readLine();
for(int i = 1; i<=row;++i)
{
for(int j = 1; j<=column;++j)
{

c = bin.read();
char character = (char) c;
if(character == '*')
{
currentWorld[i][j] = 'x';
}
else if(character == ' ')
{
currentWorld[i][j] = 'o';
}
}
String line2 = bin.readLine();
}

//Print starting world
System.out.println("This is the starting world");
print(currentWorld, row, column);

do
{
System.out.println("Enter 1 to create the next generation.");
next = userin.nextInt();
gen++;
System.out.println("This is generation " + gen);
CreateGeneration(currentWorld, nextGen, row, column);
swap(currentWorld, nextGen, row, column);
print(currentWorld, row, column);
}while(next == 1);

}
}

Link to comment
Sosyal ağlarda paylaş

bendende C# gelsin :)


using System;
using System.IO;

namespace csharp_station.howto
{
class TextFileReader
{
static void Main(string[] args)
{
// create reader & open file
TextReader tr = new StreamReader("date.txt");

// read a line of text
Console.WriteLine(tr.ReadLine());

// close the stream
tr.Close();
}
}
}
Link to comment
Sosyal ağlarda paylaş

  • 4 hafta sonra ...
gene bir dosyadan input okuyamayip cinnet gecirme durumuyla beraberiz, lan her seferinde aha bu sefer yaptim diyorum, sonra yeni bisey cikiyo gene beceremiyorum, ben kafayi yemeden bi el atin lan. format soyle,

8
3
0.000000 1.000000 0.000000
0.000000 0.000000 1.000000
1.000000 0.000000 0.000000
3
-1.000000 0.000000 0.000000
0.000000 0.000000 1.000000
0.000000 1.000000 0.000000
3
0.000000 -1.000000 0.000000
0.000000 0.000000 1.000000
-1.000000 0.000000 0.000000
3
1.000000 0.000000 0.000000
0.000000 0.000000 1.000000
0.000000 -1.000000 0.000000

en birinci inti alip bi yere koyuyorum, sonra gelenleri 3 leri atlayip, mesela bu durumda 24x3 bi arraya koymak istiyorum, koyamiyorum, cinnetim geliyo, yakicam kendimi.
Link to comment
Sosyal ağlarda paylaş

Gecen sefer c++ da yazdigin kodda txt deki enterlara dikkat etmemistin sanirim array sorun gene onlar yapman gereken

ilk int i al
enteri al dummyye at
4 kez (
sonraki 3 u al dummyye at
enteri al dummyye at
3 kez (
3 kez float al
enteri al dummyye at
)
)



Bu sekilde yazarsan olmasi gerek.
Link to comment
Sosyal ağlarda paylaş

soyle bisey yaptim ama gene kurtarmadi

infile>>triangles;
infile>>buffer;

for(int a = 0; a < triangles*3 ; ++a)
{
infile.ignore();
for(int c = 0; c < 3; ++c)
{
infile.ignore();
infile>>buffer;
if(buffer == 3){
infile.ignore();}
else
wireframe[a][c] = buffer;
}
//infile.ignore();
}


enteresan bi sekilde 3. satirin ilk elemanini atliyo veya napiyosa 0 geliyo oraya.
Link to comment
Sosyal ağlarda paylaş

×
×
  • Yeni Oluştur...