Link to home
Start Free TrialLog in
Avatar of Dargie
Dargie

asked on

C Struct & Pointer Code

100 Points if someone can help me with assignment:

The purpose of this assignment is for you to practice and understand how structs are used to store information about objects. We are going to store information about countries in our programs. There are 5 pieces of information about countries that we are interested in. They are:
Name    Continent   Population   Area   GDP
For example:
Name: Japan
Continent: Asia
Population: 125 million
Area: 378 KmSq
GDP: $1500 billion

Write a program that allows the user to add, edit and list information about countries. Your program should start with providing the user with a menu that consists of the following options:
 
Add country
Edit country
List one country
List all countries
Quit
 

Option 1:

When user chooses option 1, the program should ask the user the necessary information about the country that is being added and then add that country to the list of countries. For example, if the user chooses option 1, your program might continue like this:

Enter the following information about the country that you want to add:

Name:             Mexico           ?        User enters

Continent:       America

Population:     100    

Area:               1958

GDP:              155

Then the program should add the country to the list.

Note:

When the user enters the name of the country to be added, the program should check to see if that country already exist in the list and then warn the user about it and stop the adding process.

Option 2:

When user chooses option 2, the program should ask the user the name of the country that s/he wants to edit. Then it should search the list and find the country and print the existing information on the screen for the user. Then it should ask the user to reenter the information again. After all the data is entered, the program should update information of the country in the list.

Note:

When editing, the user should not be allowed to change the name of the country; everything else can be changed by the user.

Option 3:

When option 3 is chosen, the program should ask for the name of the country that needs to be listed. Then it should find the country in the list and print its data on the screen with a proper format.

Note:

The program should warn the user if the country was not found in the list.

Option 4:

When user chooses option 4, the program should list all the countries in a nice and easy to read format.

After each operation is completed, the menu should be printed for the user, unless quit is chosen.

Option 5
 
Option 5 should cause the program to terminate.

Assume that population, area and GDP are whole numbers (i.e. they have no floating points).

Assume your list can store at most 200 countries.

Here is my code thus far:

#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE 200

/**************************************
Structure Prototype
**************************************/

typedef struct {
 char name[25];
 char continent[25];
 char population[25];
 char area[25];
 char gdp[25];
} Country;

/**********************************
addCountry Function Prototype
**********************************/

void addCountry (Country *pCountry, int *pI);

/************************************
listAllCountries Function Prototype
************************************/

void listAllCountries (Country *pCountry, int *pI);

main(){
int intOption = 0;
char chrOption[2];
int i = 0, *pI;

Country country[ARRAY_SIZE], *pCountry;
pCountry = &country[i];
pI = &i;

 while(1){
  puts("\nPlease select an option from the list below:\n");
  printf("\n The Value of I is %d\n", i);
  puts("1. Add Country");
  puts("2. Edit Country");
  puts("3. List One Country");
  puts("4. List all Countries");
  puts("5. Quit\n");

  gets(chrOption);

  intOption = atoi(chrOption);

   if (intOption == 0 || intOption > 5)
    puts("\nInvalid Option...Please try Again");

   if (intOption == 5)
    break;

   switch (intOption) {
    case 1:
     addCountry(pCountry, &i);
     break;
    case 2:  
     printf("You entered 2");
     break;
    case 3:
     printf("You entered 3");
     break;
    case 4:
     listAllCountries(pCountry, &i);
     break;
   }


 }
}

/**********************************
addCountry Function Definition
**********************************/

void addCountry (Country *pCountry, int *pI){
system("clear");
puts("\nPlease Enter the Information Below: \n");

printf("Name: ");
gets(pCountry->name);
printf("\n%s", pCountry->name);

printf("Continent: ");
gets(pCountry->continent);
printf("\n%s", pCountry->continent);

printf("Population: ");
gets(pCountry->population);
printf("%s", pCountry->population);

printf("Area: ");
gets(pCountry->area);
printf("%s", pCountry->area);

printf("GDP: ");
gets(pCountry->gdp);
printf("%s", pCountry->gdp);

++*pI;
++pCountry;
}

/**************************************
listAllCountries Function Definition
***************************************/

void listAllCountries (Country *pCountry, int *pI){
int j = 0;

 for (j = 0; j < *pI; j++){
  printf("\nThe country is %s\n", pCountry->name);
  ++pCountry;
 }
}

I am new to C programming and need all the help I can get!!!
ASKER CERTIFIED SOLUTION
Avatar of Aggarwal
Aggarwal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aggarwal
Aggarwal

Post , in case u are having any problems with this now !!!

There were following problems with your code.

1. Always passing the same pointer pCountry to the addCountry ..

Because of this , there will be only last entry stored in the list and i will have the number of coutries entered ..

so , printing will give one valid country ( i.e. last entered ) and remaining junk !!!

hope this helps !!

Dun forget to change the prototypes as well !!
There are some comments I want to add.

1. According to your requirement of assignment, population, area and GDP should be a Number not string. So you should declare your struct as
     typedef struct {
                   char name[25];
                   char continent[25];
                   int population;
                   int area;
                   int gdp;
        } Country;
Assume int is enough. Otherwise, you can use long instead.
 
2. The variable *pI, *pCountry are not necessary.
   Below is my way,

   int index = 0; // This is same as i you declare and is a number of country in array.
   Country country[ARRAY_SIZE];

    while (1) {
     ...
     // same coding.
     switch (intOption) {
            case 1:
               addCountry(&country[index]);
            index++;
               break;
            case 2:  
               printf("You entered 2");
               break;
            case 3:
               printf("You entered 3");
               break;
            case 4:
               listAllCountries(Country, index);
               break;
        }
    }

    The line
     addCountry(pCountry+i);
    by Aggarwal is not correct. It's meaningless adding pointer and integer.
   
    Notis that, I have changed the calling function for case 4.
    So the prototype of the 2 functions are
     addCountry(Country*);
     listAllCountries(Country[], int);    

3. In the addCountry:
   I've changed to

   void addCountry (Country *pCountry) {
        system("clear");
        puts("\nPlease Enter the Information Below: \n");

        printf("Name: ");
        gets(pCountry->name);
        printf("\n%s", pCountry->name);

        printf("Continent: ");
        gets(pCountry->continent);
        printf("\n%s", pCountry->continent);

        printf("Population: ");
     scanf("%d", &(pCountry->population));
//        gets(pCountry->population);
//        printf("%s", pCountry->population);

        printf("Area: ");
        scanf("%d", &(pCountry->area));
//        gets(pCountry->area);
//        printf("%s", pCountry->area);

        printf("GDP: ");
     scanf("%d", &(pCountry->gdp));
//        gets(pCountry->gdp);
//        printf("%s", pCountry->gdp);
    }

     Just replaced gets to scanf for integer variable.

4. For the listAllCountries() function,
   What Aggarwal done is correct, you don't have to pass the addrress of i because you are not going to change it.

good luck!

Regards,
kotan
line
    addCountry(pCountry+i);
>>   by Aggarwal is not correct. It's meaningless adding >> pointer and integer.


..Why it is meaningless ..can you please explain ????
infact it will produce the same effect as that of

addCountry( &pCountry[i] )..

Sorry Aggarwal.
You are right.
You should get the points.
Avatar of Dargie

ASKER

Thank you Very Much Aggarwal!!!!