# Trouble Understanding Structure Algorithm

This is a discussion on Trouble Understanding Structure Algorithm within the C Programming forums, part of the General Programming Boards category; I'm having trouble setting up a structure. My instructions are &quot;Define the structure dollarsQuartersDimes with three simple variables int dollars, ...

1. ## Trouble Understanding Structure Algorithm

I'm having trouble setting up a structure. My instructions are

"Define the structure dollarsQuartersDimes
with three simple variables int dollars, int quarters and int dimes
to declare monies in dollars, quarters and dimes.

Write a program to
add two dollarsQuartersDimes monies and store the result in the third dollarsQuartersDimes monies.
Remember to make necessary dime and nickels to quarters and quarters to dollar conversions."

Yes that is word for word from my professor.....a foreign professor that cannot communicate in English should have no business trying to communicate a programming language. Anyways...what I got from the assignment description was this :

-Ask user to input amount of Dollars, Quarters and Dimes
-Add the inputted values to previous values in the structure
-Redisplay the updated values

This is the code I have so far :
Code:
```#include <stdio.h>

typedef struct dollarsQuartersDimes_struct {
int dollars;
int quarters;
int dimes;

} dollarsQuartersDimes;

int main(void)

{

int a;

printf("Enter the amount of dollars, quarters, and dimes to insert, separated by spaces> \n");
scanf("%i %i %i", &a.dollars, &a.quarters, &a.dimes);

a.dollars[3] = (a.dollars[1] + a.dollars[2]);
a.quarters[3] = (a.quarters[1] + a.quarters[2]);
a.dimes[3] = (a.dimes[1] + a.dimes[2]);

printf("You currently have %i Dollars, %i Quarters, and %i Dimes.", a.dollars[3], a.quarters[3], a.dimes[3]);

a.dollars[0] = (a.dollars[3]);
a.quarters[0] = (a.quarters[3]);
a.dimes[0] = (a.dimes[3]);

}```
For some reason I feel like I am moving in the wrong direction as I do not think it's possible to set an initial value of a structure. Any suggestions on where to build from?

Thanks.
-Jim

2. What error messages do you get when you try to compile this code?

Jim

3. Interesting... You define an integer 'a', and yet you try to access elements of the dollarsQuartersDimes structure through your integer? Does that make sense to you?

Code:
`int a;`
Try:
Code:
`dollarsQuartersDimes a;`

4. After changing "a" to "dollarsQuartersDimes a;" I received the following error :
Code:
`21 invalid types `int[int]' for array subscript`

5. Originally Posted by JJohnson
This is the code I have so far :
Code:
```#include <stdio.h>

typedef struct dollarsQuartersDimes_struct {
Not needed ^^^^^^^^^^^^^^^^^

int dollars;
int quarters;
int dimes;

} dollarsQuartersDimes;

int main(void)

{

int a;

printf("Enter the amount of dollars, quarters, and dimes to insert, separated by spaces> \n");
scanf("%i %i %i", &a.dollars, &a.quarters, &a.dimes);

Int a has no members^^^^^^^^^^^^^^^^^^ use
dollarsQuartersDimes.dollars, etc.

a.dollars[3] = (a.dollars[1] + a.dollars[2]);
a.quarters[3] = (a.quarters[1] + a.quarters[2]);
a.dimes[3] = (a.dimes[1] + a.dimes[2]);

Same as above. a.dollars doesn't exist - it's just an int.

printf("You currently have %i Dollars, %i Quarters, and %i Dimes.", a.dollars[3], a.quarters[3], a.dimes[3]);

a.dollars[0] = (a.dollars[3]);
a.quarters[0] = (a.quarters[3]);
a.dimes[0] = (a.dimes[3]);

Same as above. Use a name for a struct.member, not a name for an int.member.
}```
And
Welcome to the Forum, Jim!

6. Thanks for the help so far and the forums have been very helpful so far. I have browsed through other people's issue's for help and regret not registering earlier.

