0

Anyone out there pliz help me.am new to c++ and the program am supposed to write has to accept

  1. 19 country names
  2. there populations
  3. growth const

and i have to use a formula (population*growth const) to find the growth of the country.
dispalay sholud be like as below.

country_name   population   growth_const   growth

here is what i have tried

#include<iostream.h>
#include<conio.h>

char country[20];
int grth_const[20];
int pop[20]
int i;
int growth[20]

void main()

{
clrscr();

for (i=o;i<=19;i++)

    {




cout<<"Enter country name";

cin>>country[i];                                 //the country name is supposed to be stored into an array of countries since ther are loads to be entered

cout<<"Enter population";                        //this is the population of the countries above

cin>>pop[i];

cout<<"enter growth rate";

cin>>grth_const[i];

growth[i]=pop[i]*grth_const[i];

    }
for(int b=0;b<=19;b++)
{
cout<<country[i]<<pop[i]<<grth_const[i]<<growth[i];
}


}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
13 Years
Discussion Span
Last Post by Stack Overflow
0

Greetings,

Firstly, there are a few syntax problems with your provided source.

int pop[20]

nor

int growth[20]

Has a semicolon, which could cause the compiler to result in errors.

Secondly:

for (i=o;i<=19;i++)

may cause a problem as o and 0 are two different things.

Thirdly, calling country can cause your program to fail, as it is an array of characters. For example:

char word[6] = "Hello";

Would be interpereted as word[0] is 'H', word[1] is 'e', word[2] is 'l', and so forth. So we will never really get our full country name per loop around. We can fix this though by changing it to a two dimensional array.

Q. How do those work?
A. Like any two dimensional realm; rows and colums.

For a quick example of how this would work, would be presented as the following:

For instance, take:

int myValue[2][3];

It would appear to be as:

Rows/Columns	Column 0	Column 1	Column 2	Column 3
Row 0		myValue[0][0]	myValue[0][1]	myValue[0][2]	myValue[0][3]
Row 1		myValue[1][0]	myValue[1][1]	myValue[1][2]	myValue[1][3]

In the 2-D realm. So if we had:

char letters[2][3] = { {‘A’}, {‘D’, ‘E’} };

It would look like:

Rows/Columns	Column 0	Column 1	Column 2
Row 0		A		‘\0’		‘\0’
Row 1		D		E		‘\0’

To simply fix the code, we could change country to:

char country[20][15];

Which contains 20 rows, with 15 letters maximum each.

Fourthly, using "cin >>" to input into the stream is very dangerous, especially when dealing with a limited space character array. You could overflow the arrays boundaries as "cin >>" does not support buffer overflow protection. In a more safer environment, we can use getline() as a substitute:

cin.getline(country[i], 15);

If you have further questions, please feel free to ask.


- Stack Overflow

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.