Author Topic: Code only compiles some of time  (Read 2982 times)

Offline C0UG3R

  • Single posting newcomer
  • *
  • Posts: 4
Code only compiles some of time
« on: January 25, 2015, 10:06:40 am »
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;
}

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Code only compiles some of time
« Reply #1 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)
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.
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...
« Last Edit: January 25, 2015, 11:32:42 am by BlueHazzard »

Offline C0UG3R

  • Single posting newcomer
  • *
  • Posts: 4
Re: Code only compiles some of time
« Reply #2 on: January 25, 2015, 10:15:38 pm »
1) Please use code Tags, if you post code (the # symbol in the new post editor)
2)
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.
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...
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;".

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Code only compiles some of time
« Reply #3 on: January 25, 2015, 10:20:38 pm »
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)?

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

Offline C0UG3R

  • Single posting newcomer
  • *
  • Posts: 4
Re: Code only compiles some of time
« Reply #4 on: January 26, 2015, 01:25:24 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)?

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
Windows 7. What would be the correct forum that I should post to if this does get locked?

Online stahta01

  • Lives here!
  • ****
  • Posts: 7588
    • My Best Post
Re: Code only compiles some of time
« Reply #5 on: January 26, 2015, 01:58:54 am »
You need to learn how to post the full rebuild log; and ask the questions that are not valid on this site on another site.
That site should be one the supports either the programming Language, the Compiler, or the software Library being used.

http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Note: I only remember reading your last post; so, I have no idea if your issue is valid or not for this site.

For another site to look at I suggest this one http://cboard.cprogramming.com/

Tim S.



 
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Code only compiles some of time
« Reply #6 on: January 26, 2015, 02:00:05 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.
This happens because the std::cout is buffered and doesn't flush the buffers to the output device.
Read the iostream documentation to understand how to flush the buffers. Hint use std::endl;
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline C0UG3R

  • Single posting newcomer
  • *
  • Posts: 4
Re: Code only compiles some of time
« Reply #7 on: January 26, 2015, 03:55:46 am »
Thank for all the replies. The code was for a friend but he die so I do not need anymore.

Bye.