Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

struct problem

Status
Not open for further replies.

vwrice

Instructor
Jan 20, 2004
3
US
I am having a problem either initializing this array or getting to its contents.

What is wrong here?/*Body of program*/

#include<stdio.h>
#include <stdlib.h>
#include <string.h>


/*Function Library*/
/* global variables */



int main(void)

{
/* Start Program */
float amount, USDollarFromAmount, USDollars; /* variable name */
int success = 0;
int status, i, x, num, selection;


/* Currency rates as of 1/19/04 */
struct Currency_rec
{
char cname[15];
float camount;
};
struct Currency_rec country[5] =
{
{&quot;Euro&quot;, 0.81},
{&quot;Swiss Franc&quot;, 1.27 },
{&quot;Mexican Peso&quot;, 10.80 },
{&quot;British Pound&quot;, 0.56 },
{&quot;Japanese Yen&quot;, 107.00 }
};


/* Printing Title - I added an extra line break */
/*cls*/;
printf(&quot;\nForeign Currency Conversion Program\n&quot;);
printf(&quot;===================================\n\n &quot;);

/*Assigning constants to variable names*/

/* Display Header */
printf(&quot;Selection: Currency:\n&quot;);
printf(&quot;----------------------\n&quot;);
for (i = 0; i < 5; ++i)
{
/* num is one more that rthe offset to display option numbers */
num = i + 1;
/*Display the option number and the country name */
fflush(stdin);
printf(&quot;%d. %s\n&quot;,num , country.cname);
}
num++;
printf(&quot;%d. Exit&quot;,num);



/* Print Termination message */
printf (&quot;\nProgram terminated!&quot;);
getchar();
return 0;
}
The output is as follows:

C:\WINDOWS\HIMEM.SYS
Foreign Currency Conversion Program
===================================

Selection: Currency:
----------------------
1. ÖÖÖÖÖ%@8
2.
3.
4.
5.
6. Exit
Program terminated!


what am I doing wrong?

 
Your statement:
Code:
printf(&quot;%d. %s\n&quot;,num , country.cname);
Should be:
Code:
printf(&quot;%d. %s\n&quot;,num , country[i].cname);

Good Luck.
 
Sorry,

I sent the wrong source code. This is the out put from that statement:

C:\WINDOWS\HIMEM.SYS
Foreign Currency Conversion Program
===================================

Selection: Currency:
----------------------
1. ?
2. ÖÖÖÖÖ%@8
3.
4.
5.
6. Exit
Program terminated!

the statement was the same as yours but same output.


 
OK, here's the source code I have with your comments stripped out & with
Code:
country[i].cname
. It compiles and executes as expected.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)

{
  float amount, USDollarFromAmount, USDollars;  
  int success = 0;
  int status, i, x, num;


  struct Currency_rec
  {
    char cname[15];
    float camount;
  };

  struct Currency_rec country[5] =
  {
    {&quot;Euro&quot;, 0.81},
    {&quot;Swiss Franc&quot;, 1.27 },
    {&quot;Mexican Peso&quot;, 10.80 },
    {&quot;British Pound&quot;, 0.56 },
    {&quot;Japanese Yen&quot;, 107.00 }
  };

  printf(&quot;\nForeign Currency Conversion Program\n&quot;);
  printf(&quot;===================================\n\n &quot;);
  printf(&quot;Selection:   Currency:\n&quot;);
  printf(&quot;----------------------\n&quot;);
  for (i = 0; i < 5; ++i)
  {
    num = i + 1;
    printf(&quot;%d. %s\n&quot;,num , country[i].cname);
  }
  num++;
  printf(&quot;%d. Exit&quot;,num);

  printf (&quot;\nProgram terminated!&quot;);
  getchar();

  return 0;
}


Output:
Code:
Foreign Currency Conversion Program
===================================

 Selection:   Currency:
----------------------
1. Euro
2. Swiss Franc
3. Mexican Peso
4. British Pound
5. Japanese Yen
6. Exit
Program terminated!

Does this look similar to your code,or am I missing something?
 
This is the same code that I have. It must be the compiler. I am using Miracle C. Do you know of another compiler that I can use?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top