Error with this code? - c

So, I don't see what is flawed with my logic on this problem.
It reads the expression from left to right, and operands are floating point numbers.
However, my program gets stuck in the loop. It reads and assigns the final number, for example it assigns 10.5 to num1, but it never exits the loop.
int main(void)
{
float num1, num2;
char oper = 0;
printf("Enter an expression: ");
scanf("%f", &num1);
while (oper != ('\n' || EOF))
{
oper = getchar();
scanf("%f", &num2);
switch (oper)
{
case '+':
num1 += num2;
break;
case '-':
num1 -= num2;
break;
case '*':
num1 *= num2;
break;
case '/':
num1 /= num2;
break;
}
}
printf("Value of Expression: %.2f", num1);
return 0;
}
Expected output:
Enter an expression: 1+2.5*3
Value of expression: 10.5

while (oper != ('\n' || EOF)) Here, ('\n' || EOF) == 1, so the loop will not terminate unless you input the ASCII character that corresponds to 1 (which is unprintable). As Leffler points out, this should be while (oper != '\n' && oper != EOF) (though see the next point).
You check oper in your while loop before you read from it, so your while loop will terminate one step "after" the newline/EOF. The while loop should actually be:
while(1) {
oper = getchar();
if(oper == '\n' || oper == EOF) {
break; // Exit out of loop, ignoring the rest of the code inside the loop.
}
scanf("%f", &num2);
// ...
}
getchar returns an int, but oper is a char. Change oper to an int (EOF cannot be represented as a char)

Make oper an int. 255 is not equal to EOF

You need to revise the loop ending conditions. Your program is actually busy waiting for you to type another number after the first newline.
#include <stdio.h>
int main(void)
{
float num1, num2;
printf("Enter an expression: ");
if (scanf("%f", &num1) != 1)
return 1;
printf("Num1: %f\n", num1);
while (1)
{
int oper;
if ((oper = getchar()) == EOF || oper == '\n')
break;
printf("Operator: %c\n", oper);
if (scanf("%f", &num2) != 1)
break;
printf("Num2: %f\n", num2);
switch (oper)
{
case '+':
num1 += num2;
break;
case '-':
num1 -= num2;
break;
case '*':
num1 *= num2;
break;
case '/':
num1 /= num2;
break;
}
printf("num1 = %f\n", num1);
}
printf("Value of Expression: %.2f\n", num1);
return 0;
}
Note that this code checks each input operation to make sure it was successful. It also uses int oper instead of char oper to ensure that EOF is detected accurately. And the key trick is not to ask for more input after the getchar() reads the newline; your program was stuck waiting for you to type something in response to the second scanf() call. Notice how this code prints its inputs so you can tell what's going on. When a program is misbehaving, it is one of the simpler techniques to find out what's going wrong. When I saw Operator: and a blank line — and the program hanging — I knew very quickly what the trouble was.
Example run (program name: stuck):
$ ./stuck
Enter an expression: 1+2.5*3
Num1: 1.000000
Operator: +
Num2: 2.500000
num1 = 3.500000
Operator: *
Num2: 3.000000
num1 = 10.500000
Value of Expression: 10.50
$

Related

When I add two integers it says invalid although supposedly it's for characters and symbols. Am I missing something?

