Sign in to follow this  

program ignoring system("PAUSE")

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I need to use use system("PAUSE"); at the end of my program - otherwise it will close. I can't have it closed because then he won't finish writing the data he calculated into the file input. Ok, so I place system("PAUSE"); at the end, but my program is ignoring this line. It used to work before, now it won't. I will work in the beginning of the Main() funtion, but not at the end. More precisely, it will stop working at the folowing loop:
        for (int j = 0;j < delta.size(); j++)
        {
            dummier = calculate(beta[i], delta[j]);
            dummy += dummier;
            system("PAUSE");
            cout << dummier << " " << j << endl;
            if(j==0 && i==0)
            probdeltas.push_back(dummier);
            else
            probdeltas[j] += dummier;
        }



It will work fine until j = 5. Then, it will not print. It will happend after the j = 5, independently of how many times system("PAUSE"); was called before. Does anybody know what is going on? How can my loop interfere with that... and only after j = 5? EDIT: I'll have to leave now, so I think I'll post the source code, if anyone is interested. I'll warn it is a mess, and the interface is in portuguese (it shouldn't be needed tougth, as the program deals with file input and output).
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <math.h>
#include <stdlib.h>
#include <cstdlib>

#define e 2.718281828459

using namespace std;

vector <float> delta;
vector <string> deltaname;
vector <float> beta;
vector <string> betaname;
vector <float> probabilidade;
vector <float> probdeltas;

// calculate is the heart. It will take the beta value of each person,
// with the delta value of each question, and give that probability
// the result is used both for the personal chance of scoring the questions
// and the question chance of being scored
float calculate(float beta, float delta)
{
    float dummy = pow(e, (beta - delta));
    float probability = dummy/(1+dummy);
    return probability;
}

int main()
{
    ifstream deltas ("deltas.txt"); // loading deltas.txt
    if(!deltas.is_open())
    {
        cout << "ERRO: o arquivo deltas.txt nao foi encontrado ou nao pode ser aberto" << endl; //ERROR, couldn't open it
        return 0;
    }
    string line; // just a random string for storing the item name
    float deltainfile;
    while (!deltas.eof())
    {
    	deltas >> line >> deltainfile;  //read first the name than the delta value of it
    	deltaname.push_back(line);  //add the name to a vector
    	delta.push_back(deltainfile);   //then add the value to another vector
    }
    deltas.close();

    ifstream betas ("betas.txt");
    if(!betas.is_open())
    {
        cout << "ERRO: o arquivo betas.txt nao foi encontrado ou nao pode ser aberto" << endl;
        return 0;
    }

    string nameinfile;  //will be the name, similar to line up there
   	float betainfile; // will store the beta value we got from the file
    while (!betas.eof())
    {
        betas >> nameinfile >> betainfile;
    	beta.push_back(betainfile);
    	betaname.push_back(nameinfile);
    	cout << nameinfile << "\t" << betainfile << endl; //for debuggin, we get the value
    }
    betas.close();

    float probabilidade = 0; //this will store the overall probability of each beta versus each delta
    bool jackass = true; //to see if we have allready been there
    ofstream betafile;
    betafile.open ("probabilidade individual.txt");
    //each beta (i) will go tought all deltas(j) and get the value
    for (int i = 0;i < beta.size();i++)
    {
        float dummy = 0, dummier = 0;
        for (int j = 0;j < delta.size(); j++)
        {
            dummier = calculate(beta[i], delta[j]);
            dummy += dummier;   //dummy stores the values of all deltas for a single beta, latter sent into
                                //file output on betafile
            system("PAUSE");
            cout << dummier << " " << j << endl;
            system("PAUSE");
            if(j==0 && i==0) //if there is no probdelta values for each j iet, we must create it
            probdeltas.push_back(dummier);  //we can't send these values as they are calculated to the file
                                            //like we are doing with dummy, so we need to store them for later.
            else
            probdeltas[j] += dummier;
            system("PAUSE");
        }

        if(jackass)//if this is the first line of the output, jackass will be true.
        {
            jackass = false;
            betafile << betaname[i] << "\t" << dummy/float(delta.size());
        }
        else //if this is not the first line, then we need to start with a "\n"
        {
            betafile << "\n" << betaname[i] << "\t" << dummy/float(delta.size());
        }
        probabilidade += dummy/float(delta.size()); //this must be avaraged by the ammount of questions
    }

    betafile.close();
    ofstream deltafile;
    deltafile.open ("probabilidade_de_cada_questão.txt");
    jackass = true;
    // Now, we need to output the calculated data of each delta for all betas, we do
    // that in this loop
    for (int j = 0;j < delta.size(); j++)
    {
        if(jackass) //if is first line of file, no need of "\n"
        {
            jackass = false;
            deltafile << deltaname[j] << "\t" << probdeltas[j]/float(beta.size());
            cout << probdeltas[j] << " " << float(beta.size()) << " "; //couting for debug
        }
        else
        {
            deltafile << "\n" << deltaname[j] << "\t" << probdeltas[j]/float(beta.size());
            cout << probdeltas[j] << " " << float(beta.size()) << " ";
        }
    }
    deltafile.close();
	system("PAUSE");
    cout << "a media das probabilidades de todos os candidatos e de: " << probabilidade/float(beta.size()) << endl;

	system("PAUSE");
	return 0;
}

and the files should look like these:

betas.txt
m45628	2
m45627	1
m45626	0
deltas.txt
item001	2
item002	1
item003	0
item004	-0.5
(no blank lines inside the file)

I don't advise anyone try to understand this kind of mess, but if you are curius about what is going on, you can try to compile it and see if in you PC the same problem happens. I just don't get how can it stop calling a function like that... anyway, thanks for any one bothering trying to understand what is going on. [Edited by - algumacoisaqualquer on March 2, 2006 11:46:15 PM]

Share this post


Link to post
Share on other sites
Just from that code, there's no real reason why your system("PAUSE") would not be called when j=5 unless if delta.size is 5, and your loop no longer executes. system() _could_ be returning an error of some sort, so you might want to check that.

If you are on Windows, I would think MessageBox() or a console gets() would be a much better implementation of a pause, however...

Share this post


Link to post
Share on other sites
Oh, and an update: it will not have the same behavior if I click the execultable file in windows, rather than running if tought the compiler (Code::Blocks by the way). By running directly, all system("PAUSE"); calls are made corectly, then it will give me an error and crash :D

bpoint: how do I check what system returned? I'll try the MessageBox when I return, thans for remembering, but now I realise I have the crashing problem... oh well.

Share this post


Link to post
Share on other sites
Quote:
Original post by algumacoisaqualquer
bpoint: how do I check what system returned? I'll try the MessageBox when I return, thans for remembering, but now I realise I have the crashing problem... oh well.


int res = system("PAUSE");

Incidentally, the pause command is actually part of cmd.exe. It is not a real application (unless you have a pause.exe somewhere in your path). You really should use MessageBox instead. :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Not a part of problem, but this isn't very nice #define e 2.718281828459, because now everywhere you have 'e' it has replacet 2.718281828459

Share this post


Link to post
Share on other sites
Well, now MessageBox works fine when I run from Code::Blocks, but won't appear when I run directly from windows explorer. Also, running from windows (wicth it will be made when it starts working) will crash. I checked it out, and is seems that the vector <string> deltaname[j] will crash for j > 2. Now, weird part is that, if I run from code::blocks, this crash won't happen. And if I tell him to print all deltaname before the main loops where I calculate data, it will print fine. But deltaname isn't even used in that piece of the code. I was considering memory leaking, but my entire program uses vectors and stringstrems!
Oh, by the way anon, I changed that. Didn't solved anything as you said, but looks safer.

Share this post


Link to post
Share on other sites
probdeltas[j] += dummier; is an error. You only ever add one element to probdeltas, so access to any element except probdeltas[0] is undefined behaviour.

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
probdeltas[j] += dummier; is an error. You only ever add one element to probdeltas, so access to any element except probdeltas[0] is undefined behaviour.

Σnigma


Jesus Christ, thanks a milion for this!!
What happened was that I was using j == 0, so when that gave me problems, I added the & i==0. Turns out it should be only i==0 the whole time. I feel stupid now for not spoting this, but in my defence, the program was complaining about the deltaname[j], and was printing probdeltas[j] just fine...
Oh boy, thank you so much!

Share this post


Link to post
Share on other sites

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this