Strange beeping console

Whenever I run this code, the console scrolls through a bunch of garbage and beeps annoyingly at me. The beeps are fast and loud. When I change it from (rand()%2 + 1) to (rand()%3 + 1), it does this. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Actor.cpp
#include "Actor.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<string>

void Actor::SetGender()
{
    srand ( time(NULL) );

    int x = 0;
    while(x == 0)
    {x = (rand()% 3 + 1);}
    m_Gender = x;
}
I think the problem is not in Acror.cpp, but in Actor.h.

1
2
3
4
5
6
7
8
#include <iostream>
#include <conio.h>
int main ()
{
    std::cout << (char) 7;
    getch ();
    return 0;
}


This code beeps also, but make sure its the same kind of beep. Maybe you are printing ASCII character 7, which is the bell: http://www.asciitable.com/ inside Actor.h.
This the full Header(Actor.h).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//there will be many "people" simulated
//actors will interact, interactions will adjust each variable through an equation
/*
the greater the difference between each actors personality variables there is
the greater the mutual adjustment to bring said variable closer to matching
*/
//see http://en.wikipedia.org/wiki/Big_Five_personality_traits
#ifndef ACTOR_H
#define ACTOR_H
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<string>

using namespace std;


class Actor
{
    public:
        Actor(void);
        virtual ~Actor(void);
        //(inventive / curious vs. consistent / cautious). Appreciation for art, emotion, adventure,
        //unusual ideas, curiosity, and variety of experience.
        int GetOpenness(void) { return m_Openness; }
        void SetOpenness(int val) { m_Openness = val; }
        //(efficient / organized vs. easy-going / careless). A tendency to show self-discipline,
        //act dutifully, and aim for achievement; planned rather than spontaneous behavior.
        int GetConscientiousness(void) { return m_Conscientiousness; }
        void SetConscientiousness(int val) { m_Conscientiousness = val; }
        //(outgoing / energetic vs. shy / reserved). Energy, positive emotions, surgency,
        //and the tendency to seek stimulation in the company of others.
        int GetExtraversion(void) { return m_Extraversion; }
        void SetExtraversion(int val) { m_Extraversion = val; }
        //(friendly / compassionate vs. cold / unkind). A tendency to be compassionate and
        //cooperative rather than suspicious and antagonistic towards others.
        int GetAgreeableness() { return m_Agreeableness; }
        void SetAgreeableness(int val) { m_Agreeableness = val; }
        //(sensitive / nervous vs. secure / confident). A tendency to experience unpleasant emotions
        //easily, such as anger, anxiety, depression, or vulnerability.
        int GetNeuroticism(void) { return m_Neuroticism; }
        void SetNeuroticism(int val) { m_Neuroticism = val; }
        //actors will eventually die of old age
        int GetAge(void) { return m_Age; }
        void SetAge(void);
        //male or female
        string GetGender(void);
        void SetGender(void);
        //prefer male companionship or female, relations with like orientation and gender possible
        //strait actors may be hostile to gay actors, dependant on other member variables
        int GetSex_Orient_(void) { return m_Sex_Orient_; }
        void SetSex_Orient_(int val) { m_Sex_Orient_ = val; }
        //females in relationships with males may become pregnant, add actor(s) to simulation
        bool GetPregnant(void) { return m_Pregnant; }
        void SetPregnant(bool val) { m_Pregnant = val; }
        //each actor needs an id number to track it
        int GetActor_Number(void) { return m_Actor_Number; }
        void SetActor_Number(int val) { m_Actor_Number = val; }
    protected:
    private:
        int m_Openness;
        int m_Conscientiousness;
        int m_Extraversion;
        int m_Agreeableness;
        int m_Neuroticism;
        int m_Age;
        int m_Gender;
        int m_Sex_Orient_;
        bool m_Pregnant;
        int m_Actor_Number;
};

#endif // ACTOR_H 


And main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
//Author: Chris Kelso(lare26)
//Date of conception: November 04, 2010
//Purpose: Use a large, and complicated project to force myself to
//         learn C++
//Project: World Detailed. Many actors will move throughout a grid representing a
//         map of a small(or larger) city and interact and learn from personality
//         traits from each other. They will reproduce and develope relationships.
//         They will live for a limited time and then die. There will be a set
//         number of turns to run the simulation(users discretion) and then will
//         pause to allow continuation of the simulation or termination.
*/
#include <iostream>
#include<string>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include "header_file_32478.h"

using namespace std;


//using main to test ideas and code (mine and others) first!
int main()
{
    //seed rand()
    srand(time(NULL));
    //create a list
    list<Actor> my_list;
    int NN = 10;

for(int i = NN; i > 0; i--){
    Actor a;
    a.SetOpenness(rand()%1000);
    a.SetActor_Number(i);
    a.SetAge();
    a.SetAgreeableness(rand()%1000);
    a.SetConscientiousness(rand()%1000);
    a.SetExtraversion(rand()%1000);
    a.SetGender();
    a.SetNeuroticism(rand()%1000);
    a.SetPregnant(rand()%1);
    a.SetSex_Orient_(rand()%3);
    my_list.push_back(a);
}

list<Actor>::iterator it, tt;
for(it = my_list.begin(); it != my_list.end(); it++){
    cout << "Actor Number: " << it->GetActor_Number() << " ";
    cout << "Age: " << it->GetAge() << endl;
        if(it->GetAge() > 80){
        cout << "He/she died\n";
        //erase returns an iterator to the next element.
        it = my_list.erase(it);
        //if this element already was the last one, it will return my_list.end().
        //in that case it++ will fail, so I break out of here.
        if(it == my_list.end()) break;
    }
    cout << "Openness: " << it->GetOpenness() << "  ";
    cout << "Agreeableness: " << it->GetAgreeableness() << endl;
    cout << "Conscientiousness: " << it->GetConscientiousness() << "  ";
    cout << "Extraversion: " << it->GetExtraversion() << endl;
    cout << "Gender: " << it->GetGender() << "  ";
    cout << "Neuroticism: " << it->GetNeuroticism() << endl;
    cout << "Pregnant: " << it->GetPregnant() << "  ";
    cout << "Sex Orient: " << it->GetSex_Orient_() << endl << endl;
}

    return 0;
}


Could it be in main()? I am still teaching myself how to use dynamic memory(new delete).
Last edited on
what are you trying to do? Line 60 - 67 are outside of the for loop. So 'it' will be invalid in any case. So it might beep or not.

line 55 is also not ok. If you erease 'it' (and not end()) the very next thing happens is the increment. So you will skip the entry after the erased one.
Lines 60-78 are part of the for block started on line 48. I am trying to dynamically create actors which will live a life while interacting with each other. Lines 60-67 are there so I can see if what I write has the desired effect. The end program won't have those lines. In fact all of main will be different. I'm just trying to figure out how to access each object after creating it. I'm not even halfway there yet.

Also, I am confused about line 55 as well. Someone else helped me with that one. I have no idea how-why-if it does what I want it to.
Last edited on
well now I see that's not outside the loop. This format of the { } can really be misleading

to cope with erase you need another loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
list<Actor>::iterator it = my_list.begin(), tt;
while(it != my_list.end()){
    cout << "Actor Number: " << it->GetActor_Number() << " ";
    cout << "Age: " << it->GetAge() << endl;

// put lines 60 - 67 here to avoid the end() => invalid 'it' problem

    if(it->GetAge() > 80){
        cout << "He/she died\n";

        //erase returns an iterator to the next element.
        it = my_list.erase(it);
    }
    else
        ++it;
}
Last edited on
Now all the stats are identicle for each Actor object. Did I do it wrong, again? Maybe rand() is too slow?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
//Author: Chris Kelso(lare26)
//Date of conception: November 04, 2010
//Purpose: Use a large, and complicated project to force myself to
//         learn C++
//Project: World Detailed. Many actors will move throughout a grid representing a
//         map of a small(or larger) city and interact and learn from personality
//         traits from each other. They will reproduce and develope relationships.
//         They will live for a limited time and then die. There will be a set
//         number of turns to run the simulation(users discretion) and then will
//         pause to allow continuation of the simulation or termination.
*/
#include <iostream>
#include<string>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include "header_file_32478.h"

using namespace std;


//using main to test ideas and code (mine and others) first!
int main()
{
    //seed rand()
    srand(time(NULL));
    //create a list
    list<Actor> my_list;
    int NN = 10;

for(int i = NN; i > 0; i--){
    Actor a;
    a.SetOpenness(rand()%1000);
    a.SetActor_Number(i);
    a.SetAge();
    a.SetAgreeableness(rand()%1000);
    a.SetConscientiousness(rand()%1000);
    a.SetExtraversion(rand()%1000);
    a.SetGender();
    a.SetNeuroticism(rand()%1000);
    a.SetPregnant(rand()%1);
    a.SetSex_Orient_(rand()%3);
    my_list.push_back(a);
}

list<Actor>::iterator it = my_list.begin(), tt;
while(it != my_list.end()){
    cout << "Actor Number: " << it->GetActor_Number() << " ";
    cout << "Age: " << it->GetAge() << endl;
    cout << "Openness: " << it->GetOpenness() << "  " << endl;
    cout << "Agreeableness: " << it->GetAgreeableness() << endl;
    cout << "Conscientiousness: " << it->GetConscientiousness() << "  " << endl;
    cout << "Extraversion: " << it->GetExtraversion() << endl;
    cout << "Gender: " << it->GetGender() << "  " << endl;
    cout << "Neuroticism: " << it->GetNeuroticism() << endl;
    cout << "Pregnant: " << it->GetPregnant() << "  " << endl;
    cout << "Sex Orient: " << it->GetSex_Orient_() << endl << endl;
    if(it->GetAge() > 80){
        cout << "He/she died\n";
        //erase returns an iterator to the next element.
        it = my_list.erase(it);
    }
    else
        ++it;
}

    return 0;
}
I made it compiling and for me they're all different.
1
2
3
void Actor::SetGender()
{
    srand ( time(NULL) );
You are reseting the seed. (time measures in seconds, but the computer is a lot faster than that)

If your getter/setter don't do anything then just make your variables public.

Do you still have the beep? Maybe is a hardware problem
Holy Cow! I changed a lot. I got the beeping to stop after pulling out some of my hair. I can't recall how. I changed a lot in the program too. Thanks for all your help coder777.

ne555, I figured out the srand() deal. I'm actually learning how to understand the STL references. I looked it up and *KER-POW* I finally got it right. Re-seeding really does mess it up.

*Wipes sweat from brow and glues hair back on*

Now for my next trick...learning file I/O. This forum keeps pulling my hide out of the E-fire. I appreciate every ones help.
Topic archived. No new replies allowed.