| | |
program overwritng the 1st input
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 7
Reputation:
Solved Threads: 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];
}
}
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];
}
}
Greetings,
Firstly, there are a few syntax problems with your provided source.
nor Has a semicolon, which could cause the compiler to result in errors.
Secondly: 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: 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: It would appear to be as: In the 2-D realm. So if we had: It would look like: To simply fix the code, we could change country to: 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:
If you have further questions, please feel free to ask.
- Stack Overflow
Firstly, there are a few syntax problems with your provided source.
int pop[20]int growth[20]Secondly:
for (i=o;i<=19;i++)
Thirdly, calling country[i] can cause your program to fail, as it is an array of characters. For example:
char word[6] = "Hello";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]; C++ Syntax (Toggle Plain Text)
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]
char letters[2][3] = { {‘A’}, {‘D’, ‘E’} }; C++ Syntax (Toggle Plain Text)
Rows/Columns Column 0 Column 1 Column 2 Row 0 A ‘\0’ ‘\0’ Row 1 D E ‘\0’
char country[20][15];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)
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Similar Threads
- Program Keeps reading wrong input (C)
- Help with Java with pay roll program pt 3 (Java)
- help with program using structures and printing to screen (C)
- Program does not recognize actual input = expected output (C++)
- Receiving user input to direct program (C++)
- how to append input at the end of the old data in a text file? (C++)
Other Threads in the C++ Forum
- Previous Thread: Help Me Please
- Next Thread: I need help with this c++ program please!!!
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





