I used to compile in VSCode with mingw-64 in Windows 10.
Please rectify my errors.
[1]: https://i.stack.imgur.com/j5CKZ.jpg [
#include <stdio.h>
int main() {
float rupees,result;
int choice;
printf("Enter 1 for USD\n");
printf("Enter 2 for EUR\n");
printf("Enter 3 for AUS\n");
printf("Enter 4 for UAE Dirham\n");
printf("Enter amount in Rupees\n");
scanf("%d",&rupees);
printf("Enter your choice\n");
scanf("%d",choice);
switch (choice)
{
case 1:
result=rupees*73.04 ;
break;
printf("%f Amount equal to %f USD\n",rupees,result);
case 2:
result=rupees*88.82 ;
printf("%f Amount equal to %f EUR\n",rupees,result);
break;
case 3:
result=rupees*56.36 ;
printf("%f Amount equal to %f AUS\n",rupees,result);
break;
case 4:
result=rupees*19.89 ;
printf("%f Amount equal to %f Dirham\n",rupees,result);
break;
default:
printf("Enter correct choice");
break;
}
return 0;
}
Firstly, Let me try to review your thought process before getting onto the code.
You declared and read variables to store the value in rupees, the user choice and the conversion result.
You wrote a switch case to activate a particular block of code based on user choice and in a default case, you wrote a "printf" statement to convey to the user that the choice was invalid.
However, the program is tainted and below are the reasons why:
The Logic is invalid, as per your program, 1 rupee must be equivalent to 73.04 USD, 88.82 EUR,56.36 AUS and 19.89 Dirham accordingly which is a falsity. The vice versa is true! 1 USD=73.04 Rupees and so on. Quick Fix would be to replace "*" operator with "/" operator at every switch case.
You cannot actually read a fractional value for rupees using the above code as the placeholder for a float data-type in C is "%f" and not "%d".
In the line where you wanted to read the user choice, you did not use the Address-Of Operator(&) i.e you did not give the address location into which the value is to be read, you gave the identifier itself.
In the first switch case, you placed the break statement before the "printf" statement. Which would mean that you would break the control flow even before you would print something on your console.
In a default case, you asked the user to enter a valid choice but the problem is that the control terminates thereafter leaving no scope to re-enter the choice. A quick fix to this is to put the switch case within a loop or alternatively, just display an error message stating "Invalid Choice". (Note that such issues are called semantic errors where the structure of the program is valid as per the language rules but the overall logic and flow of the application is inconsistent).
Miscellaneously, there are some message-format errors that could be made better.
Ex: You don't have to ask to enter his/her choice given that you prior mentioned "Enter 1 for USD.....2 for EUR.....". Also, you could probably replace Amount with Rupees while printing the end-result to make the output more elegant and clear.
Last but not the least, the flow of your code is not so appropriate. Initially you display the entire catalogue to the user to choose from and then you ask the user to enter the value in rupees(you could ask the user to enter the rupee value irrespective of his conversion scale). By slightly rearranging the code, the usability of your script increases.
Want to look at Rectified Code? Here you go!
Rectified Code:
#include <stdio.h>
int main()
{
float rupees,result;
int choice;
printf("Enter amount in Rupees\n");
scanf("%f",&rupees);
printf("Enter 1 for USD\n");
printf("Enter 2 for EUR\n");
printf("Enter 3 for AUS\n");
printf("Enter 4 for UAE Dirham\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
result=rupees/73.04 ;
printf("%f Rupees is equivalent to %f USD\n",rupees,result);
break;
case 2:
result=rupees/88.82 ;
printf("%f Rupees is equivalent to %f EUR\n",rupees,result);
break;
case 3:
result=rupees/56.36 ;
printf("%f Rupees is equivalent to %f AUS\n",rupees,result);
break;
case 4:
result=rupees/19.89 ;
printf("%f Rupees is equivalent to %f Dirham\n",rupees,result);
break;
default:
printf("That was an Invalid Choice!");
break;
}
return 0;
}
Related
I am new to C and am writing a simple code of converting temperatures. The code is still incomplete but still should give me some output
#include<stdio.h>
void main()
{
float temp;
char choice;
printf("\n 1. Celcius to Farenhite\n 2. Farenhite to Celcius\n What do you want to convert from? : ");
scanf("%c", &choice);
switch (choice)
{
case 1:
printf("Enter temperature in Celcius: ", temp );
scanf("%f", &temp);
break;
case 2:
printf("Enter temperature in Farenhite: ", temp);
scanf("%f", &temp);
break;
default:
printf("Invalid Choice");
break;
}
}
When I run this it asks "what do you want to convert from?" and shows the options. But when I enter 1 or 2, it directly prints and shows "Invalid Choice".
Pls tell me what's wrong :(
1 is 'int' and not a char.
1 and '1' are different.
This is the edited code
#include<stdio.h>
void main()
{
float temp;
char choice;
printf("\n 1. Celcius to Farenhite\n 2. Farenhite to Celcius\n What do you want to convert from? : ");
scanf("%c", &choice);
switch (choice)
{
case '1':
printf("Enter temperature in Celcius: ", temp );
scanf("%f", &temp);
break;
case '2':
printf("Enter temperature in Farenhite: ", temp);
scanf("%f", &temp);
break;
default:
printf("Invalid Choice");
break;
}
}
The number one is not the same as the digit "1". You are entering the character "1" and your switch statement is checking for the number one.
The number one is how many hearts I have. It can be written with the digit '1' but any number of other ways.
The digit '1' is a character. It is sometimes use to represent the number one but can also be used for other things. A variable of type char can hold the digit 1 since digits are characters.
Your switch statement is checking for the number one. You want to check for the character 1.
'1' is a character, whereas 1 is an integer. Your switch statement can't find a case to match with, hence it goes to default.
Is that the complete code? You're not converting anything here, you're only asking the user for the temperature and storing it in a variable.
OT: Indent properly, your code is messy and difficult to read. And do not use scanf for taking input. You may want to check out this link http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html.
This is a common misunderstanding for novices and occasional typo for experienced programmers(*).
1 means "the integer value one" (it is the integer literal for the value of 1).
Assuming your on an ASCII compatible platform(**) that is the character with ASCII code 1 (the non-printing character called Start of Header - SOH).
The ASCII Code for the character 1 is actually 49. But you should write:
case '1':
Because those apostrophes identify the 1 as the character representing 1 not the numeric value of 1.
The compiler will interpret '1' as actually meaning character of 1 meaning the (codepoint) value of 49.
In C char has a deeply dual role as a numeric type and as the smallest unit of text and accidental mixing of those roles causes endless confusion.
Arguably char has 3 roles. The smallest arithmetic type, the smallest unit of text (a character) and the unit of addressable memory (byte).
There are historical reasons why those roles are conflated in C.
(*) But I can't find a good answer for it even though there should be thousands of times this has been asked.
(**) C does not specify a character set but if you working on a platform that isn't ASCII compatible you will know about it. Unless you've found something in your Grandparent's basement and they're sadly not around to explain it. This footnote exists for the likely comments to the effect that C doesn't specify a character set.
Your program reads a byte from stdin and compares that to the values 1 and 2. It is unlikely the user can type these byte values as they correspond to control characters CtrlA and CtrlB1. The byte values representing the digits 1 and 2 typed by the user are noted in C as '1' and '2'. These are called character constants and are int values for the encoding of the corresponding characters.
Furthermore, you should:
indent your code more consistently.
define main with a return type int
test the return value of scanf()
fix the spelling of Fahrenheit, named after German physicist Daniel Gabriel Fahrenheit and Celsius, named after Swedish astronomer Anders Celsius.
1: From a unix terminal, one can actually enter these byte values by hitting CtrlV CtrlA and CtrlV CtrlB, respectively, followed by Enter.
Here is a modified version:
#include <stdio.h>
int main() {
float temp;
char choice;
printf(" 1. Celsius to Fahrenheit\n"
" 2. Fahrenheit to Celsius\n"
" What do you want to convert from? ");
if (scanf(" %c", &choice) != 1)
return 1;
switch (choice) {
case '1':
printf("Enter temperature in Celsius: ", temp);
if (scanf("%f", &temp) != 1)
return 1;
printf("%g degrees Celsius is %g degrees Fahrenheit\n",
temp, 32 + temp * 9 / 5);
break;
case '2':
printf("Enter temperature in Fahrenheit: ", temp);
if (scanf("%f", &temp) != 1)
return 1;
printf("%g degrees Fahrenheit is %g degrees Celsius\n",
temp, (temp - 32) * 5 / 9);
break;
default:
printf("Invalid choice\n");
break;
}
return 0;
}
#include <stdio.h>
int main () {
int vIn_a, vIn_b, vIn_c;
char vOperator;
printf("Please enter a number\n");
scanf("%d", vIn_a);
printf("Please enter a number\n");
scanf("%d", vIn_b);
printf("Please enter a Operator\n");
scanf("%c", vOperator);
switch(vOperator){
case '+':
vIn_c = (vIn_a + vIn_b);
break;
case '-':
vIn_c = (vIn_a - vIn_b);
break;
case '/':
vIn_c = (vIn_a / vIn_b);
break;
case '*':
vIn_c = (vIn_a * vIn_b);
break;
}
printf("Result: %d %c %d = %d", vIn_a, vOperator, vIn_b, vIn_c);
return 0;
}
Just trying to figure this out, i ran gdb. But not sure what my debugger is telling me at this point. Maybe im overlooking it?
Debugger: Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7c60d36 in ?? () from /usr/lib/libc.so.6
So what is causing this segmentation fault guys? Im learning C and im lost.
Thanks in advance.
When you use scanf(), you don't pass the variable into which you want to store the value, you pass a pointer to the variable.
So, for instance, instead of scanf("%d", vIn_a);, you need scanf("%d", &vIn_a); - note the '&'!
The effect of the scanf() calls, as you wrote them, was to pass an arbitrary number (whatever random content was in the uninitialised vIn_a and vIn_b) into scanf(). It treated those random(ish) integer values as pointers. So when it wrote the user-contributed value into the "pointer" it has been passed, it had the effect of:
*(int *)vIn_a = user_entered_value;
If you know your way around pointers, you'll know this is a recipe for disaster!
There are two more gotchas:
Your '/' operator doesn't check whether its divisor (vIn_b) is zero, so it would be easy to crash with a divide-by-zero error if the user selected zero for vIn_b and '/' for the operator.
You don't have a default: clause in your switch statement, so if the user types something other than the operator characters you're checking for, vIn_c will contain random rubbish as it's uninitialised by the time it's printed.
You can use scanf function with &. For example; scanf("%d", &vIn_a);
For documentation : https://cplusplus.com/reference/cstdio/scanf/
I am writing code for my switch case. I am taking input from the user and printing the respective day based on the input. But my question is, if user is giving string input it is printing case 0 statement as output. Can anyone please correct this program?
#include<stdio.h>
int main(void){
int days;
scanf("%d",&days);
switch(days){
case 0: printf("Mon");break;
case 1: printf("Tue");break;
case 2: printf("Wed");break;
case 3: printf("Thu");break;
case 4: printf("Fri");break;
case 5: printf("Sat");break;
case 6: printf("Sun");break;
default: printf("Plz enter a valid day(0-6) :( ");
}
return 0;
}
My output printing is :
>>a.out
Naveen
mon
scanf won't modify your int if the format specifier isn't found in the input.
In this case, you're switching on an uninitialized int. Doing anything with uninitialized POD types is generally A Bad Thing™, so you should take care that your value is initialized properly.
In this case, since you want it to trigger the default branch, initializing as int days = -1; (or as anything not in [0,6]) should do the trick.
PS: Note that scanf also returns an int telling you how many arguments it successfully found. This means you can check if scanf returned either 0 or EOF and handle this case separately - for example a more descriptive error message.
loop until days was properly scanned
while(scanf("%d",&days) != 1) getc(stdin);
I am writing a program that accepts 2 binary numbers from the user. Then the user selects a arithmetic expression (+ - / * %) to apply to the numbers. I have the general input code but am at a loss as to where to go next. I'm fairly new to the C language. Here is what I have so far.
#include <stdio.h>
int main(){
int number1, number2;
char expression;
//Basic instructions at the beginning of the program
printf("This is a program to execute arithmetic in binary.\n");
printf("The program will ask you for input in the form of two binary numbers separated byan arithmetic expression (+ - / * %).\n");
printf("The binary numbers must be only 1's and 0's and a maximum of seven digits.\n");
printf("You may exit the program by typing 'exit'.\n");
//Obviously an incomplete do statement, need a loop
do {
//Getting input from the user
printf("\nEnter first binary number: ");
scanf("%d", &number1);
printf("Enter second number: ");
scanf("%d", &number2);
printf("Which expression would you like (+ - / * %): ");
scanf("%c", &expression);
}
}
Since expression is a char(and not char[]), you can use switch-case:
int result;
switch(expression){
case '+':
result=number1+number2;
break;
case '-':
result=number1-number2;
break;
case '*':
result=number1*number2;
break;
case '/':
result=number1/number2;
break;
}
You might also want to add a default, in case the user entered an invalid operator.
Hi I am new to programming and have been working on a calculator for a while now. I am trying to add some trig functions in and I am having trouble with sine. The other functions work (+, -, *, /) but when I put in "sine" it skips to the part of the code where it says it is an incorrect function. Please help out with my code. Thanks!
#include <stdio.h>
#include <math.h>
int main()
{
float firstnum, secondnum, angle, answer, pi;
char function, sine;
pi = atan(1.0)*4;
printf("\nHello and welcome to my calculator!\n");
while(1)
{
printf("\nPlease input the function you would like to use. These include +, -, *, /, sine.\n");
scanf("%s", &function);
switch(function)
{
case '+':
printf("\nNow please input the two variables.\n");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
answer = firstnum+secondnum;
break;
case '-':
printf("\nNow please input the two variables.\n");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
answer = firstnum-secondnum;
break;
case '*':
printf("\nNow please input the two variables.\n");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
answer = firstnum*secondnum;
break;
case '/':
printf("\nNow please input the two variables.\n");
scanf("%f", &firstnum);
scanf("%f", &secondnum);
answer = firstnum/secondnum;
break;
case 'sine':
printf("\nPlease enter the angle.\n");
scanf("%f", &angle);
answer = sin(angle);
break;
default: printf("Sorry, that is an incorrect function. The only available choices are +, -, *, /, sine.");
break;
}
printf("Your answer is %f \n", answer);
printf("\nWhen you are ready to quit, simply press Ctrl + C or just hit the X button in the top right.\n");
}
return 0;
}
'sine'
That is a multi-character literal. function is a single character. It's integral value is checked in the switch statement. You will likely never be able to consume a single character from the user which matches sine in the way that you are attempting to do so. Read a string (a char*) instead.
From the standard:
C99 6.4.4.4p10: "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."
C does not have a first class string type. This means that you cannot use a switch statement for strings, you will need to use functions such as strlcmp for string comparison.
Depending on your objective (either making a calculator, or learning C) it might be wise to either switch to a different language with higher abstraction levels, or start with lower level exercises from a good C textbook.
Also, please be aware that working with strings and user input correctly in C, that is without security holes, is much more difficult than it would seem at first. If your objective is learning a language perhaps learning C++ is a better bet where you have std::string to handle your comparisons and iostreams to handle your input and output.