Bitte um kleine Hilfe
-
Guten Abend,
ich habe einen Code in C-Sprache geschrieben fuer meinen Unterricht und komme mit ein oder zwei befehlen nicht klar.
#include <stdio.h> #include <ctype.h> int main() { float Eur = 0.793777, CHF = 1.23850, GBP = 0.558036, JPY = 106.860, SAR = 3.75010;/*Declaring constant value*/ float result1,result2,result3,result4,result5, amount;/* Declaring variable*/ char input;/* Declaring variable*/ /* Title of Program */ printf("\t CURRENCY CONVERSION PROGRAM\n\n"); /* Output Statements */ printf("\t\t 1. Europe\n"); /* Displays currency name*/ printf("\t\t 2. Switzerland\n"); /* Displays currency name*/ printf("\t\t 3. England\n"); /* Displays currency name*/ printf("\t\t 4. Japan\n"); /* Displays currency name*/ printf("\t\t 5. Saudi Arabia\n\n"); /* Displays currency name*/ printf("\t Select country by pressing 1-5 or press (q) to exit\n\n"); scanf("%c", &input);/*Asks user input*/ tolower(input); while ( input != 'q') { if (isdigit(input)) { printf("\b Wrong entry, try again...\n"); } if(input == '1') { printf("\b You have selected Europe\n"); printf("\b Enter Euro amount to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result1 = Eur * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result1); } else if(input == '2') { printf("\b You have selected Switzerland\n"); printf("\b Enter amount of Swiss Francs to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result2 = CHF * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result2); } else if(input == '3') { printf("\b You have selected England\n"); printf("\b Enter amount of British Pounds to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result3 = GBP * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result3); } else if(input == '4') { printf("\b You have selected Japan\n"); printf("\b Enter amount of Japanese Yens to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result4 = JPY * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result4); } else if(input == '5') { printf("\b You have selected Saudi Arabia\n"); printf("\b Enter amount of Saudi arabia Riyals to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result5 = SAR * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result5); } fflush(stdin); printf("\t Select country by pressing 1-5 or press (q) to exit\n\n"); scanf("%c", &input);/*Asks user input*/ } exit(1); getchar(); /* Allows statements to be displayed on screen */ return 0; } /***********************End of Currency Conversion Program******************************/
Wenn das Programm fraegt das man ein Land auswaehlen soll nimmt er den Befehl an... Aber bringt gleichzeitig die Fehlermeldung die ich Defeniert habe.
Desweiteren bei Eingabe eines Betrags. Wenn eine Zahl eingegeben wird konvertiert er es ohne Probleme. Bei eingabe eines Buchstaben ebenfalls
Seht euch doch bitte den Code mal an und weist mich auf meine Fehler hin.
Vielen Dank
schoenen Abend
Daniel
-
aber bringt gleichzeitig die Fehlermeldung die ich Defeniert habe.
du testes ja auch auf isdigit()... vielleicht meintest du !isdigit()?
-
du hast viel redundanz im programm. die ganze ein/ausgabe und berechnung kann getrennt von der auswahl des kurses passieren.
du brauchst auch nicht result1 bis 5.
edit: tolower() funktioniert nicht so.
-
@ DrGreenthumb - Danke für die Info. Funktioniert nun
@ c.rackwitz - Dein Rat wurde befolgt und Code wurde ein wenig "aufgeräumt"
Hier nun mein Update des Codes
#include <stdio.h> #include <ctype.h> int main() { float Eur = 0.793777, CHF = 1.23850, GBP = 0.558036, JPY = 106.860, SAR = 3.75010;/*Declaring constant value*/ float result[6], amount;/* Declaring variable*/ char input;/* Declaring variable*/ /* Title of Program */ printf("\t CURRENCY CONVERSION PROGRAM\n\n"); /* Output Statements */ printf("\t\t 1. Europe\n"); /* Displays currency name*/ printf("\t\t 2. Switzerland\n"); /* Displays currency name*/ printf("\t\t 3. England\n"); /* Displays currency name*/ printf("\t\t 4. Japan\n"); /* Displays currency name*/ printf("\t\t 5. Saudi Arabia\n\n"); /* Displays currency name*/ printf("\t Select country by pressing 1-5 or press (q) to exit\n\n"); scanf("%c", &input);/*Asks user input*/ while ( input != 'q') { if (!isdigit(input)) { printf("\b Wrong entry, try again...\n"); } if(input == '1') { printf("\b You have selected Europe\n"); printf("\b Enter Euro amount to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result[1] = Eur * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result[1]); } else if(input == '2') { printf("\b You have selected Switzerland\n"); printf("\b Enter amount of Swiss Francs to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result[2] = CHF * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result[2]); } else if(input == '3') { printf("\b You have selected England\n"); printf("\b Enter amount of British Pounds to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result[3] = GBP * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result[3]); } else if(input == '4') { printf("\b You have selected Japan\n"); printf("\b Enter amount of Japanese Yens to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result[4] = JPY * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result[4]); } else if(input == '5') { printf("\b You have selected Saudi Arabia\n"); printf("\b Enter amount of Saudi arabia Riyals to convert and press enter:\n\n"); scanf("%f", &amount);/*Asks user input*/ result[5] = SAR * amount;/*Harcoded equation*/ printf("The transaction will provide you with: %1.2f USD\n\n", result[5]); } fflush(stdin); printf("\t Select country by pressing 1-5 or press (q) to exit\n\n"); scanf("%c", &input);/*Asks user input*/ } exit(1); getchar(); /* Allows statements to be displayed on screen */ return 0; } /***********************End of Currency Conversion Program******************************/
Nun das Problem bei der Eingabe des Betrags... Wie kann man die Eingabe testen um keine Buchstaben zuzulassen?
Schönen Abend
Daniel[ edit by c.rackwitz: [cpp] tags ]
-
DanielHitt schrieb:
@ c.rackwitz - Dein Rat wurde befolgt und Code wurde ein wenig "aufgeräumt"
du machst dich ueber mich lustig und das find ich doof.
- nimm die redundanzen raus (result ist kein array, ein/ausgabe der kurse + berechnung des wechselwertes gehoert getrennt von der auswahl des kursfaktors)
- nach nem exit() wird nichs mehr ausgefuehrt
- exit() ist im programm total ueberfluessig. ein "return 0" allein reicht
- int main(void) heisst das, wir sind ja hier in C und nicht in C++
- statt scanf(%c) geht auch getchar()
- fflush(stdin) gibts nur beim ms compiler. mit jedem anderen compiler stuerzt dein programm ab
- "Select country ..." + folgendes scanf() gehoert IN die schleife und damit auch nur einmal ins gesamte programm
- deine einrueckung [1] [2], zeichensetzung und verwendung von leerzeilen ist aeusserst inkonsistent. ja, das ist wichtig, denn code wird gelesen.- benutze [cpp] tags fuer deinen code, das schaltet syntax highlighting ein
[1] http://de.wikipedia.org/wiki/Einrückungsstil
[2] http://en.wikipedia.org/wiki/Indent_style
-
Kleine Hilfe? Keine Ursache.
#include <conio.h> #include <stdio.h> struct tWaehrung { char *Land; float Kurs; char *Name; char *Bezeichnung; }; struct tWaehrung Waehrung[] = { { "Europe", 0.793777, "Euro", "EUR" }, { "Switzerland", 1.23850, "Swiss Franc", "CHF" }, { "England", 0.558036, "British Pound", "GBP" }, { "Japan", 106.860, "Japanese Yen", "JPY" }, { "Saudi Arabia", 3.75010, "Saudi Arabia Riyal","SAR" } }; int main() { float result, amount; char input; while(1) { clrscr(); printf("CURRENCY CONVERSION PROGRAM TO US-DOLLAR\n"); printf("\n"); for (input = '1'; input <= '5'; input++) printf("%c. %s\n", input, Waehrung[input-'1'].Land); printf("\n"); printf(" Select country by pressing 1-5 or press (q) to exit\n"); printf("\n"); input = getch(); if (input == 'q') break; if (input < '1' || '5' < input) continue; printf("You have selected %s\n", Waehrung[input-'1'].Land); printf("\n"); printf("Enter %ss amount (%s) to convert and press enter:\n", Waehrung[input-'1'].Name, Waehrung[input-'1'].Bezeichnung); scanf("%f", &amount); result = Waehrung[input-'1'].Kurs * amount; printf("The transaction will provide you with: %1.2f USD\n", result); printf("\n"); printf("\n"); printf("PRESS ANY KEY"); getch(); } return(0); }
-
kek, du bist zu freundlich fuer diese welt. ich hatte eigentlich vor, DanielHitt etwas schwitzen zu lassen, auf dass er die lektion ins hirn gebrannt bekommt. prima code.
edit: aber nicht freundlich genug, um clrscr() und getch() zu verdammen...
-
@ c.rackwitz - es lag nicht in meiner Absicht mich über Dich lustig zu machen. sorry wenn das so rüberkommt. Ich bin relativ neu in diesem Gebiet... Kenn mich also nicht so gut aus. Ich bin also sehr am schwitzen =). Für Deine Hinweise bin ich auch sehr Dankbar.
@ keksekekse - Danke für die Hilfe mit diesem Code. Nun habe ich einen vergleich mit meinem geschriebenen.
-
c.rackwitz schrieb:
kek, du bist zu freundlich fuer diese welt.
... und zu viel Zeit.