944,054 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1969
  • C++ RSS
Oct 12th, 2004
0

program overwritng the 1st input

Expand Post »
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];
}


}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tendekai is offline Offline
7 posts
since Oct 2004
Oct 12th, 2004
0

Re: program overwritng the 1st input

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[i] 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:
C++ Syntax (Toggle Plain Text)
  1. Rows/Columns Column 0 Column 1 Column 2 Column 3
  2. Row 0 myValue[0][0] myValue[0][1] myValue[0][2] myValue[0][3]
  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:
C++ Syntax (Toggle Plain Text)
  1. Rows/Columns Column 0 Column 1 Column 2
  2. Row 0 A ‘\0’ ‘\0
  3. 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:
C++ Syntax (Toggle Plain Text)
  1. cin.getline(country[i], 15);

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


- Stack Overflow
Reputation Points: 26
Solved Threads: 4
Junior Poster
Stack Overflow is offline Offline
185 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help Me Please
Next Thread in C++ Forum Timeline: Number Loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC