User forums > Help
Code only compiles some of time
C0UG3R:
Sorry if explanation is bad, English is not my first language.
I'm needed to make code to read file and print code from file.
I'm new to coding but know the basics.
I've just included the code that written up to this point.
When program is run nothing shows up. Not even "Hi" at begin of code.
When I remove parts of code "Hi" shows up. It seem to cause problem when opening of file first starts in function.
If this is wrong place to post I apologize, anymore information needed can be provided.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void ReadData (int accts[], double balance [], int size);
void SortData (int accts[], double balance[]);
int ReadNumber ();
int Search (int userNum, int accts[], double balance []);
int main()
{
cout << "Hi";
//Declare variables
const int ARRAY_SIZE = 20;
int accts[ARRAY_SIZE];
double balance[ARRAY_SIZE];
int count;
int userNum;
//Call functions
ReadData (accts, balance, ARRAY_SIZE);
//Printing the Arrays
for(count = 0; count < 20; count++)
cout << accts[count] << " " << balance[count];
return 0;
}
void ReadData (int accts[], double balance [], int size)
{
ifstream infile; //input
int count;
//Opening the file
infile.open("demo.txt");
cout << "Opening file...\n";
if (!infile)
cout << "File open failure!\n";
//Reading the file
for(count = 0; count < size; count++)
{
infile >> accts[count];
infile >> balance[count];
}
//Closing the file
cout << "Closing file...\n";
infile.close();
return;
}
BlueHazzard:
1) Please use code Tags, if you post code (the # symbol in the new post editor)
2)
--- Quote from: C0UG3R on January 25, 2015, 10:06:40 am ---When program is run nothing shows up. Not even "Hi" at begin of code.
When I remove parts of code "Hi" shows up. It seem to cause problem when opening of file first starts in function.
--- End quote ---
2.1) Does the program compile?
2.2) Does the compiler throws errors?
2.3) Does the console shows up?
2.4) Is the file "demo.txt" in the rigth folder (in the folder of the exe if you run it directly, in the folder of the .cbp file if you run it from within c::b)?
if 2.1 or 2.2: Give us the full rebuild log: http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F (read ALL and follow ALL steps)
if nothing from the upper points:
What code do you remove?
if not one of the upper parts, then it is probably a programming error, and so this forum is not the right place and this topic will probably get locked...
greetings
[Edit:] i have tested your program and on my pc it works without problems...
C0UG3R:
--- Quote from: BlueHazzard on January 25, 2015, 11:29:16 am ---1) Please use code Tags, if you post code (the # symbol in the new post editor)
2)
--- Quote from: C0UG3R on January 25, 2015, 10:06:40 am ---When program is run nothing shows up. Not even "Hi" at begin of code.
When I remove parts of code "Hi" shows up. It seem to cause problem when opening of file first starts in function.
--- End quote ---
2.1) Does the program compile?
2.2) Does the compiler throws errors?
2.3) Does the console shows up?
2.4) Is the file "demo.txt" in the rigth folder (in the folder of the exe if you run it directly, in the folder of the .cbp file if you run it from within c::b)?
if 2.1 or 2.2: Give us the full rebuild log: http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F (read ALL and follow ALL steps)
if nothing from the upper points:
What code do you remove?
if not one of the upper parts, then it is probably a programming error, and so this forum is not the right place and this topic will probably get locked...
greetings
[Edit:] i have tested your program and on my pc it works without problems...
--- End quote ---
The code does compile. I have noticed no errors. The console does show up. And demo.txt is in the same folder as the program. I attempt to remove all of the code and one by one add to it and the part that is making the program print nothing is the "ifstream infile;".
BlueHazzard:
--- Quote from: BlueHazzard on January 25, 2015, 11:29:16 am ---2.4) Is the file "demo.txt" in the rigth folder (in the folder of the exe if you run it directly, in the folder of the .cbp file if you run it from within c::b)?
--- End quote ---
What os are you using.
NOTICE: as you pointed out, this is not a c::b error, but a general programming error/question. This forum is the wrong place to ask this questions. I can't guarantee that this topic won't get locked by an admin because it violates the rules
C0UG3R:
--- Quote from: BlueHazzard on January 25, 2015, 10:20:38 pm ---
--- Quote from: BlueHazzard on January 25, 2015, 11:29:16 am ---2.4) Is the file "demo.txt" in the rigth folder (in the folder of the exe if you run it directly, in the folder of the .cbp file if you run it from within c::b)?
--- End quote ---
What os are you using.
NOTICE: as you pointed out, this is not a c::b error, but a general programming error/question. This forum is the wrong place to ask this questions. I can't guarantee that this topic won't get locked by an admin because it violates the rules
--- End quote ---
Windows 7. What would be the correct forum that I should post to if this does get locked?
Navigation
[0] Message Index
[#] Next page
Go to full version