error checking help
Please support our C advertiser: Programming Forums
![]() |
•
•
Posts: 1
Reputation:
Solved Threads: 0
Everyone,
I am having a problem with trying to get this to prompt the user for the correct input. I would like to user to be required to enter numbers only after it prompts him/her at "How much money would you like to convert today? (above 100): " My question is what is the easiest way to get this done?
Also, I am a newbie to this and this is my first programming class so please break things down so even I can understand them.
Thank you for your help.
[PHP]#include <stdio.h>
int main (int argc, char** argv)
{
float name; //declares a variable "name"
float canada; //declares a variable "canada"
float sa; //declares a variable "sa"
float botswana; //declares a variable "botswana"
float china; //declares a variable "china"
float russia; //declares a variable "russia"
float amt; //declares a variable "amt"
int correctItemsRead;
printf("\n"); //Skips a line
printf("Currency Conversion\n"); //Displays Currency Conversion to screen
printf("\n"); //Skips a line
printf("I can convert US dollars to the following currencies: \n"); //Prints statement to the screen
printf("\n"); //Skips a line
printf("Canadian Dollars\nSouth African Rand\nBotswana Pula\nChina Renminbi\nRussian Rubles\n"); //Lists the different currencies that will be converted
printf("\n"); //Skips a line
do //This says to do what is in the braces at least once
{
printf("How much money would you like to convert today? (above 100): ");
correctItemsRead = scanf("%f", &amt);
}
while ((amt <= (float) 100) && (correctItemsRead == 1)); // this says that if amt <= 100 do what is in the braces again
//(float) tells the compiler to treat the value next to it
//like a float instead of a literal int
printf("I would be happy to help you with that, Thank you.\n"); //Prints statement to the screen
canada=1.324*amt; //Sets "canada" to 1.324 x "amt"
sa=6.865*amt; //Sets "sa" to 6.865 x "amt"
botswana=4.558*amt; //Sets "botswana" to 4.558 x "amt"
china=8.265*amt; //Sets "china" to 8.265 x "amt"
russia=29.234*amt; //Sets "russia" to 29.234 x "amt"
printf("\n"); //Skips a line
printf("In Canadian Dollars you would have = %.2f\n",canada); //Displays dollar equivilant to user input as Canadian Dollars
printf("In South African Rand you would have = %.2f\n",sa); //Displays dollar equivilant to user input as Canadian Dollars
printf("In Botswana Pula you would have = %.2f\n",botswana); //Displays dollar equivilant to user input as Canadian Dollars
printf("In China Renminbi you would have = %.2f\n",china); //Displays dollar equivilant to user input as Canadian Dollars
printf("In Russian Rubles you would have = %.2f\n",russia); //Displays dollar equivilant to user input as Canadian Dollars
printf("\n"); //Skips a line
printf("I would go for the Russian Rubles. At least you'll feel rich!\n"); //Prints statement to screen
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("By the way, Could I get your name please, "); //Prints atatement to the screen and capture users name
scanf("%s",&name); //Captures user input
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("You have a wonderful adventure,%s.\n",&name); //Prints users input to the screen
getchar();
return 0;
}[/PHP]
I am having a problem with trying to get this to prompt the user for the correct input. I would like to user to be required to enter numbers only after it prompts him/her at "How much money would you like to convert today? (above 100): " My question is what is the easiest way to get this done?
Also, I am a newbie to this and this is my first programming class so please break things down so even I can understand them.
Thank you for your help.
[PHP]#include <stdio.h>
int main (int argc, char** argv)
{
float name; //declares a variable "name"
float canada; //declares a variable "canada"
float sa; //declares a variable "sa"
float botswana; //declares a variable "botswana"
float china; //declares a variable "china"
float russia; //declares a variable "russia"
float amt; //declares a variable "amt"
int correctItemsRead;
printf("\n"); //Skips a line
printf("Currency Conversion\n"); //Displays Currency Conversion to screen
printf("\n"); //Skips a line
printf("I can convert US dollars to the following currencies: \n"); //Prints statement to the screen
printf("\n"); //Skips a line
printf("Canadian Dollars\nSouth African Rand\nBotswana Pula\nChina Renminbi\nRussian Rubles\n"); //Lists the different currencies that will be converted
printf("\n"); //Skips a line
do //This says to do what is in the braces at least once
{
printf("How much money would you like to convert today? (above 100): ");
correctItemsRead = scanf("%f", &amt);
}
while ((amt <= (float) 100) && (correctItemsRead == 1)); // this says that if amt <= 100 do what is in the braces again
//(float) tells the compiler to treat the value next to it
//like a float instead of a literal int
printf("I would be happy to help you with that, Thank you.\n"); //Prints statement to the screen
canada=1.324*amt; //Sets "canada" to 1.324 x "amt"
sa=6.865*amt; //Sets "sa" to 6.865 x "amt"
botswana=4.558*amt; //Sets "botswana" to 4.558 x "amt"
china=8.265*amt; //Sets "china" to 8.265 x "amt"
russia=29.234*amt; //Sets "russia" to 29.234 x "amt"
printf("\n"); //Skips a line
printf("In Canadian Dollars you would have = %.2f\n",canada); //Displays dollar equivilant to user input as Canadian Dollars
printf("In South African Rand you would have = %.2f\n",sa); //Displays dollar equivilant to user input as Canadian Dollars
printf("In Botswana Pula you would have = %.2f\n",botswana); //Displays dollar equivilant to user input as Canadian Dollars
printf("In China Renminbi you would have = %.2f\n",china); //Displays dollar equivilant to user input as Canadian Dollars
printf("In Russian Rubles you would have = %.2f\n",russia); //Displays dollar equivilant to user input as Canadian Dollars
printf("\n"); //Skips a line
printf("I would go for the Russian Rubles. At least you'll feel rich!\n"); //Prints statement to screen
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("By the way, Could I get your name please, "); //Prints atatement to the screen and capture users name
scanf("%s",&name); //Captures user input
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("\n"); //Skips a line
printf("You have a wonderful adventure,%s.\n",&name); //Prints users input to the screen
getchar();
return 0;
}[/PHP]
•
•
Posts: 76
Reputation:
Solved Threads: 2
u are almost there. the scanf function returns teh number of correctly read variables. in your while loop, u are testing:
while ((amt <= (float) 100) && (correctItemsRead == 1));
but actually it should be:
while ((amt <= (float) 100) && (correctItemsRead != 1));
when it returns 1, u want to break out of the loop, not the other way around.
while ((amt <= (float) 100) && (correctItemsRead == 1));
but actually it should be:
while ((amt <= (float) 100) && (correctItemsRead != 1));
when it returns 1, u want to break out of the loop, not the other way around.
![]() |
Similar Threads
Other Threads in the C Forum
- Custom error checking (C#)
- error checking (Java)
- User Form error checking (PHP)
- Help with 1.pointers and 2.error checking (C++)
- Error Checking for user input (Java)
- error checking of user input (C++)
- Basic Error Checking (C++)
- How to Perform Disk Error Checking in Windows XP (Windows tips 'n' tweaks)
Other Threads in the C Forum
- Previous Thread: TImage & TBitmap Compatibilities
- Next Thread: fread problem?
•
•
•
•
Views: 5183 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode