#include<stdio.h>
int main()
{
int a,b;
char op;
scanf("%d",&a);
scanf("%c",&op);
scanf("%d",&b);
int w,x;
w=a+b;
x=a-b;
switch(op)
{
case'+':
{
printf("%d",w);
break;
}
case'-':
{
printf("%d",x);
break;
}
default:
{
printf("Invalid");
break;
}
}
return 0;
}
Every time I enter the second input – the character(+ or -) – it directly goes to invalid in the switch case. What am I doing wrong here?
Your scanf sequence isn't doing what you think it is. Print out the value of op before the switch and you'll see the problem.
Your program works just fine. Your problem is with the input: use 123+456 instead of pressing enter between the decimal and the operator.
Here is an example of your program:
$ ./basic
456-123
333$
I suggest adding \n to each printf() format string to eliminate the prompt displaying in the same line.
Related
I want to write a loop that runs until the user enters a number greater than 10, but I have to do something wrong because it creates an infinite loop.
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
for(int i=0;a<10;i++){
printf("Enter value>10");
i++;
printf("%d",&a);
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
You mix an index that does not make sense. Also you print the memory address of variable instead of its value, not sure it is what you wanted?
Code partially corrected (because I don't know what is your ultimate goal):
#include <stdio.h>
int main()
{
int a;
do {
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
printf("\na: %d\n",a);
} while (a <= 10);
printf("Result:%d\n",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
ps: \n is line return and added do while which is what you want when you want to execute a loop at least once.
Have a look at your for-loop: you let i start at zero, you continue until a is not smaller than ten anymore, but it's not the value of a you need to check, it's the one of i.
In top of that, you are doing a i++ within your for-loop, while this is already covered in the definition of the for-loop.
I think this is the code that you are looking for: See comments
#include <stdio.h>
int main()
{
int a, ok = 0, end_of_input = 0;
do {
printf("Please input an integer value (min. 10): ");
fflush(stdout); // So the user can see the above line!
switch(scanf("%d",&a)) {
case EOF: // End of input - Give up!
end_of_input = 1;
break;
case 1: // Got a number - Check it!
if (a < 10)
{
ok = 1;
} else {
printf("%d - Not appropriate input. Please try again.\n\n",a);
}
break;
default: // Summat else - "eat" the input to the next line
scanf("%*[^\n]\n"); // "eats" the rest of the line in the buffer w/o assignment
break;
}
} while (end_of_input == 0 || ok == 0);
if (ok) { // User entered a valid number
printf("Got a that is smaller than ten %d\n", d);
} else { // We have ran out of input
printf("See you want to leave us :-(\n");
}
return 0;
}
I am not sure what you are trying to achieve but one problem that I found in your logic is you prompting user for input outside the loop. So whenever you enter number less than 10 it always goes in infinite iteration.
Try following code, with scanf inside loop
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
int i=0;
for(;a<10;){
printf("Enter value>10");
scanf("%d",&a);
printf("%d",a);
i++;
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
I have written this code and tried to compile it in codeblocks, using default GCC compiler.
When giving option 1, I am getting this output "hello world6356744".
Please explain what is happening.
#include<stdio.h>
#include<stdlib.h>
int fun1(int x,int y)
{
printf("hello world");
return x+y;
}
int fun2()
{
return 0;
}
int main()
{
printf("type 1 for * or type 2 for #");
int a;
scanf("%d", &a);
int ca1;
switch(a)
{
case 1:
ca1=fun1(2,3);
printf("%d", &ca1);
break;
case 2:
fun2();
break;
default:
printf("invalid input");
}
return 0;
}
https://i.stack.imgur.com/A5PhT.png
You are printing the address of the pointer with & on line 28. Remove the & and printf should work correctly. Printf does not take an address, instead it takes the value.
When reading input using scanf, that is when you provide the address of the variable/pointer because scanf directly modifies the variable/pointer you pass into it.
I am new in programming and try to learn by own ,i have an error in my code,i don't understand it's a syntax error or i've done smth wrong.I used 3 equations with it's condition and made it in for isntruction,while,do-while,if-else,with switch.After i introduce variable a it show me an error "exited with non-zero status".
#include <stdio.h>
#include <math.h>
int main() {
float a,x,b;
float L;
int m;
printf("Enter variables a,x,b:");
scanf("%f%f%f,&a,&x,&b");
printf("For using for instruction (enter 1)");
printf("For using while instruction (enter 2)");
printf("For using do-while instruction (enter 3)");
printf("For using ef instruction (enter 4)");
scanf("%d,&m");
switch(m){
case 1:
for (x=0;x<1.2;x++)
{
L=2*cos(x-(3.14/6));
}
for (x=0;x>= 1.2 && x<=3.9;x++)
{
L=x*x/(a+cos(powf((x+b),3)));
}
for (x=0;x>3.9;x++)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
break;
case 2:
while (x<1.2)
{
L=2*cos(x-(3.14/6));
}
while (x>= 1.2 && x<=3.9)
{
L=x*x/(a+cos(powf((x+b),3)));
}
while (x>3.9)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
break;
case 3:
do
{
L=2*cos(x-(3.14/6));
}
while (x<1.2);
do
{
L=x*x/(a+cos(powf((x+b),3)));
}
while (x>= 1.2 && x<=3.9);
do
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
while (x>3.9);
break;
case 4:
if (x<1.2)
{
L=2*cos(x-(3.14/6));
}
else
{
printf("First statement is false");
}
if(x>= 1.2 && x<=3.9)
{
L=x*x/(a+cos(powf((x+b),3)));
}
else
{
printf("Second statement is false");
}
if(x>3.9)
{
L=fabs(x/2*a)+powf(sin(x+1),2);
}
else
{
printf("Third statement is false");
}
break;
default:
printf("\nNo right choices\n");
}
printf("Your answer is: L = %.3f,L");
}
Your problem is that your scanf arguments are not formatted correctly.
Instead of scanf("%f%f%f,&a,&x,&b"); use scanf("%f%f%f",&a,&x,&b);. Same in the second scanf.
The variables addresses are parameters, not part of the string.
When you call it, scanf finds the first %f but it doesn't have any address to put the value into. Or more accuratly, it finds the value it needs from garbage (read about the stack and dynamic number of arguments), because you didn't insert it.
scanf("%f%f%f,&a,&x,&b"); should be like this scanf("%f%f%f",&a,&x,&b);. Please correct where ever you used scanf. Your code is not taking input from user due to wrong syntax. I have compiled and tried it is runnig correctly. Please change scanf syntax every where.
scanf("%f%f%f,&a,&x,&b") to scanf("%f%f%f",&a,&x,&b)
scanf("%d,&m"); to scanf("%d",&m);
printf("Your answer is: L = %.3f,L"); to printf("Your answer is: L = %.3f",L);
You are missing the return 0; Statement at the end of the main function. Since c main function is int main ()
I'm running this program in C to convert Fahrenheit to Celsius and need to accept only integer value from the user.
Please tell me how I can modify this?
int main() {
int x;
double y;
while(x>0) {
printf("Enter the temperature in Fahrenheit:");
scanf("%d", &x);
y=((x-32)/1.8)
printf("%f\n",y);
}
}
The reason your code does not work is that sometimes scanf does not read anything, so it does not modify x.
You know that scanf read something by checking its return value. It returns the number of "scanned" items. In this case, the number must be 1.
When scanf returns 0 instead, you should read and discard the data in the buffer. You do it by supplying %*[^\n] format specifier, which means "read and discard input up to '\n' character. The complete snippet that reads an int until success looks like this:
while (scanf("%d", &x) != 1) {
printf("Please enter a valid number:");
scanf("%*[^\n]");
}
Note: It goes without saying that you should fix your syntax error with the missing semicolon ; on the line that computes y.
You can use below code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
double y;
char str1[5];
int num1,i;
bool yes = true;
while(x>0)
{
printf("Enter the temperature in Fahrenheit:");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
if(!(str1[i]>=48&&str1[i]<=56))
{
printf("The value is invalid \n");
yes = false;
}
num1 = atoi(str1);
if(yes == true)
{
printf("This Number is %d\n",num1);
y=((num1-32)/1.8);
printf("%f\n",y);
}
}
}
Just having a few kinks in this assignment I'm trying to do. Basically I need to have a menu, 4 options, two of them accept input from user as the form of a base number and an exponent. The third one outputs the answer of the base raise to the power and then the fourth just exits the program.
I'm having trouble obtaining the users input via getNum(); I'm not too sure how to use it properly. Just looking on some tips on how to make my code work a little better.
Looking for Help:
Accepting user input from two different functions and using it to
output an answer
Working out the infinite loop problem when selecting menu option
Loop back program to main menu after each function is done and only
exit program when menu option 4 is selected
int main(void)
{
int option = 0;
do
{
loadMenu();
while (option<1 || option>4)
{
printf("\nChoose an option between 1 and 4:");
option = getNum();
while (getNum() != '\n');
}
switch (option)
{
case 1:
baseChange(); //Gets base number
break;
case 2:
powerChange(); //Gets exponent
break;
case 3:
calcMath(); //Calculates the answer
break;
default:
break;
}
}
while (option != 4);
printf("Goodbye!\n");
}
void loadMenu() //Menu choices
{
printf("Power Menu:\n" );
printf(" 1. Change base\n");
printf(" 2. Change exponent\n");
printf(" 3. Calculate\n");
printf(" 4. Exit\n");
printf("Option?\n");
}
int baseChange(int base)
{
printf("What is your base?: ");
base = getNum();
while (getNum() != '\n');
return base;
}
int powerChange(int power)
{
printf("What is the power?: ");
power = getNum();
while (getNum() != '\n');
return power;
}
int calcMath(int base, int power)
{
int index = 0;
long answer = 1.00;
for(index = 1; index <= power; index++) answer = answer * base;
{
printf("%d raised to the power of %d is %ld.\n\n", base, power, answer);
}
return answer;
}
I'm having trouble obtaining the users input via getNum(); I'm not too
sure how to use it properly.
You haven't told us anything about this function; it's not part of the C standard.
Just looking on some tips on how to make my code work a little better. Looking for Help:
I think it's a little early for that. Put more effort into solving your problems, and then come back if you have specific questions. More like this one:
Working out the infinite loop problem when selecting menu option
Look at what your program does with option the second time through the loop.
Please Declare the getnum() function before main() like below;
/* declare getnum() prior to its first use */
float getnum(void)
{
float x;
printf("Enter a number: ");
scanf("%f", &x);
return x;
}