In my calculator I tried firstly to make one operation functioning to have integers be displayed properly and when someone inputted a character it would say invalid.
When I input two integers it say's invalid. Not the actual sum of it.
#include <stdio.h>
#include <conio.h>
int main(){
char op;
int num1, num2;
int result;
printf("Enter (+, -, /, *): ");
scanf("%c", &op);
printf("Enter Two Integers: \n");
scanf("%d %d", &num1, &num2);
switch (op){
case '+':
result = num1+num2;
if(!(num1 == '+' && num2 == '+')){
printf("Invalid");
}
else{
printf("Sum: %d ", result);
}
break;
case '-':
result = num1-num2;
printf("Difference: %d ", result);
break;
case '/':
result = num1/num2;
printf("Quotient: %d ", result);
break;
case '*':
result = num1*num2;
printf("Product: %d ", result);
break;
default:
break;
}
getch();
return 0;
}
I expected that with that new line of condition it will make characters and symbols print "Invalid"
if(!(num1 == '+' && num2 == '+'))
This doesn't make any sense for several reasons. First of all De Morgan's laws and boolean algebra is often considered a prerequisite before studying any form of programming. By applying De Morgan/common sense, then we can tell that the opposite to "if num1 is + and num2 is +" is "if num1 isn't + OR num2 isn't +". That is:
if(num1 != '+' || num2 != '+') or if you will if(!(num1 == '+' || num2 == '+')).
With that logic flaw out of the way, this is the wrong solution to the the actual problem anyway. You simply want to prevent the user from entering a character - any character not just '+' - when expecting a number. The easiest way of doing that is to check the result of scanf - it returns a number corresponding to the number of arguments successfully read. For example:
int result;
do
{
result = scanf("%d %d", &num1, &num2);
if(result != 2)
{
printf("You must enter two numbers!\n");
}
}
while(result != 2);
Another option is to read the input as a string with fgets and then parse that string afterwards.
As a side note, please note that <conio.h> has been obsolete for well over 20 years and if someone taught you to use it, you need a more updated source of learning. The future for freshly graduated MS DOS programmers isn't very promising...
Be sure to check the return value of scanf to ensure it read the number of values you expected.
num1 and num2 are int variables and you are reading into them using the %d specifier. If you type in + or other symbols, scanf("%d %d", &num1, &num2) will return 0. A simple demonstration:
$ cat > testing.c
#include <stdio.h>
int main(void) {
int op;
int result = scanf("%d", &op);
printf("%d\n", result);
}
$ gcc testing.c
$ ./a.out
+
0
$ ./a.out
67
1
The odds that num1 and num2 uninitialized both contain the value '+' are not impossible but highly unlikely, so the most likely outcome is that "Invalid." is printed.
As noted in comments, 43 is the ASCII code for '+' so if these specific numbers are input, both num1 and num2 will test as equal to 43 and their sum (86) will be printed.

scanf("%d/%d%c%d/%d", &num1, &denom1, &sign, &num2, &denom2); Keeps on scanning for user input at the console

scanf("%d/%d%c%d/%d", &num1, &denom1, &sign, &num2, &denom2); keeps on scanning for user input at the console, no matter how many integers or characters I enter. The program does not proceed further. What is happening here?
By the way my code is as follows
#include<stdio.h>
//------------------------START OF MAIN()--------------------------------------
int main(void)
{
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
int num1, denom1, num2, denom2, result_num, result_denom,flag=1;
char sign;
printf("Enter fraction1 operator fraction2(operators + - * /): ");
scanf("%d/%d%c%d/%d", &num1, &denom1, &sign, &num2, &denom2); //doesn't work. Won't stop taking inputs
//three scanfs --for fraction1, operator, fraction2--also takes unlimited inputs.
//A scanf to read fraction1, getchar to read operator and another scanf to read fraction2 works fine.
//scanf("%d/%d", &num1, &denom1);
//sign=getchar();
//scanf("%d/%d",&num2, &denom2);
switch(sign)
{
case '+':
result_num = num1 * denom2 + num2 * denom1;
result_denom = denom1 * denom2;
break;
case '-':
result_num = num1 * denom2 - num2 * denom1;
result_denom = denom1 * denom2;
break;
case '*':
result_num = num1 * num2;
result_denom = denom1 * denom2;
break;
case '/':
result_num = num1 * denom2;
result_denom = denom1 * num2;
break;
default:
flag=0;
printf("Invalid Operator.");
}
if(flag)
printf("The result is %d/%d", result_num, result_denom);
printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
return 0;
}
//-------------------------END OF MAIN()---------------------------------------
The input I tried to enter is 31/8+7/4. And I press enter, but the program won't proceed further.
If I use the following three statements viz, scanf(), getchar(), printf() instead (which are commented), I am able to execute the program without issues (as long as there is no line feed while entering the input). What is happening here?

A simple “printing” calculator

When I'm typing in a digit I see
Type in a digit 1
Type in an operator ERROR: Unknown operator!
accumulator = 0.000000
Type in a digit
Why step - printf("Type in an operator ") is skipped and is replaced by - default:
printf ("ERROR: Unknown operator!\n");
break;
Thanks for the help in advance!
// Program to produce a simple printing calculator
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
double accumulator = 0.0, number; // The accumulator shall be 0 at startup
char operator;
bool isCalculating = true; // Set flag indicating that calculations are ongoing
printf("You can use 4 operator for arithmetic + - / *\n");
printf("To set accumulator to some number use operator S or s\n");
printf("To exit from this program use operator E or e\n");
printf ("Begin Calculations\n");
while (isCalculating) // The loop ends when operator is = 'E'
{
printf("Type in a digit ");
scanf ("%lf", &number); // Get input from the user.
printf("Type in an operator ");
scanf ("%c", &operator);
// The conditions and their associated calculations
switch (operator)
{
case '+':
accumulator += number;
break;
case '-':
accumulator -= number;
break;
case '*':
accumulator *= number;
break;
case '/':
if (number == 0)
printf ("ERROR: Division by 0 is not allowed!");
else
accumulator /= number;
break;
case 'S':
case 's':
accumulator = number;
break;
case 'E':
case 'e':
isCalculating = false;
break;
default:
printf ("ERROR: Unknown operator!\n");
break;
}
printf ("accumulator = %f\n", accumulator);
}
printf ("End of Calculations");
return 0;
}
scanf for a char consumes the newline characters. So the scanned char is "linefeed" instead of the one you're expecting.
I replaced:
scanf ("%c", &operator);
by
scanf ("%*c%c", &operator);
(consuming linefeed before the operator without assigning it using %*c format)
and your code worked fine.

Struggling to program a simple calculator

I am trying to program a simple calculator. Here is my code first:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char operator = 0;
float num1 = 0.0;
float num2 = 0.0;
float sol = 0.0;
while (operator != 'q') {
printf("Operator: ");
scanf("%c", &operator);
printf("First Number: ");
scanf("%f", &num1);
printf("Second Number: ");
scanf("%f", &num2);
switch (operator)
{
case '+': sol = num1 + num2; break;
case '-': sol = num1 - num2; break;
case '*': sol = num1 * num2; break;
case '/': sol = num1 / num2; break;
case 'q': printf("Finished!"); exit(0);
default: printf("Error!"); exit(0);
}
printf("The solution is: %.2f\n\n", sol);
}
return 0;
}
So for me the code is fine. As you can see I did this with a while loop that lets you calculate until you type in 'q' as operator. The first run of the loop works fine but then it gets creepy (my console):
Operator: +
First Number: 5
Second Number: 4
The solution is: 9.00
Operator: First Number:
Why does the program not let me enter an operator in the second loop run?
Most format specifiers with scanf will skip leading whitespace. %c does not.
scanf("%f", &num2); at the end of the first iteration leaves a newline in the input buffer.
scanf("%c", &operator); at the start of the second iteration, reads the newline and proceeds.
using a space before %c in scanf(" %c", &operator); will allow %c to skip the leading whitespace and capture the operator.
You should check scanf for errors:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char operator = 0;
float num1 = 0.0;
float num2 = 0.0;
float sol = 0.0;
while (operator != 'q') {
printf("Operator: ");
if((scanf(" %c", &operator)) != 1){
printf("Error, Fix it!\n");
exit(1);
}
printf("First Number: ");
if((scanf("%f", &num1)) != 1){
printf("Error, Fix it!\n");
exit(1);
}
printf("Second Number: ");
if((scanf("%f", &num2)) != 1){
printf("Error, Fix it!\n");
exit(1);
}
switch (operator){
case '+': sol = num1 + num2; break;
case '-': sol = num1 - num2; break;
case '*': sol = num1 * num2; break;
case '/': sol = num1 / num2; break;
case 'q': printf("Finished!"); exit(0);
default: printf("Error!"); exit(0);
}
printf("The solution is: %.2f\n\n", sol);
}
return 0;
}
and as you can see I changed scanf("%c", &operator); to this scanf(" %c", &operator); to make scanf to ignore (skip) Whitespace.

While loop - changing string variable

As a newbie to c programming (I've only had experience in visual basic), I'm not entirely sure how a while loop with a changing string variable in its condition statement should function.
The following code is a simple calculator that I was making that allows the user to input an operation and two numbers, then output the respective result. I'am trying to code in a while loop that continually repeats the procedure until the user decides to exit it. However it seems that the line scanf("%c", &quit); isn't affecting the while loop condition statement.
#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = "n";
while (quit = "n"){
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
case '+':
printf("%f\n", num1+num2);
break;
case '-':
printf("%f\n", num1-num2);
break;
case '*':
printf("%f\n", num1*num2);
break;
case '/':
printf("%f\n", num1/num2);
break;
}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}
Thanks for all your help in advance.
You could do like this
#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = 'n';
//while (quit = "n") //
while (quit!= 'y')
{
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
case '+':
printf("%f\n", num1+num2);
break;
case '-':
printf("%f\n", num1-num2);
break;
case '*':
printf("%f\n", num1*num2);
break;
case '/':
printf("%f\n", num1/num2);
break;
}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}
replace while (quit = "n") with while (quit! = 'y')
That's because you're assigning the value of the quit variable in your while loop, instead of checking for its value.
Use == for =
while(quit == "n"){...}

Resources