Please Help asap, extremely frustrated


I am working on a simple Population class program that would succesfully read in info to the object data and then succesfully print them.

I am an extreme beginner and am struggling mightily and have become extremely frustrated, any assistance would be much appreciated.

I am getting invalid conversion errors from int* to int and from long int* to long int and i have not been able to figure out the error

I am also getting an error : invalid types `int[int]' for array subscript

please help I am at my wits end!


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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

class Population{
private:
        long population;
        int births;
        int deaths;
        string city;
        string state; 
        double birthrate;
        double deathrate;
public:
       Population(){
                    population = 0;
                    births=0;
                    deaths=0;
                    city=" ";
                    state=" ";
                    birthrate=0.0;
                    deathrate=0.0;
                    }
       void setPop(long);
       void setBirths(int);
       void setDeaths(int);
       void setCity(string);
       void setState(string);
       double calcBirthrate();
       double calcDeathrate();
       string getCity();
       string getState();
       double getBirthrate();
       double getDeathrate();
};
void Population::setPop(long pop){
     population=pop;
     }
void Population::setBirths(int birth){
     births=birth;
     }
void Population::setDeaths (int death){
     deaths=death;
     }
void Population::setCity (string cityname){
     city=cityname;
     }
void Population::setState (string statename){
     state=statename;
       }
string Population::getCity(){
       return city;
       }
string Population::getState(){
       return state;
       }
double Population::getDeathrate(){
       deathrate=(deaths/population);
       return deathrate;
       }
double Population::getBirthrate(){
        birthrate=(births/population);
       return birthrate;
       }




int main()
{
    Population pop[3];    //Declare 3 Population objects
    
    for(int i=0;i<3;i++)
    {
    //Get the Population

    cout<<"Please enter the population of the City."<<endl;
    int citypop;
    cin>>citypop;
    pop[i].setPop(citypop);
    
    //Get the annual # of births
    
    cout<<"Please enter the # of births in the last year."<<endl;
    int citybirths;
    cin>>citybirths;
    pop[i].setBirths(citybirths);
    
    //Get the annual # of Deaths
    cout<<"Please enter the # of deaths in the last year."<<endl;
    int citydeaths;
    cin>>citydeaths;
    pop[i].setDeaths(citydeaths);
    
    //Get the City
    cout<<"Please enter the Name of the City Name."<<endl;
    string currentcity;
   getline(cin, currentcity);
    pop[i].setCity(currentcity);
    cin.get();
    //Get the State
    cout<<"Please enter the Name of the State."<<endl;
    string currentstate;
    getline(cin, currentstate);
    pop[i].setState(currentstate);

    cin.get();
    }
 
 //Print information
 cout << "Here is the information for each city."<<endl;
 cout << fixed << showpoint << setprecision(2);
  
for (int j= 0; j < 3; j++)
   {
   cout<<pop[j].getCity()<<" , "<<pop[j].getState()<<endl;
   cout<<"Birthrate : "<<pop[j].getBirthrate()<<endl;
   cout<<"Deathrate : "<<pop[j].getDeathrate()<<endl;
}
          
system ("pause");
}
Last edited on
Hi there ! Use code tags please XD

First of all, always follow strict rules when naming variables/functions/classes/objects. One of the errors is directly related to a mix of names.

The following will cause problems because pop is an array so to store a value into population which is a variable of type long you have to either specify a specific element of the array pop[] or maybe you don't want an array at all here.
1
2
3
void Population::setPop(long pop[]){
population=pop[element];
}


Looking through at first glance it looks like you mean this:
1
2
3
void Population::setPop(long pop){
population=pop;
}

which will also fix this section:
1
2
3
long pop; //setPop expects a 'long'
cin>>pop;
pop[i].setPop(pop); //only passing 1 variable 



The error from ambiguous names is here:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
Population pop[3]; //pop[] is declared

for(int i=0;i<3;i++)
{
//Get the Population

cout<<"Please enter the population of the first City."<<endl;
int pop; //oh ohhh another pop is declared...
cin>>pop;
pop[i].setPop(pop);  //pop[i] ? what is that ? is it the 'int pop' or the Population 'pop' ? 


There's a few other related errors that u should be able to fix now with ninja speed. Hope this helps :)
Last edited on
thanks for the quick reply!

it was very helpful I will relook over the code and try to implement the necessary fixes


thanks again!
Last edited on
ugh more errors! Please help again! lol i have no clue where to even start with this one. I've never seen anything like it yet.

I took your advice and changed lots of naming schemes and such and got through the initial errors but new ones arose.

It seems to be caused by this piece of code:

1
2
3
4
5
6
7
for (int j= 0; j < 3; j++)
   {
   cout<<pop[j].getCity<<" , "<<pop[j].getState<<endl;
   cout<<"Birthrate : "<<pop[j].getBirthrate<<endl;
   cout<<"Deathrate : "<<pop[j].getDeathrate<<endl;
}


the errors are :
120 no match for 'operator<<' in 'std::cout << pop[j].Population::getCity'
121 ")) << pop[j].Population::getBirthrate'
122 ")) << pop[j].Population::getDeathrate'

Also, in between these 3 errors, i get a mass ammount of NOTES that look like: (and by mass, i mean mass, there are a lot of notes that look like this and i have no clue what it is)

note C:\Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:63 candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]



note C:\Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:63 std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]


thanks for any assistance!
Last edited on
If getCity is a function then you forgot the parenthesis. With names like getCity, getState, etc I'd imagine they are all functions and need to be called like so:
1
2
cout << pop[j].getCity() << " , " << pop[j].getState() << endl;
...
omg thx, dont i feel stupid! i was looking into the notes too much trying to figure it out
Haha it looks nasty I know but it's probably something small. I think that this:
pop[j].getCity //there is no member getCity in Population

should be this:
pop[j].getCity(); //call the member function getCity()
Last edited on
ive succesfully compiled it finally! Now it just isnt doing what I want it to but i guess thats for me to figure out.

thanks a lot guys !
cool XD
Now that I have succesfully compiled (original topic updated with current code)
I am getting errors in the output.

The loop is only going through once, asking me for all the correct information. But after the last piece of information is input, it skips to end of program, ignoring the input for the other two objects.

Also, the output section at the end of the program does not correctly print the information that was stored. (in this case only for object pop[0].)

I think its caused by either my declaration of the array, or by lack of cin manipulation (get/ignore). idk im losing it.

Here is the output



Please enter the population of the City.
36000
Please enter the # of births in the last year.
240
Please enter the # of deaths in the last year.
350
Please enter the Name of the City Name.
Brooklyn
Please enter the Name of the State.
New York
Please enter the population of the City.
Please enter the # of births in the last year.
Please enter the # of deaths in the last year.
Please enter the Name of the City Name.
Please enter the Name of the State.
Please enter the population of the City.
Please enter the # of births in the last year.
Please enter the # of deaths in the last year.
Please enter the Name of the City Name.
Please enter the Name of the State.
Here is the information for each city.
 , rooklyn
Birthrate : 0.00
Deathrate : 0.00
 ,
Birthrate : 0.00
Deathrate : 0.00
 ,
Birthrate : 0.00
Deathrate : 0.00
Press any key to continue . . .

Last edited on
Hi !
First, there's some inconsistent use of cin/getline:
1
2
3
4
5
6
7
8
9
10
11
12
    //Get the annual # of Deaths
    cout<<"Please enter the # of deaths in the last year."<<endl;
    int citydeaths;
    cin>>citydeaths;
    pop[i].setDeaths(citydeaths);
    
    //Get the City
    cout<<"Please enter the Name of the City Name."<<endl;
    string currentcity;
   getline(cin, currentcity);
    pop[i].setCity(currentcity);
    cin.get();  //what is this for? 


Try to stick to using either 1 way or the other. Often when a program skips like that it's because there is still something in the stream. 'cin' only waits for user input when it is in a good state and there is nothing currently in the stream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    //Get the annual # of Deaths
    //this should fix that problem
    cout<<"Please enter the # of deaths in the last year."<<endl;
    int citydeaths;
    cin>>citydeaths;
    cin.clear(); //keeps the input stream empty
    pop[i].setDeaths(citydeaths);
    
    //Get the City
    cout<<"Please enter the Name of the City Name."<<endl;
    string currentcity;
   cin >> currentcity; 
   cin.clear();
    pop[i].setCity(currentcity);

Also note that 'cin >>' is for formatted output. If the operation fails (ie: bad input) things will go strange.

The last thing will be 'Birthrate and Deathrate' not printing anything but 0. This is because of a division of type long which will truncate decimals to 0 in your member functions.
Last edited on
i switched population type to int and am still not getting any calculations.

cin.clear statements are helping, but am now skipping in different areas of the program.

the final cin.get() that was asked about was because it was freezing after the first run through, and this allowed it to go through next loop.

still cant get it


Please enter the population of the City.
36000
Please enter the # of births in the last year.
550
Please enter the # of deaths in the last year.
660
Please enter the Name of the City Name.
brooklyn
Please enter the Name of the State.
New York
Please enter the population of the City.
Please enter the # of births in the last year.
Please enter the # of deaths in the last year.
Please enter the Name of the City Name.
Bradfield
Please enter the Name of the State.
New Jersey
Please enter the population of the City.
Please enter the # of births in the last year.
Please enter the # of deaths in the last year.
Please enter the Name of the City Name.
why
Please enter the Name of the State.
Happens?
Here is the information for each city.
 ,
Birthrate : 0.00
Deathrate : 0.00
ew York ,
Birthrate : 0.00
Deathrate : 0.00
ew Jersey ,
Birthrate : 0.00
Deathrate : 0.00
Press any key to continue . . .



Every instance that i run the program, it seems a different skip is happening
I have gotten the calculations to work correctly, i changed everything to doubles.

Now the only thing left is to get it to stop skipping questions, and to print my strings correctly.
No, the cin.get() just hides the problem and they should be removed.
The problem is that the input stream needs to be empty and 'cin' has to be in a good state. If cin is in a bad state then none of those cin lines will execute because when cin >> is called it immediately takes what it sees (which is garbage) in the stream - so it doesn't wait for user input (it thinks the user input something).

cin.clear, cin.ignore, cin.sync can put cin back into a good state depending on the errors.



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
int main()
{
    Population pop[3];    //Declare 3 Population objects
    
    for(int i=0;i<3;i++)
    {
    //Get the Population

    cout<<"Please enter the population of the City."<<endl;
    int citypop;
    cin>>citypop;
cin.clear();
    pop[i].setPop(citypop);
    
    //Get the annual # of births
    
    cout<<"Please enter the # of births in the last year."<<endl;
    int citybirths;
    cin>>citybirths;
cin.clear();
    pop[i].setBirths(citybirths);
    
    //Get the annual # of Deaths
    cout<<"Please enter the # of deaths in the last year."<<endl;
    int citydeaths;
    cin>>citydeaths;
    pop[i].setDeaths(citydeaths);
    
    //Get the City
    cout<<"Please enter the Name of the City Name."<<endl;
    string currentcity;
   cin>>currentcity;
cin.clear();
    pop[i].setCity(currentcity);

    //Get the State
    cout<<"Please enter the Name of the State."<<endl;
    string currentstate;
    cin >> currentstate;
cin.clear();
    pop[i].setState(currentstate);
    }
 
 //Print information
 cout << "Here is the information for each city."<<endl;
 cout << fixed << showpoint << setprecision(2);
  
for (int j= 0; j < 3; j++)
   {
   cout<<pop[j].getCity()<<" , "<<pop[j].getState()<<endl;
   cout<<"Birthrate : "<<pop[j].getBirthrate()<<endl;
   cout<<"Deathrate : "<<pop[j].getDeathrate()<<endl;
}
          
system ("pause");
}

Last edited on
I have done exactly what you posted and I am still getting skipping errors/printing errors for my strings.

sigh!

thanks for the continued help by the way

how can i use getline for the City / State parts so that if i input a state like "New York" it doesnt only take the "new" part and mess the rest up
Last edited on
Right. That's because cin reads by default up to the first whitespace/newline encountered. So, typing in 'new york' will cause cin to go into a bad state (i know, streams can be frustrating to deal with).

Here's another attempt that uses stringstream to convert characters from the stream into integers. It is much more robust for handling invalid input. As is, cin shouldn't crash if the user types extra spaces or types in characters for numbers (but there's no code to prompt the user for correct input here):
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
int main()
{
	stringstream ss;
	string strConverter;

    Population pop[3];    //Declare 3 Population objects
    
    for(int i=0;i<3;i++)
    {
    //Get the Population

    cout<<"Please enter the population of the City."<<endl;
    int citypop;
	getline(cin, strConverter);
	cin.clear();
	ss << strConverter;
	ss >> citypop;
	ss.clear();

    pop[i].setPop(citypop);
    
    //Get the annual # of births
    
    cout<<"Please enter the # of births in the last year."<<endl;
    int citybirths;
	getline(cin, strConverter);
	cin.clear();
	ss << strConverter;
	ss >> citybirths;
	ss.clear();

    pop[i].setBirths(citybirths);
    
    //Get the annual # of Deaths
    cout<<"Please enter the # of deaths in the last year."<<endl;
    int citydeaths;
	getline(cin, strConverter);
	cin.clear();
	ss << strConverter;
	ss >> citydeaths;
	ss.clear();

    pop[i].setDeaths(citydeaths);
    
    //Get the City
    cout<<"Please enter the Name of the City Name."<<endl;
    string currentcity;
	getline(cin, currentcity);
	cin.clear();

    pop[i].setCity(currentcity);

    //Get the State
    cout<<"Please enter the Name of the State."<<endl;
    string currentstate;
	getline(cin, currentstate);
	cin.clear();
    pop[i].setState(currentstate);
    }
 
 //Print information
 cout << "Here is the information for each city."<<endl;
 cout << fixed << showpoint << setprecision(2);
  
for (int j= 0; j < 3; j++)
   {
   cout<<pop[j].getCity()<<" , "<<pop[j].getState()<<endl;
   cout<<"Birthrate : "<<pop[j].getBirthrate()<<endl;
   cout<<"Deathrate : "<<pop[j].getDeathrate()<<endl;
}
          
system ("pause");
}


birthrate and deathrate still need to be fixed though. I'll leave that for u XD
Last edited on
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
#include <cctype>  // I include these here because you're probably not already including them.
#include <sstream>

unsigned get_unsigned(const std::string& prompt )
{
    for ( ; ; )
    {
        std::cout << prompt << '\n' ;

        std::string input ;
        std::getline(std::cin, input) ;

        if ( input.size() )
        {
            unsigned i ;
            for ( i=0; i<input.size();  ++i )
                if ( !isdigit(input[i]) )
                    break ;

            if ( i == input.size() )
            {
                istringstream num_text(input) ;
                unsigned n ;
                num_text >> n ;
                return n ;
            }
        }
    }
}

std::string get_string( const std::string& prompt )
{
    std::cout << prompt << '\n' ;

    std::string input ;
    std::getline(std::cin, input) ;
    return input ;
}


int main()
{
    Population pop[3];    //Declare 3 Population objects

    for(int i=0;i<3;i++)
    {
        pop[i].setPop(get_unsigned("Please enter the population of the city.")) ;
        pop[i].setBirths(get_unsigned("Please enter the # of births in the last year."));
        pop[i].setDeaths(get_unsigned("Please enter the # of deaths in the last year."));
        pop[i].setCity(get_string("Please enter the name of the city.")) ;
        pop[i].setState(get_string("Please enter the name of the state.")) ;
     }

    //Print information
    cout << "Here is the information for each city."<<endl;
    cout << fixed << showpoint << setprecision(2);

    for (int j= 0; j < 3; j++)
    {
        cout<<pop[j].getCity()<<" , "<<pop[j].getState()<<endl;
        cout<<"Birthrate : "<<pop[j].getBirthrate()<<endl;
        cout<<"Deathrate : "<<pop[j].getDeathrate()<<endl;
    }

    system ("pause");
}
thanks guys! its working perfectly now!

I havent learned this sstream stuff or this unsigned stuff but its good to know about it seems!

thanks again for sticking with me !
Topic archived. No new replies allowed.