C/C++ :: Loop Runs Twice When Reading Chars To Array From File

Nov 26, 2014

I am trying to write a function to save the state of a tic tac toe game. It seems to be working well except the loop to read the chars in from a .txt file is running 18 times instead of 9 and thus overwriting the array with blank boxes. The code below is the part of the function I am having an issue with. Counter is being increased every time the second for loop runs which should be 9 times. However, it is apparently running 18 times with the first 9 runs filling newBoard correctly and the second 9 times overwriting it with boxes. how to fix it?

ifstream inputFile("file.txt");
char newBoard[3][3];
char a;
int counter = 0;
while(counter<=9) {
for(int r = 0; r<3; r++)

[code]...

View 2 Replies


ADVERTISEMENT

C :: Reading In A File Of Chars Until EOF

Apr 13, 2013

I'm working on a homework project, and it requires me to read in a file of chars into an array, and then do stuff with that array.

Anyways, I have the first part written, where I'm just trying to read in my data.txt file, and I thought I had it written well. It compiles, but then it seg faults, and I'm not sure why. I used calloc for the array, but maybe I misused it? Or is it in my EOF statement? I'm still not sure if that's coded correctly. I need to get past this so I can start testing the other parts of my code.

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[] ) {
/* Local Declarations*/
int i;
int *ptr;
char tempc;

[Code] .....

View 1 Replies View Related

C :: Adding Chars From File To Array

Jan 29, 2014

lets say we have a txt that contains this

| | | | |*| |
| | | |*| | |

and that symbolizes a 2x6 char array and we want to take only the symbols inside the || and place them in a 2x6 char array. how do we do that?

View 1 Replies View Related

C Sharp :: Loop In A Button / Runs Endless When Pressing The Button And Not Only Once

Jun 10, 2014

I have an code that makes form window + button bet bigger when pressed each time, but it runs endless till out of screen,

code:
{
int S1 = 300;
int S2 = 0;

[Code]...

make it so it runs only each time i press the button?

View 2 Replies View Related

C/C++ :: How To Check If EXE File Runs

May 19, 2014

found out that I had to split my project in two programs (console projects).

I wanna make sure in the main program that the logger runs and if I can, have the main program do commands within the console.

Now how can I do this? is it a header that I have to include? is it a built in feature of c++?

View 7 Replies View Related

C++ :: Prevent The Loop From Reading The Last File Line Twice?

Oct 19, 2013

I know how to do it in C, but in C++?

Here is my code. Let the variables be of type double.

Code:
while(infile) // Will read the last line twice
{
if(!N)//Read first line of file
{
infile >> d;
infile >> N;
infile >> epsilon;
infile >> t;

[Code] .....

View 8 Replies View Related

C :: How To Convert From Array Of Chars To Strings

Jan 13, 2014

As a part of a program I am supposed to write, I would like to receive a string from the user (for example: "Hi my name is Joe").

obviously, the string is inserted to an array of chars (arr[0]='H', arr[1]='i', arr[2]=' ',... and so on).

What I would like to do, is to put each word separately in each array cell (for example arr[0]='Hi', arr[1]="my"..., and so on). How can I do this? (I can not use any functions, unless I write them myself).

View 8 Replies View Related

Visual C++ :: Best Way To Read Array Of Chars

Jan 16, 2014

I am receiving a max of 50000 bytes from a socket.

char packet1[50000];
recv(Socket, (char*)&packet1, 50000, 0);

What is the best way to read those bytes? Let's say I need to see if bytes 14-15 and byte 16-17 equal each other.

Only thing I can think of is to create a buffer array of 2 chars, and feed the bytes into those and compare them. Is that right?

View 5 Replies View Related

C :: How To Account For Different Number Of Chars In Array Of Names

Dec 9, 2013

I am trying to make a for loop that will print out chars in an array while using a #define before main. My problem is that each name has a different amount of chars in it. How do you account for that when you are trying to define a size? For example, I am playing around with the numbers and I just put 7 in for size:

Code:
#include <stdio.h>
#define sizeOf 7
//char personName( char * name[] );
char printName ( char * name[]);

[Code] .....

View 8 Replies View Related

C :: Replacing Individual Chars In A String Array?

Nov 16, 2013

I'm trying to right the function that puts the correct letters in the right positons being reported from my previous compareletter function for my game of hang man and its not updating the word in progress array. Here's my compare letter function that's working correctly:

Code: //function that returns the index of the letter that the user has guessed or Code: //-1 if the letter isn't in the word
int CompareLetter(char array[], char guess, int numLetters)
{
int i;

[Code....

However, this isn't changing any of the position of the asterisks in the word in progress array positions 0-3.

View 7 Replies View Related

Visual C++ :: Reading Into Array / 2d Array From Mixed Text File?

Jan 18, 2014

I know to read a strings into array and tables.. what is they are mixed up?? strings are just names ( 3 characters) and there are bunch of table.. the max size was set to 60

ex. text file

JES
DAN
JEN
.
.
.
01010101
10010101
RAM
JET
01010010
10100101
.... and so on

should i use a while loop?

View 10 Replies View Related

C :: Writing Array Into File And Reading Array From File

Apr 20, 2014

i've been trying to write array into file and read them into the same program again, but the output is all wrong.

Code:

#include<stdio.h>
int main()
{
FILE *fp;
char name[3][7];
int x;
}

[code]....

View 1 Replies View Related

C/C++ :: Reading A File Into Array Then Reversing The Array

Jan 19, 2015

This is what I am supposed to be doing with this problem: Read the integer values in the file "numbers.dat" and display

* them in reverse order (that is, display the last number in the
* file first, the second-to-last second and so on). Use an array to
* store the values. It is safe to assume that the file contains no
* more than 100 values.

I have gotten far enough to read the file and display as an array, but it is displaying vertically rather than horizontally. So it is displaying 10 and a 1 on the first line then the 0 on the second line. Before I can work the reverse part out, I need it to display each number as 10 line 1 -235 line 2 so on and so forth.

This is my code so far:

void display_array_reversed() {
ifstream fin ("numbers.dat");
if (!fin){
cerr << "Error opening file" << endl;
exit(1);

[Code] .....

View 2 Replies View Related

C :: Reading From A File And 2D Array?

Apr 25, 2013

So if I have a file that has a list of scores and the name of the student, how am I going to read the scores and names, and print to output? For example, the text file will look like:

83 75 89 90 75 67 John Doe
67 92 78 64 88 95 Jane Waters
76 65 87 70 97 76 Billy Bob

And they'll be printed like that too.

I made a 2D array for the scores and names, and was able to scan the scores into the scoreArray, but I am stuck for the names. Do I use fscanf, fgets, etc? And how do I use these for a char array?

insert Code:
int main {
int scoreArray[NUM_ROW][NUM_COLUMN];
char nameArray[NUM_ROW][SIZE];
...........opened file, etc.

[ for (row=0; row<2; row++)
{ if(row>0)
{printf("

[Code] ....

When I run this, I get the scores and names (somewhat) in a jumbled mess.....

View 3 Replies View Related

C++ :: Reading In TXT File To 2D Array?

Mar 27, 2013

My assignment is to read in from a .txt file two things: an integer and a string. After reading in these 2 items I have to put them into a 10x10 2D array so that both the number and the character can be manipulated by the user. Here are the contents of the text file:

Dr. J's Garden
0.21,B, 2.80,G, 4.96,B, 2.66,B, 4.48,B, 0.61,T, 0.40,B, 3.50,G, 3.63,B, 3.91,T,
2.33,T, 4.51,G, 1.15,G, 4.95,T, 2.76,T, 4.51,T, 2.54,G, 4.04,T, 2.38,B, 0.62,B,
4.54,G, 3.38,T, 1.57,T, 3.92,T, 3.03,B, 4.72,G, 0.23,B, 4.02,G, 4.69,G, 0.66,G,
1.34,T, 2.21,G, 2.48,G, 4.85,T, 3.25,G, 3.55,G, 4.78,B, 0.81,G, 0.74,G, 2.55,G,
3.35,G, 4.52,G, 4.81,G, 2.67,G, 4.97,B, 0.87,G, 1.28,G, 4.58,B, 1.91,B, 3.69,B,
3.02,T, 3.15,T, 1.08,T, 4.68,G, 1.10,B, 3.17,G, 1.97,T, 0.99,G, 4.50,T, 3.87,T,
3.36,G, 1.60,T, 3.73,G, 2.14,B, 3.68,B, 2.44,G, 3.10,G, 4.54,B, 0.25,B, 0.25,G,
1.79,B, 3.02,G, 2.21,T, 0.22,G, 3.67,B, 4.46,G, 2.14,G, 2.31,G, 0.80,T, 3.83,B,
1.56,B, 1.41,T, 0.80,G, 1.27,T, 0.08,B, 1.20,G, 2.88,B, 2.78,T, 3.30,B, 1.75,B,
2.60,G, 4.72,T, 4.55,T, 0.89,B, 0.52,B, 2.06,T, 0.28,G, 4.36,T, 4.41,G, 0.36,B,

One number and one character need to be assigned to each element of the array. I know exactly how to fill a 2D array when it's just numbers, but the characters and the commas are giving me a lot of trouble.

View 1 Replies View Related

C++ :: Reading File Into Array?

May 10, 2012

i'm reading a file which has 20 rows and random number of columns, i want to put them in array/vector which i did but the problem is that array is filling a cell with a garbage value at location where i don't have a value in the data file.

suppose i have data file like following (there is tab between each column and each row has different number of columns)

Code:
20 30 10
22 10 9 3 40
60 4
30 200 90
33 320 22
here is my code

Code:
#include <vector>
#include <iostream>
#include <fstream>

[Code]....

View 4 Replies View Related

C++ :: Reading A File Into Array Of Struct

Nov 20, 2014

Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct inventory

[Code]...

This is my program output Enter the file name : "filename".txt ..... (Nothing shows up)

View 3 Replies View Related

C :: Reading A File Into Array Of Pointers?

Mar 24, 2013

why when I print out "array[2]" nothing prints? It just prints blank space. My file definitely has text in it, but when I try to assign "text" into the array of pointers it won't show any text. I know fgets() appends a newline at the end of the string, not sure if that has anything to do with it, but I've tried printing everything that should be in "array" with a for loop and I get nothing.

Code:
#include <stdio.h>
#define FILEPATH "/home/user/Desktop/text"
int main(){
FILE *myFile = fopen(FILEPATH, "r");
if(myFile == NULL) return 0;
char text[100];
char *array[100];
int idx = 0;
while(fgets(text, 100, myFile)){
array[idx++] = text;
}
puts(array[2]);
}

View 2 Replies View Related

C :: Reading File To Array Of Structures

Jun 9, 2013

I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:

78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc

There's suppose to one array of companies, where each company will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in an array of structures.

My question is, how do I read the data from the file and put the data from each line into the next structure in the array of structures? I read the numbers in fine. I just use:

Code: fscanf(fp, "%f", &companyAry[i].hourlyPrices[j]);

But my program crashes when I try to read the name.

Code: fscanf(fp, "%[^]", &companyAry[i].companyName);

I'm thinking it has something to do with the fact the companyName is a pointer.

My structure looks like this:

Code:

typedef struct {
char *companyName;
float hourlyPrices[NUM_PRICES];
float avgPrice;
}
COMPANY;

View 8 Replies View Related

C :: Reading Values From CSV File Into Array

Jan 21, 2014

My problem is that I need to take a csv or excel file with tens of thousands of datapoints, write all points in a section of column to an array, perform calculations on them, and then write it back into another column.

I've been looking all over the internet for ways to do this easily. So far I have not found anything that I can follow and implement.Some codes have been slightly useful, but they aren't commented in a way I can understand. How to write code to take, for instance, lines from column b between 500-1000, write them to an array, and write another array 500 characters in column c. If that code could be commented, I have tried a few different techniques (counting commas), but haven't gotten anything to work.

View 9 Replies View Related

C :: Reading Data Into 2D Array From A File

Apr 29, 2013

I'm having issues reading to a 2d array from a file. When I try to read the data from my file into my matrix variable it simply doesn't read anything and leaves the variable unmodified. I've tried just reading the first piece of data in the main function and it doesn't work there either. I'm really perplexed at this point since I've never had an issue reading from a file before. Here's my relevant code:

Code:
int main(int argc, char** argv) {
double matrix[MINSIZE][MINSIZE]={0}, vectors[MAXSIZE][MINSIZE], ans[MAXSIZE][MINSIZE];
FILE * inFile;
inFile = fopen(FILENAME,"r");
if(inFile==NULL){
printf("File does not exist.

[Code] .....

View 4 Replies View Related

C :: Storing A Value In Array From Reading In From A File

Sep 12, 2013

Is something like this possible and if not how can I make this work?

fscanf(in, "%d %d %d", &i, &x, &arr[i+x]);

View 4 Replies View Related

C++ :: Error Reading File Into Array

Feb 6, 2013

I am working in Eclipse, and it keeps giving me this error that I do not understand. In the fillTable function, "is >> kP[i]" Eclipse says: no match for 'operator>>' in 'is >> *(((TranslationTable<int, std::string>*)this)->TranslationTable<int, std::string>::kP + (+(((unsigned int)i) * 8u)))'.

#ifndef TRANSLATIONTABLE_H_
#define TRANSLATIONTABLE_H_
#include "KeyValuePair.h"
#include <iostream>
#include <cstdlib>

[Code]....

View 3 Replies View Related

C++ :: Reading Int From Text File With 2D Array

Nov 3, 2014

Reading in a maze into a 2D array. The first two reads will give me the dimension of the maze(ex. m x n maze). So in order to create the 2D array i need the first two reads. Then after it is created it will read the rest of the data which are 1s and 0s. I have to create a program that will solve the maze but i cant test my movement code if i cant read in the data first. the entire program compiles but to test if i read the file i have a function to print it. But it says "There are: 0 rows and 0 columns " so it didn't read anything since rCount and cCount are initialized to 0. and basically the maze has nothing in it.

Code:
#include <iostream>
#include <fstream>
#include <stack>
#include <cstdlib>
#include "position.h"

[Code] .....

View 3 Replies View Related

C++ :: Reading Strings From File To 2D Array?

Apr 27, 2013

I have a text file with scores and names.

I have to read the score into an array and the names into an arrays also

27,Tom Jefferson
23,Ozzie Osborne
18,Wilt Chamberlain
15,Marie Antoinette

I've gotten the score to display correctly, but i cant get the full names. My function only reads the first names

View 8 Replies View Related

C++ :: Reading From A File - Getting Empty Array?

Nov 30, 2013

I started making something for my class and the thing im getting stuck with is this function:

void ucitajOdgovore(string asocijacija[21]){
int brojac=0;
ifstream fajl;
string putanja;
/*srand(time(NULL)); int random=(rand() %5) +1;
switch(random){

[Code] ....

In the main, i pass real string array "asocijacije" in function which i use in it, and when i use it after this fun. i get an empty array. Its like it didnt happend and i cant see where i went wrong.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved