program overwritng the 1st input

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 7
Reputation: tendekai is an unknown quantity at this point 
Solved Threads: 0
tendekai tendekai is offline Offline
Newbie Poster

program overwritng the 1st input

 
0
  #1
Oct 12th, 2004
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];
}


}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: program overwritng the 1st input

 
0
  #2
Oct 12th, 2004
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:
  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:
  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:
  1. cin.getline(country[i], 15);

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


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC