Solved

C Struct & Pointer Code

Posted on 2001-08-08
7
293 Views
Last Modified: 2010-05-18
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!!!
0
Comment
Question by:Dargie
  • 4
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
Aggarwal earned 100 total points
ID: 6366686
Change this function from ->

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;
}

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: ");
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);
}

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

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

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

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


accordignly change while loop to look like this ..



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);
    i++ ;
    break;
   case 2:  
    printf("You entered 2");
    break;
   case 3:
    printf("You entered 3");
    break;
   case 4:
    listAllCountries(pCountry, i);
    break;
  }



0
 
LVL 1

Expert Comment

by:Aggarwal
ID: 6366689
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 !!
0
 
LVL 6

Expert Comment

by:kotan
ID: 6366915
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:Aggarwal
ID: 6366959
line
    addCountry(pCountry+i);
>>   by Aggarwal is not correct. It's meaningless adding >> pointer and integer.


..Why it is meaningless ..can you please explain ????
0
 
LVL 1

Expert Comment

by:Aggarwal
ID: 6366964
infact it will produce the same effect as that of

addCountry( &pCountry[i] )..

0
 
LVL 6

Expert Comment

by:kotan
ID: 6366976
Sorry Aggarwal.
You are right.
You should get the points.
0
 

Author Comment

by:Dargie
ID: 6367948
Thank you Very Much Aggarwal!!!!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stack 22 156
How to Deploy a Windows C++ Application With Libraries 9 136
Finding a good hash function 4 116
gdb doesn't stop on breakpoint 2 7
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now