Code:
```#include <stdio.h>

typedef struct {
int dollars;
int quarters;
int dimes;

} dollarsQuartersDimes;

int main(void)

{

printf("Enter the amount of dollars, quarters, and dimes to insert, separated by spaces> \n");
scanf("%i %i %i", &dollarsQuartersDimes.dollars, &dollarsQuartersDimes.quarters, &dollarsQuartersDimes.dimes);

dollarsQuartersDimes.dollars[2] = (dollarsQuartersDimes.dollars[0] + dollarsQuartersDimes.dollars[1]);
dollarsQuartersDimes.quarters[2] = (dollarsQuartersDimes.quarters[0] + dollarsQuartersDimes.quarters[1]);
dollarsQuartersDimes.dimes[2] = (dollarsQuartersDimes.dimes[0] + dollarsQuartersDimes.dimes[1]);

printf("You currently have %i Dollars, %i Quarters, and %i Dimes.", a.dollars[2], a.quarters[2], a.dimes[2]);

dollarsQuartersDimes.dollars[0] = (dollarsQuartersDimes.dollars[2]);
dollarsQuartersDimes.quarters[0] = (dollarsQuartersDimes.quarters[2]);
dollarsQuartersDimes.dimes[0] = (dollarsQuartersDimes.dimes[2]);

}```
But program should scan [0] first to get initial value...correct? Or set the value of all [0]s to 0 to begin?

7. Your code is trying to stuff more data into a struct than is possible - you don't have that. You have just ONE instance of the struct, so there is NO DQDimes.dollars[2], etc.

DQDimes.dollars is just ONE integer, same with quarters and dimes in that struct.

I would just assign the struct members to zero, if the compiler has not already done so, when the struct was declared.

Code:
```dollarsQuartersDimes.dimes += a; //or whatever variable or actual
integer number you want added to dimes.```

8. Code:
```#include <stdio.h>

typedef struct {
int dollars;
int quarters;
int dimes;

} dollarsQuartersDimes;

int main(void)

{
int a, b, c;

printf("Enter the amount of dollars, quarters, and dimes to insert, separated by spaces> \n");
scanf("%i %i %i", &dollarsQuartersDimes.dollars, &dollarsQuartersDimes.quarters, &dollarsQuartersDimes.dimes);

dollarsQuartersDimes.dollars += a;
dollarsQuartersDimes.quarters += b;
dollarsQuartersDimes.dimes += c;

printf("You currently have %i Dollars, %i Quarters, and %i Dimes.", a, b, c);

}```
Getting this error :
Code:
`19 expected primary-expression before '.' token`

9. You've created a type called dollarsQuartersDimes, but you've not created an instance of that type.

10. You need to add the code so the user can put a value into variables a, b, and c. and add a return 0; statement, at the very end of the program.

sorry, Jim. You need to declare an instance of the struct. Like this:

Code:
```#include <stdio.h>

typedef struct {
int a;
int b;
char str[20];
} mystruct;   //<== Just a name for this struct, not an instance.

int main() {
int i, n;
mystruct my;  //<== creates the instance of the struct "mystruct"
my.a=10;
my.b=20;
strcpy(my.str, "Unchained Melody");
printf("\n\n%d %d %s", my.a, my.b, my.str);

(void) getchar();
return 0;
}```

11. I think I'm getting closer....however, if I try to insert the following "2 3 4" into my following code, it prints the following, "You currently have 4140946 Dollars, 41232475 Quarters, and 2686780 Dimes." This happens after initially running the program. Why are the value's so off?

