Question

Hi, I need to make a program in C that reads the type of currency and...

Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it?

#include <stdio.h>
#include <stdlib.h>
const float POUND = 1.31;
const float YEN = 0.0091;
const float RUPEE = 0.014;
const float EURO = 1.11;
char c;
int currValue;
float exchangeValue;
float finValue;
int printValue;

struct node {
   int value;
   struct node *next;
};

struct node *dollarHead;
struct node *poundHead;
struct node *euroHead;
struct node *yenHead;
struct node *rupeeHead;
struct node *head;
int main(){
   dollarHead = malloc(sizeof(struct node));
   poundHead = malloc(sizeof(struct node));
   euroHead = malloc(sizeof(struct node));
   yenHead = malloc(sizeof(struct node));
   rupeeHead = malloc(sizeof(struct node));
   head = malloc(sizeof(struct node));

   if (head == NULL){
   printf("UHHH ERROR");
} else {
   printf("%x\n", head);
head->value = 36;
head->next = NULL;
printf("h value: %x\n", head->value);
printf("h next: %x\n", head->next);


float currencyExchange() {
   float exchangeRate = 1;
   c = getchar();
   if ( c == '$') {
       printf("$");
       return exchangeRate;
} else if (c =='\xC2') {
   c = getchar();
if (c == '\xA3') {
   printf("£");
   exchangeRate = POUND;
   return exchangeRate;

} else if (c == '\xA5') {
   printf("Â¥");
   exchangeRate= YEN;
   //printf("the exchange rate is %f",exchangeRate);
   return exchangeRate;
   }
   else{
   c = getchar();
   c = getchar();
   if(c== '\xB9') {
   printf("₹");
   exchangeRate = RUPEE;
   return exchangeRate;
   }
   else if (c == '\xAC') {
   printf("€");
   exchangeRate = EURO;
   return exchangeRate;
   }
   else
                   }
                   }
               }
   //

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This problem can be solved by using a double pointer of type node.So that it can hold the currency type pointer and it updates the corresponding pointer.As C doesnot have reference (C supports only pointers where as C++ supports boyh pointers and references),double pointer plays a key role and it can be solved.

struct node** head;

#include <stdio.h>
#include <stdlib.h>
const float POUND = 1.31;
const float YEN = 0.0091;
const float RUPEE = 0.014;
const float EURO = 1.11;
char c;
int currValue;
float exchangeValue;
float finValue;
int printValue;

struct node{
int value;
struct node* next;
};
struct node *dollarHead=NULL;
struct node *poundHead=NULL;
struct node *euroHead=NULL;
struct node *yenHead=NULL;
struct node *rupeeHead=NULL;
struct node *head=NULL;
void insert(float value){
struct node** head;
if(value==POUND)
{
head=&poundHead;
}
else if(value==YEN)
{
head=&yenHead;
}
else if(value==RUPEE)
{
head=&rupeeHead;
}
else if(value==1)
{
head=&dollarHead;
}
else if(value==EURO)
{
head=&euroHead;
}
struct node* temp=(node*) malloc(sizeof(struct node));
printf("Enter value");
scanf("%d",&temp->value);
temp->next=NULL;
if(*head==NULL)
{
*head=temp;
}
else
{
temp->next=*head;
*head=temp;
}
}
void print(float value )
{
struct node** head;
if(value==POUND)
{
head=&poundHead;
}
else if(value==YEN)
{
head=&yenHead;
}
else if(value==RUPEE)
{
head=&rupeeHead
}
else if(value==1)
{
head=&dollarHead
}
else if(value==EURO)
{
head=&euroHead;
}
struct node* temp=*head;
while(temp!=NULL)
{
printf("%d ",temp->value);
temp=temp->next;
}
printf("\n");
}

int main(){
while(1)
{
float rate=currencyExchange();
if(rate==POUND)
{
insertpound(rate);
}
else if(rate==YEN)
{
insert(rate);
}
else if(rate==RUPEE)
{
insert(rate);
}
else if(rate==1)
{
insert(rate);
}
else if(rate==EURO)
{
insert(rate);
}
printf("Do you want to continue...if yes press Y else N");
char s;
scanf("%c",&s);
if(s=='Y' || s=='y')
continue;
else
break;
}
printf("Pound currency ");
print(POUND);
printf("Yen currency ");
print(YEN);
printf("Rupee currency ");
print(RUPEE);
printf("Euro currency ");
print(EURO);
printf("Dollar currency ");
print(1);

}
float exchangeValue(){
    float exchangerate=1;
    c = getchar();
    if ( c == '$') {
       printf("$");
       return exchangeRate;
}
else if (c =='\xC2') {
    c = getchar();
if (c == '\xA3') {
    printf("£");
    exchangeRate = POUND;
    return exchangeRate;
}
else if (c == '\xA5') {
    printf("Â¥");
    exchangeRate= YEN;
   //printf("the exchange rate is %f",exchangeRate);
    return exchangeRate;
    }
   else{
    c = getchar();
    c = getchar();
    if(c== '\xB9') {
    printf("₹");
    exchangeRate = RUPEE;
    return exchangeRate;
   }
   else if (c == '\xAC') {
    printf("€");
    exchangeRate = EURO;
    return exchangeRate;
   }
  
  }
}
}

Add a comment
Know the answer?
Add Answer to:
Hi, I need to make a program in C that reads the type of currency and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • This program uses C++. This program reads in a line from the user and prints it...

    This program uses C++. This program reads in a line from the user and prints it out in a certain format. An example would be Input: 1 2 3 4 5 would result Output: [{1}, {2}, {3}, {4}, {5}]. When quotations marks are added into the input the format becomes different. For instance, Input 1 2 "3 4 5" would result in [{1}, {2}, {3 4 5}]. When I ad multiple quotation marks into the input, it will only use...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • Write a function to insert a name at the end of a linked list? (C) I...

    Write a function to insert a name at the end of a linked list? (C) I have a linked list: struct node { char person[100]; struct node *next; }; int main() { struct node *new_node; new_node=malloc(sizeof(struct node)); printf("Please enter a name:\n"); scanf("%s", ); } How do I get the name from the user and write a function to add it to the end of the linked list? I'm not supposed to use the insert() library function, which is why I'm...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT