I've made a program that is a cheque generator. Whatever amount you input in the scanf will output in words. For example if I were to to input "1234.56" My output will be "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents", or if I wanted to input the amount "0.01" the output will be "Zero Dollars and ... 1 Cents". The program works perfectly, however there is a minor issue, if I wanted to input the amount "9909" it will output "Nine Thousand Nine Hundred Ninety Nine Dollars and ... 0 Cents". The output should be "Nine Thousand Nine Hundred Nine Dollars and ... 0 Cents". Please help!
The code is as follows:
#include <stdio.h>
void printNum(int);
void printNum2(int);
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int num = 0;
int printcents; //To convert the float "cents" to an integer.
float inclusive;
float cents;
printf("Welcome to the IPC144 Cheque Generator!!\n");
printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
scanf("%f", &inclusive);
if(inclusive < 0.00 || inclusive >= 10000.00) {
printf("Sorry, cannot create cheque for that amount, try again next time!\n");
}
else
{
a = inclusive / 1000; //This data is replacing our variable by diving whatever the vaulue is by either 1000, 100, 10.
inclusive = inclusive - (a*1000);
b = inclusive / 100;
inclusive = inclusive - (b*100);
if ( inclusive > 19 ){
c = inclusive / 10;
inclusive = inclusive - (c*10);
}
else
{
c = inclusive;
d = 0;
}
d = inclusive;
num = inclusive;
cents = (inclusive - num)*100; //To calculate our "Cents" with numerals.
printcents = cents;
/*Printing if the variables are in the thousands, hundreds, tens or ones categories.*/
if (c == 0 && b == 0 && a == 0){
printf("Zero Dollars and ... %d Cents\n", printcents);
}
else{
if (a > 0){
printNum(a);
printf("Thousand ");
}
if (b > 0){
printNum(b);
printf("Hundred ");
}
printNum2(c);
if (d >= 0){
printNum(d);
printf("Dollars and ... ");
}
printf("%d", printcents);
printf(" Cents\n");
}
}
}
void printNum(int x) //Created functions to easily output various if statements.
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
void printNum2(int x)
{
if ( x == 10)
printf("Ten ");
else if ( x == 11)
printf("Eleven ");
else if ( x == 12)
printf("Twelve ");
else if ( x == 13)
printf("Thirteen ");
else if (x == 14)
printf("Fourteen ");
else if (x == 15)
printf("Fifteen ");
else if (x == 16)
printf("Sixteen ");
else if (x == 17)
printf("Seventeen ");
else if (x == 18)
printf("Eighteen ");
else if (x == 19)
printf("Nineteen ");
else if (x == 2)
printf("Twenty ");
else if (x == 3)
printf("Thirty ");
else if (x == 4)
printf("Forty ");
else if (x == 5)
printf("Fifty ");
else if (x == 6)
printf("Sixty ");
else if (x == 7)
printf("Seventy ");
else if (x == 8)
printf("Eighty ");
else if (x == 9)
printf("Ninety ");
}
I have been coding for a month now so I apologize for the simple errors.
After you have calculated b and adjusted inclusive so it is now 9, you get to your else case, which sets c to 9 (hence the Ninety output) and d to 0, but immediately afterwards sets d to 9 (hence the Nine in the output). I think you mixed up c and d in the else case.
Related
I wrote a program that will ask for an integer input (1 - 9999) and will convert the inputted integer into its corresponding word format in English.
And I'm trying to modify it so that switch statements will be used instead of if-statements (where it is applicable).
Example 1:
Input number: 2481
Output: two thousand four hundred eighty one
Here is my code:
#include<stdio.h>
#include<conio.h>
main()
{
int num,thousands,hundreds,tens,ones;
printf("Enter number (1-9999): ");
scanf("%d",&num);
//&& - check if within the range
//|| - check if outside of the range
if (num < 1 || num > 9999)
printf("Invalid number.");
else
//if (num > 0 && num < 10000)
{
thousands = num / 1000;
hundreds = num % 1000 / 100;
tens = num % 1000 % 100 / 10;
ones = num % 1000 % 100 % 10;
//if (num / 1000 == 1)
if(thousands == 1)
printf("one thousand ");
if(thousands == 2)
printf("two thousand ");
if(thousands == 3)
printf("three thousand ");
if(thousands == 4)
printf("four thousand ");
if(thousands == 5)
printf("five thousand ");
if(thousands == 6)
printf("six thousand ");
if(thousands == 7)
printf("seven thousand ");
if(thousands == 8)
printf("eight thousand ");
if(thousands == 9)
printf("nine thousand ");
//if (num % 1000 / 100 == 1)
if(hundreds == 1)
printf("one hundred ");
if(hundreds == 2)
printf("two hundred ");
if(hundreds == 3)
printf("three hundred ");
if(hundreds == 4)
printf("four hundred ");
if(hundreds == 5)
printf("five hundred ");
if(hundreds == 6)
printf("six hundred ");
if(hundreds == 7)
printf("seven hundred ");
if(hundreds == 8)
printf("eight hundred ");
if(hundreds == 9)
printf("nine hundred ");
//if (num % 1000 % 100 / 10 == 1)
if(tens == 1)
{
//if (num % 1000 % 100 % 10 == 0)
if(ones == 0)
printf("ten ");
if(ones == 1)
printf("eleven ");
if(ones == 2)
printf("twelve ");
if(ones == 3)
printf("thirteen ");
if(ones == 4)
printf("fourteen ");
if(ones == 5)
printf("fifteen ");
if(ones == 6)
printf("sixteen ");
if(ones == 7)
printf("seventeen ");
if(ones == 8)
printf("eighteen ");
if(ones == 9)
printf("nineteen ");
}
if(tens == 2)
printf("twenty ");
if(tens == 3)
printf("thirty ");
if(tens == 4)
printf("forty ");
if(tens == 5)
printf("fifty ");
if(tens == 6)
printf("sixty ");
if(tens == 7)
printf("seventy ");
if(tens == 8)
printf("eighty ");
if(tens == 9)
printf("ninety ");
if (tens != 1)
{
if(ones == 1)
printf("one ");
if(ones == 2)
printf("two ");
if(ones == 3)
printf("three ");
if(ones == 4)
printf("four ");
if(ones == 5)
printf("five ");
if(ones == 6)
printf("six ");
if(ones == 7)
printf("seven ");
if(ones == 8)
printf("eight ");
if(ones == 9)
printf("nine ");
}
}
//else
//printf("Invalid number.");
getch();
}
However, when I tried substituting the if statements into switch statements it displays a blank.
I'm stuck at the thousands place. I'm trying to fix it first before getting to the hundreds, tenths, and ones.
Here is what I tried:
#include<stdio.h>
#include<conio.h>
main()
{
int num,thousands,hundreds,tens,ones;
printf("Enter number (1-9999): ");
scanf("%d",&num);
if (num < 1 || num > 9999)
printf("Invalid number.");
else
{
thousands = num / 1000;
switch (thousands)
{
case '1': printf("one thousand ");
break;
case '2': printf("two thousand ");
break;
case '3': printf("three thousand ");
break;
case '4': printf("four thousand ");
break;
case '5': printf("five thousand ");
break;
case '6': printf("six thousand ");
break;
case '7': printf("seven thousand ");
break;
case '8': printf("eight thousand ");
break;
case '9': printf("nine thousand ");
break;
}
}
}
When you write case '1':, you compare to character '1''s value (converted to an integer), not integer; you should write case 1:.
You're not comparing the same values you were before.
In your original code, you were comparing thousands against 0, 1, 2, etc. In your updated code, you're comparing thousands against '0', '1', '2', etc.
Use the same values you had before and you'll get the expected results.
Single quotes in C are used to define single characters. Since characters size is 1 byte in C, a char can be assigned up to 128 different values (1 byte = 8 bits = 2⁸ = from 0 to 127). But how are char represented, according to that integer value?
That is implementation-defined, but usually C uses the seven-bit US ASCII character set to represent char by default (for example, you might change the locale to have it use something different, like utf-8).
Back to your question, in the second snippet you're comparing the value of thousands with the ASCII values of the characters '1', '2', '3', etc.
That means you are not comparing thousands with the value between single quotes, but with the character's ASCII decimal value corresponding to the character, so for example, case '1': equals to case 49:
decimal - character
[...]
49 1
50 2
51 3
52 4
53 5
54 6
[...]
Practical Example
The following program shows just what I've said:
#include <stdio.h>
int main(int argc, char** argv)
{
char ch = '1';
printf("size of a char: %ld bytes\n", sizeof(char));
printf("character representation: %c\n", ch);
printf("character corresponding value in ASCII table: %d\n", ch);
return 0;
}
Output:
size of a char: 1 bytes
character representation: 1
character corresponding value in ASCII table: 49
I am composing a code that gives the user the total of their package after weight and mileage is calculated. It was working before but after I made minor changes anything that weighs over 2lbs is not printing out anything. Why is this?
#include <stdio.h>
#include <stdlib.h>
int main(){
int weight;
int miles;
double mileCost;
int segment;
int remainder;
printf("Charge by weight:(We don't tale packages over 10lbs\n");
printf("\n 1-2 lbs: $1.50\n 3-6 lbs: $3.70\n 7-10 lbs: $5.25\n ");
printf("Enter your package's weight:\n");
scanf("%d", &weight);
printf("Charge by mile: \n");
printf("$1.50 for every 500 miles\n");
printf("Enter the total miles for your package:\n");
scanf("%d", &miles);
if(miles == 0 && weight == 0 && weight < 10){
printf("Invalid entry! Try Again.");
}
segment= miles / 500;
remainder = miles % 500;
if(remainder > 0)
remainder = 1;
if(weight <= 2){
mileCost = 1.50 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
if(weight > 2 && weight >= 6){
mileCost = 3.70 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
if(weight > 6 && weight >= 10){
mileCost = 5.25 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
return 0;
}
You have your upper bounds written with greater than or equal to >= instead of less than or equal to <=:
if(weight > 2 && weight >= 6){
if(weight > 6 && weight >= 10){
This should have been
if(weight > 2 && weight <= 6){
if(weight > 6 && weight <= 10){
regarding:
if(weight > 2 && weight >= 6){
1) this should be preceeded by else so it is not even looked at if weight is <= 2
2) the statement should be:
else if(weight > 2 && weight <= 6){
notice the <= 6
3) similar considerations exist for:
if(weight > 6 && weight >= 10){
I was recently coding a short little fallout shelter survival game and I came across the error "expected ';' before '{' token". The error is around line 28, character 23:
#include <stdio.h>
int main(int argc, char *argv[])
{//setup changing variables
int X = 0;
int money = 300;
int resources[10];
resources[0] = 100;
resources[1] = 200;
printf("Welcome!\nFirst we will choose the extra supplies for your shelter!");
while(X < 1)
{
//Setup the input
char input[15];
//Introduction:
printf("You have $%d dollars\n1: Cards ($4)\n2: Ounce of Gold ($20)\n3: 10 Pounds of Spare Parts($20)\n4: Ham Radio ($75)\n5: Calendar ($5)\n6: Book of Blueprints ($10)\n7: (2 Gallons) Extra Water Storage ($6)\n8: (1 gallon) Fuel ($10)\n9: Check Current Supplies\n10:Start\n", money);
//Have player buy Items
if(input == 1){
if(money > 3){
money = money - 4;
resources[2] = resources[2] + 1;
printf("You have purchased a deck of cards...\n");
}
else{
printf("You can not afford this item\n");
}
}
elif(input == 2)
{
if(money > 19)
{
money = money - 20;
resources[3] = resources[3] + 1;
printf("You have purchased an Ounce of Gold...\n");
}
else
{
printf("You do not have enough money\n");
}
}
elif(input == 3){
if(money > 19){
money = money - 20;
resources[4] = resources[4] + 10;
printf("You have purchased 10 pounds of Scrap Parts...\n");
}
else{
printf("You do not have enough money\n");
}
}
elif(input == 4){
if(money > 74){
money = money - 75;
resources[5] = resources[5] + 1;
printf("You have purchased 1 Ham Radio...\n");
}
else{
printf("You do not have enough money.\n");
}
}
elif(input == 5)
{
if(money > 4)
{
money = money - 5;
recources[6] = resources[6] + 1;
printf("You have just purchased a calendar...\n");
}
else
{
printf("You can not afford the calendar.\n");
}
}
elif(input == 6)
{
if(money > 9){
money = money - 10;
resources[7] = resources[7] + 1;
printf("You have just purchased a Book filled with Blueprints...\n");
}
else{
printf("You can not afford the Book of Blueprints.\n");
}
}
elif(input == 7){
if(money > 5){
int money = money - 6;
int resources[8] = resources[8] + 2;
printf("You have purchased 2 extra gallons of water storage...");
}
else{
printf("You do not have enough money...");
}
}
elif(input == 8){
if(money > 9){
money = money - 10;
resources[9] = resources[9] + 1;
printf("You have purchased one gallon of fuel...");
}
else{
printf("You can not afford the fuel...");
}
}
elif(input == 9){
printf("You currently have: \n%s Pounds of Food\n%s Gallons of Water\n%s Decks of Cards\n%s Ounces of Gold\n%s Pounds of Spare Parts\n%s Ham Radios\n%s Calendars\n%s Books of Blueprints\n%s Gallons of Fuel", resources[0], resources[8] + resources[1], resources[2], resources[3], resources[4], resources[5], resources[6], resources[7], resources[9]);
fgets(input, sizeof(input), stdin);
}
elif(input == 10){
break;
}
else
{
printf("Input Invalid");
fgets(input, sizeof(input), stdin);
}
}
int h = 100;
while(int h > 0)
{
printf("%d", h);
h = h - 1;
}
}
You cannot use elif in C. You should use else if instead.
In printf, %s is not for printing integers. You should use %d instead.
With char input[15];, comparations like input == 1 make no sense. You may use int char;.
input is uninitialized. You should read some data to that.
recources is undefined. It should be typo of resources.
I changed the type of input to int and put scanf before the branches, so remove fgets(input, sizeof(input), stdin);
Remove excess ints in int money = money - 6;, int resources[8] = resources[8] + 2; and while(int h > 0)
The arguments aren't used, so change int main(int argc, char *argv[]) into int main(void). (optional to avoid warnings)
OK, now compile errors are gone.
#include <stdio.h>
int main(void)
{//setup changing variables
int X = 0;
int money = 300;
int resources[10];
resources[0] = 100;
resources[1] = 200;
printf("Welcome!\nFirst we will choose the extra supplies for your shelter!");
while(X < 1)
{
//Setup the input
int input;
//Introduction:
printf("You have $%d dollars\n1: Cards ($4)\n2: Ounce of Gold ($20)\n3: 10 Pounds of Spare Parts($20)\n4: Ham Radio ($75)\n5: Calendar ($5)\n6: Book of Blueprints ($10)\n7: (2 Gallons) Extra Water Storage ($6)\n8: (1 gallon) Fuel ($10)\n9: Check Current Supplies\n10:Start\n", money);
//Have player buy Items
if(scanf("%d", &input) != 1) return 1;
if(input == 1){
if(money > 3){
money = money - 4;
resources[2] = resources[2] + 1;
printf("You have purchased a deck of cards...\n");
}
else{
printf("You can not afford this item\n");
}
}
else if(input == 2)
{
if(money > 19)
{
money = money - 20;
resources[3] = resources[3] + 1;
printf("You have purchased an Ounce of Gold...\n");
}
else
{
printf("You do not have enough money\n");
}
}
else if(input == 3){
if(money > 19){
money = money - 20;
resources[4] = resources[4] + 10;
printf("You have purchased 10 pounds of Scrap Parts...\n");
}
else{
printf("You do not have enough money\n");
}
}
else if(input == 4){
if(money > 74){
money = money - 75;
resources[5] = resources[5] + 1;
printf("You have purchased 1 Ham Radio...\n");
}
else{
printf("You do not have enough money.\n");
}
}
else if(input == 5)
{
if(money > 4)
{
money = money - 5;
resources[6] = resources[6] + 1;
printf("You have just purchased a calendar...\n");
}
else
{
printf("You can not afford the calendar.\n");
}
}
else if(input == 6)
{
if(money > 9){
money = money - 10;
resources[7] = resources[7] + 1;
printf("You have just purchased a Book filled with Blueprints...\n");
}
else{
printf("You can not afford the Book of Blueprints.\n");
}
}
else if(input == 7){
if(money > 5){
money = money - 6;
resources[8] = resources[8] + 2;
printf("You have purchased 2 extra gallons of water storage...");
}
else{
printf("You do not have enough money...");
}
}
else if(input == 8){
if(money > 9){
money = money - 10;
resources[9] = resources[9] + 1;
printf("You have purchased one gallon of fuel...");
}
else{
printf("You can not afford the fuel...");
}
}
else if(input == 9){
printf("You currently have: \n%d Pounds of Food\n%d Gallons of Water\n%d Decks of Cards\n%d Ounces of Gold\n%d Pounds of Spare Parts\n%d Ham Radios\n%d Calendars\n%d Books of Blueprints\n%d Gallons of Fuel", resources[0], resources[8] + resources[1], resources[2], resources[3], resources[4], resources[5], resources[6], resources[7], resources[9]);
}
else if(input == 10){
break;
}
else
{
printf("Input Invalid");
}
}
int h = 100;
while(h > 0)
{
printf("%d", h);
h = h - 1;
}
}
Enjoy!
Sorry, I didn't enjoy but got annoyed.
In c elif is not there. So, it gives that error.
Change the elif to else if and do validations.
I have made a program that can output whatever numeral is inputted in to it but it will convert it to words instead of just numerals for example if you input "1234.56" it will convert it to "One Thousand Two Hundred Thirty Four Dollars and ... 56 Cents". The cents should always be in numerals. So far everything works great, however if I put in an amount that is less that one thousand I will get the excess words such as "Thousand" or "Hundred" in there. For example if I input "15.77" my output will be "Thousand Hundred Fifteen Dollars and ... 77 Cents".
I don't want the Thousand or Hundred to be there, without those it would be perfect!
The code is as follows:
#include <stdio.h>
void printNum(int);
void printNum2(int);
void printNum3(int);
int main()
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int num = 0;
int printcents; //To convert the float "cents" to an integer.
float inclusive;
float cents;
printf("Welcome to the IPC144 Cheque Generator!!\n");
printf("PAY TO THE ORDER OF... amahmood29 (018359133)\n");
printf("Enter a monetary value from $0.01 to $9999.99 inclusive: ");
scanf("%f", &inclusive);
if(inclusive < 0.01 || inclusive >= 10000.00) {
printf("Sorry, cannot create cheque for that amount, try again next time!\n");
}
else
{
a = inclusive / 1000; //This data is replacing our variable by diving whatever the vaulue is by either 1000, 100, 10.
inclusive = inclusive - (a*1000);
b = inclusive / 100;
inclusive = inclusive - (b*100);
if ( inclusive > 19 )
{
c = inclusive / 10;
inclusive = inclusive - (c*10);
}
else
{
c = inclusive;
d = 0;
}
d = inclusive;
num = inclusive;
cents = (inclusive - num)*100; //To calculate our "Cents" with numerals.
printcents = cents;
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
printNum(b);
printf("Hundred ");
printNum2(c);
printf("");
printNum3(d);
printf("Dollars and ... ");
printf("%d", printcents);
printf(" Cents\n");
}
}
void printNum(int x) //Created functions to easily output various if statements.
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
void printNum2(int x)
{
if ( x == 10)
printf("Ten ");
else if ( x == 11)
printf("Eleven ");
else if ( x == 12)
printf("Twelve ");
else if ( x == 13)
printf("Thirteen ");
else if (x == 14)
printf("Fourteen ");
else if (x == 15)
printf("Fifteen ");
else if (x == 16)
printf("Sixteen ");
else if (x == 17)
printf("Seventeen ");
else if (x == 18)
printf("Eighteen ");
else if (x == 19)
printf("Ninteen ");
else if (x == 2)
printf("Twenty ");
else if (x == 3)
printf("Thirty ");
else if (x == 4)
printf("Forty ");
else if (x == 5)
printf("Fifty ");
else if (x == 6)
printf("Sixty ");
else if (x == 7)
printf("Seventy ");
else if (x == 8)
printf("Eighty ");
else if (x == 9)
printf("Ninety ");
}
void printNum3(int x)
{
if ( x == 1)
printf("One ");
else if ( x == 2)
printf("Two ");
else if (x == 3)
printf("Three ");
else if (x == 4)
printf("Four ");
else if (x == 5)
printf("Five ");
else if (x == 6)
printf("Six ");
else if (x == 7)
printf("Seven ");
else if (x == 8)
printf("Eight ");
else if (x == 9)
printf("Nine ");
}
I have been coding for exactly one month now so if it seems like I made simple mistakes that's why.
You need to add conditionals around your printfs:
if (a > 0)
{
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
}
The problem is that you are unconditionally printing the "Thousand", "Hundred", etc...
printNum(a); //Printing is the variables are in the thousands, hundreds, tens or ones categories.
printf("Thousand ");
printNum(b);
printf("Hundred ");
printNum2(c);
printf("");
printNum3(d);
printf("Dollars and ... ");
printf("%d", printcents);
printf(" Cents\n");
if the number you send to printNum is zero, you don't want to print out your text string, you'd have to check for that condition:
/* call to printNum x */
if ( /* check if the parameter to printNum matches any case, seems to be if not zero){
printf(/* whatever string is appropriate */);
}
as an example, say the input was 512.26
512.26/1000 <<< that should be 512.26/ 1000.f so both operands of the divide are floats
will result in a float value of : 0.51226f NOT 0.0f
using the floor(0.51226f) Will result in 0.0f
Suggest always applying the floor() function to yield a 0.0f
Even with a float, a comparison such as if( value != 0.0f ) then print it
would work after applying the floor() function.
Hi im trying to understand why if 9 or above is entered for judge it passes but it shouldnt cause the if says >= 4 and <= 8
Thanks
while(!(judge >= 4) && (judge <= 8))
{
printf("How many judges are there ? Enter a number between 4 - 8 \n");
scanf("%d", &judge);
while(!(judge >= 4) && (judge <= 8))
{
printf("You entered %d Enter a number between 4 - 8 \n", judge);
scanf("%d", &judge);
if((judge >= 4) && (judge <= 8))
{
break;
}
}
}
It looks like you are missing a pair of parentheses in
while(!((judge >= 4) && (judge <= 8)))
^ ^
(This mistake appears in two places.)
By the way, you can avoid a lot of the repetition by restructuring your code like so:
printf("How many judges are there ? Enter a number between 4 - 8 \n");
for (;;) {
scanf("%d", &judge);
if (judge >= 4 && judge <= 8) {
break;
}
printf("You entered %d Enter a number between 4 - 8 \n", judge);
}