Code:
```#include <stdio.h>
#include<string.h>    /* string definitions */

typedef struct {
int dollars;
int quarters;
int dimes;

} dollarsQuartersDimes;

int main(void)

{
const int x=1;
dollarsQuartersDimes a;
dollarsQuartersDimes b;
dollarsQuartersDimes c;

do{
printf("Enter the amount of dollars, quarters, and dimes to insert, separated by spaces> \n");
scanf("%i %i %i", &a.dollars, &a.quarters, &a.dimes);

/* Calculate new amount */
c.dollars = a.dollars + b.dollars;
c.quarters = a.quarters + b.quarters;
c.dimes = a.dimes + b.dimes;
printf("You currently have %i Dollars, %i Quarters, and %i Dimes.\n\n", c.dollars, c.quarters, c.dimes);

/* Store inputted user values to structure 2 */
b.dollars += a.dollars;
b.quarters += a.quarters;
b.dimes += a.dimes;

/* Set first structure values to 0 */
a.dollars = 0;
a.quarters = 0;
a.dimes = 0;

} while ( x == 1 );

return 0;
}```
Thanks,
-Jim

12. The values are so off because you are using structure b without setting the variables to start with because of this the values start at arbitrary values, hence why you are getting useless output. My suggestion is to initialize the variables of each structure before you use them.

13. I have it working (so far) after setting each value to 0 before main(). I am now working on the if statement to calculate the number of dollars of multiple inputted quarters and dimes. What would be the best way to do this?

Code:
```#include <stdio.h>
#include<string.h>    /* string definitions */

typedef struct {
int dollars;
int quarters;
int dimes;

} dollarsQuartersDimes;

int main(void)

{
const int x=1;
dollarsQuartersDimes a;
dollarsQuartersDimes b;
dollarsQuartersDimes c;

/* Set all values of structures to 0 */
a.dollars = 0;
a.quarters = 0;
a.dimes = 0;
b.dollars = 0;
b.quarters = 0;
b.dimes = 0;
c.dollars = 0;
c.quarters = 0;
c.dimes = 0;

do{
printf("Enter the amount of dollars, quarters, and dimes to insert, separated by spaces> \n");
scanf("%i %i %i", &a.dollars, &a.quarters, &a.dimes);

/* Calculate new amount */
c.dollars = a.dollars + b.dollars;
c.quarters = a.quarters + b.quarters;
c.dimes = a.dimes + b.dimes;
if ( c.quarters > 4 )

printf("You currently have %i Dollars, %i Quarters, and %i Dimes.\n\n", c.dollars, c.quarters, c.dimes);

/* Store inputted user values to structure 2 */
b.dollars += a.dollars;
b.quarters += a.quarters;
b.dimes += a.dimes;

/* Set first structure values to 0 */
a.dollars = 0;
a.quarters = 0;
a.dimes = 0;

} while ( x == 1 );

return 0;
}```
The only thing I can think of is :

-Multiplying [(# of quarters) * (25)] / 100
-Then take left part of decimal, place in dollars
-Then take right part of decimal, times by one hundred and divide by 25

Thanks,
-Jim

14. Well the function modf() will separate the fractional part and whole number part of a real number. Have you thought about working with pennies though? A dollar is just 100 of those and so on. Your math would be more accurate. You do not need the precision.

15. St. Alowichius preserve us!

As I understand it, the user will put money into both stucts, then your program will add them together and sum up everything, in the third struct, with the fewest coins.

So you need to get input of coins for the second struct. Natually, I'm suggesting a for loop here, to be used for input into both structs.

Code:
```for(i=0;i<2; i++)
if(i==0) {
add coins and bills to struct a

}
else {
add coins and bills to struct b
}```
now add them all together: allbills = a.bills +b.bills , allquarters = a.quarters + b.quarters, etc.

Now translate all denominations into pennies: (just go with me a sec)
pseudo code:
Code:
```  allpennies = allbills * 100
allpennies = allquarters * 25
etc.

now
c.bills = allpennies / 100
allpenies /= 100;
c.quarters = allquarters / 25
allpennies /= 25
etc.```
So first, get it all together into one pile of pennies, and then find out how much of each denomination you need, by going from large value, to small. With integer division in C, you never need to worry about the decimal place using pennies, until your final print out:
\$25.40, kind of display .