| | |
Need some help with putting a looping into a converter program.
Thread Solved |
•
•
Join Date: Jan 2005
Posts: 5
Reputation:
Solved Threads: 0
Hey guys, I need some help with looping. I am writing a currency converter (who in begining programming isn't these days) and I want to have it loop so that I can it keep asking for another entry or make it quit. Here is what I have so far:
C Syntax (Toggle Plain Text)
include <stdio.h> #include <stdlib.h> /* Main Program */ int main(void) { float USD; float CAD; float EUR; float GBP; float CHF; float YEN; int input; char q; /*Title of program*/ printf("\t This program will convert foreign currency to US Dollars\n\n"); /*Assigning Values*/ USD = 1.0; /* US Dollar*/ CAD = 1.2257; /* Canadian Dollar*/ EUR = 0.752162; /* European Euro */ GBP = .51573; /* British Pound */ CHF = 1.134; /* Swiss Franc*/ YEN = 1.038; /* Japanese Yen*/ printf("Please choose form the following list of currency or press q to quit.\n"); printf("\n"); printf("[1] Canadian Dollar\n"); printf("[2] British Pound\n"); printf("[3] European Euro\n"); printf("[4] Swiss Franc\n"); printf("[5] Japanese Yen\n"); printf("\n"); scanf("%d", &input); /*switching statements */ switch (input) { case 1 : printf("%1.6f Canadian Dollars is equal to %1.1f US Dollar.\n", CAD,USD); break; case 2 : printf("%1.6f European Euro is equal to %1.1f US Dollar.\n", EUR,USD); break; case 3 : printf("%1.6f British Pound is equal to %1.1f US Dollar.\n", GBP,USD); break; case 4 : printf("%1.6f Swiss Franc is equal to %1.1f US Dollar.\n", CHF,USD); break; case 5 : printf("%1.6f Japanese Yen is equal to %1.1f US Dollar.\n", YEN,USD); break; } scanf("%d"); return 0; }
Last edited by alc6379; Jan 10th, 2005 at 9:50 pm. Reason: added [code] tags
use a while loop checking if q is pressed. You havent actually used char, and are asking the user to input a letter or number, but only grabbing the number! I would convert to c++ and use iostream, I dont know the C equivalent
C Syntax (Toggle Plain Text)
char input; // user will type, so input is a char! while (input != 'q' || input != 'Q') // loops untill you put q or Q in input { code here... I would use cin >> input; then a strtoi (i think that is how it is spelled) call to convert the char to a number }
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
Unless you want to do precise error handling and throw a message if the user enters something other than q or Q, you can simply place the entire processing code into an infinite loop and break if scanf fails:
That's the easiest solution. If you want something more structured and good error messages, it takes more effort, usually in the form of string input and validation followed by conversions.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> /* Main Program */ int main(void) { float USD; float CAD; float EUR; float GBP; float CHF; float YEN; int input; /*Title of program*/ printf("\t This program will convert foreign currency to US Dollars\n\n"); /*Assigning Values*/ USD = 1.0f; /* US Dollar*/ CAD = 1.2257f; /* Canadian Dollar*/ EUR = 0.752162f; /* European Euro */ GBP = .51573f; /* British Pound */ CHF = 1.134f; /* Swiss Franc*/ YEN = 1.038f; /* Japanese Yen*/ for (;;) { printf("Please choose form the following list of currency or press q to quit.\n"); printf("\n"); printf("[1] Canadian Dollar\n"); printf("[2] British Pound\n"); printf("[3] European Euro\n"); printf("[4] Swiss Franc\n"); printf("[5] Japanese Yen\n"); printf("\n"); if (scanf("%d", &input) != 1) break; /*switching statements */ switch (input) { case 1 : printf("%1.6f Canadian Dollars is equal to %1.1f US Dollar.\n", CAD,USD); break; case 2 : printf("%1.6f European Euro is equal to %1.1f US Dollar.\n", EUR,USD); break; case 3 : printf("%1.6f British Pound is equal to %1.1f US Dollar.\n", GBP,USD); break; case 4 : printf("%1.6f Swiss Franc is equal to %1.1f US Dollar.\n", CHF,USD); break; case 5 : printf("%1.6f Japanese Yen is equal to %1.1f US Dollar.\n", YEN,USD); break; } } return 0; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Simple Guessing Program (C)
- Need help with converter program (C++)
- Roulette program simulation problem (Python)
- putting image in my application program... (C)
- Need Help with functions program (C++)
Other Threads in the C Forum
- Previous Thread: inserting an element into an array in c language
- Next Thread: bubble sorting in an array
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






