i was writing a program in c and i used the function flushall_
that is used in visual 2013 express and it kept giving me the error :
Error 1 error LNK2019: unresolved external symbol _flushall_ referenced in function _main C:\Users\Naya123\Desktop\proj1\proj1\q1.obj proj1
i also tryed copying the code into a new project and also i delete the function flushall_ from the code but still the same
the code (its only the main ) :
#include<stdio.h>
#include<math.h>
#include<string.h>
void main()
{
char option; int num1, num2, num3,count=0,num,base;
menue();
scanf_s("%c",&option);
while (option != 'Y' || option != 'y')
{
switch (option)
{
case 'a' :
case 'A':
printf("insert first number : "); scanf_s("%d", &num1);
printf("insert seconde number : "); scanf_s("%d", &num2);
printf("insert third number : "); scanf_s("%d", &num3);
printf("the max between these three is %d \n ", max(num1, num2, num3));
break;
case 'B' :
case 'b':
printf("insert first number : "); scanf_s("%d", &num1);
printf("insert seconde number : "); scanf_s("%d", &num2);
printf("insert third number : "); scanf_s("%d", &num3);
printf("the min \n");
//printf("the min between these three is %d \n ", min(num1, num2, num3));
break;
case 'C':
case 'c':
printf("insert number : "); scanf_s("%d", &num);
if (is_positive(num) == ERROR)
{
printf("Error back to menue \n");
break;
}
else {
printf("the oposite of the number is %d \n ", oposite(num));
break;
}
case 'D':
case 'd':
printf("insert number : "); scanf_s("%d", &num);
if (is_positive(num) == ERROR)
{
printf("Error back to menue \n");
break;
}
else {
printf("the oposite of the number is %d \n ", (is_polendrom(num)== 0) ? "the number isn't a polindrom \n" : "the number is a polindrom \n");
break;
}
case 'E':
case 'e':
printf("insert number : "); scanf_s("%d", &num);
if (is_positive(num) == ERROR)
{
printf("Error back to menue \n");
break;
}
else {
printf("insert the base: "); scanf_s("%d", &base);
printf("the number in a detcimal base is %d \n ", numByBase10(num,base));
break;
}
case 'F':
case 'f':
printf("insert number : "); scanf_s("%d", &num);
if (num < 0)
num = (make_bolindrom(-1 * num))*-1;
else num = make_bolindrom(num);
break;
case 'G':
case 'g' :
printf("DoYou Really want to quit? y/n \n");
option=getchar(); enter=flushall_();
if (option == 'Y' || option == 'y')
printf("thx for using the system ! byee \n");
else
menue();
break;
}
if (option != 'Y' || option != 'y')
{
menue();
scanf_s("%c",&option);
}
}
}
The function is named _flushall, not flushall_.
Besides, you should enable a higher warning level to get warnings if you call functions that are not declared. This would have helped you detect the cause of the problem.
Related
Using just the switch statement works totally fine. My problem comes when inserting the do-while loop. On the first try, the program works smoothly, but in the second and following loops, after asking the user if he wants to try again by typing 1, the program "jumps" the part where the user writes its input (scanf("%d", &oper)) and executes the rest of the program considering the previously typed 1 as the input and messing up everything.
I´ve tried looking for the answer on similar questions and videos, but I´m quite new and I just can´t grasp it. I have another program with a similar problem. I will really appreciate your help :)
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;}
}while(select == 1);
} ```
I think you want something like bellow code.
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
do{
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
printf("Press 0 to stop and 1 to continue: ");
scanf("%d", &select);
} while(select == 1);
printf("Finish");
}
change the following code bellow:
while(select != 0);
Try this:
#include <stdio.h>
int main()
{
int num1, num2, oper, select;
printf("Enter the first number:");
scanf("%d", &num1);
printf("Enter the second number:");
scanf("%d", &num2);
printf("Enter the number 1-5 to for the operations\n");
printf("1-Addition\n");
printf("2-Subtraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("5-Modulo\n");
scanf("%d", &oper);
do{
printf("Press 0 to stop and 1 to continue");
scanf("%d", &select);
switch(oper){
case 1:
printf("The sum is %d\n", num1+num2);
break;
case 2:
printf("The difference is %d\n", num1-num2);
break;
case 3:
printf("The quotient is %d\n", num1/num2);
break;
case 4:
printf("The product is %d\n", num1*num2);
break;
case 5:
printf("The remainder is %d\n", num1%num2);
break;
}
}while(select != 0);
}
First of all, I am a total beginner to both C (and any programming) and Stack Overflow, so sorry if something like this has been asked before.
So I've been having trouble with my math operations code in the loop part. When I type in N or Y, the program acts as if I typed in a different option and says I have to type in Y or N.
Here's the code. Sorry if it's a jumbled mess, this is all I know so far.
#include <stdio.h>
int main() {
while (1) {
int choice1, choice2, num1, num2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%d", &choice2);
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
break; }
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
break; }
else {
printf("Pick y or n only");
break;
}
}
return 0;
}
the issue here is that in the if statement you are using break which breaks the flow, instead you should be using continue
major changes: (I've added comment line to make the changes more obvious for you)
int check = 1; //added a check variable
while (check) { //used check variable for while loop
printf("\n Try another operation [y/n]: ");
scanf("%c", &choice2); //changed %d to %c as the input is a char
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
continue; } //changed break with continue
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
check= 0; } //updated check so next iteration fails and terminates the loop
else {
printf("Error wrong input");
check= 0; //there cannot be a 2nd try using if else statement
}
there will be few more changes which I'll mark in the code below
#include <stdio.h>
int main() {
int check = 1; //added a check variable
while (check) { //used check variable for while loop
int choice1, choice2, num1, num2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%c", &choice2); //changed %d to %c as the input is a char
if (choice2 == 'y' || choice2 == 'Y') {
printf("Retrying...");
continue; } //changed break with continue
else if (choice2 == 'n' || choice2 == 'N') {
printf("Exiting...");
check= 0; } //updated check so next iteration fails and terminates the loop
else {
printf("Error wrong input");
check= 0; //there cannot be a 2nd try using if else statement
}
}
return 0;
}
for more detail, you can read the docs
I think you will have to do some major changes as shown below
#include <stdio.h>
int main() {
while (1) {
int choice1, num1, num2;
char choice2;
printf("\n [1] Addition\n [2] Subtraction\n [3] Multiplication\n [4] Division\n");
printf("\n Pick a choice: ");
scanf("%d", &choice1);
printf("\n Give a number: ");
scanf("%d", &num1);
printf("\n Give another number: ");
scanf("%d", &num2);
switch (choice1) {
case 1:
printf("Sum is %d", num1+num2);
break;
case 2:
printf("Difference is %d", num1-num2);
break;
case 3:
printf("Product is %d", num1*num2);
break;
case 4:
printf("Quotient is %d", num1/num2);
break;
default:
printf("Please select a valid operation");
}
printf("\n Try another operation [y/n]: ");
scanf("%s", choice2);
if (choice2 == 'y' || choice2 == 'Y')
{
scanf("%d", &choice1);
}
else;
{
printf("Exiting...");
}
}
return 0;
}
Can you help me with the code below? I am trying to make a program where users need to choose the following options:
1 - Multiplication table
2- Test
When the user chooses either one and it's finished running, the program will prompt users to choose the following:
1 - Another multiplication table
2 - Another test
3 - Exit
Problem occurs when I choose the 2nd option. Whenever I try to exit (3rd option), it goes back to 1st option. Everything is normal when I choose the 1st option and then the 3rd option. How do I solve this?
Here's my code:
int c1;
int num;
int multi;
int num1, num2;
int answer;
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d", &c1);
while (c1 != 1 && c1 != 2)
{
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d", &c1);
}
if (c1 == 1)
{
do
{
printf("\nChoose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d", &num);
while (num > 12)
{
printf("\nInvalid selection. Please choose again.\n");
printf("Choose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d", &num);
}
printf("\nMultiply of %d\n", num);
for (multi = 1; multi <= 12; multi++)
{
printf("%d x %d = %d\n", num, multi, num * multi);
}
printf("\nNext program?\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
while (c1 != 1 && c1 != 2 && c1 != 3)
{
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
}
} while (c1 == 1);
}
if (c1 == 2)
{
do
{
printf("\nEnter 1st number: ");
scanf("%d", &num1);
printf("Enter 2nd number: ");
scanf("%d", &num2);
printf("Your answer is: ");
scanf("%d", &answer);
if (answer == num1 * num2)
{
printf("\nYou are correct. The answer for %d times %d is %d.", num1, num2, num1 * num2);
}
else if (answer != num1 * num2)
{
printf("\nYou are incorrect. The answer for %d times %d is %d, not %d.", num1, num2,
num1 * num2, answer);
}
printf("\nNext program?\n");
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
while (c1 != 1 && c1 != 2 && c1 != 3)
{
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
}
do
{
printf("\nChoose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d", &num);
while (num > 12)
{
printf("\nInvalid selection. Please choose again.\n");
printf("Choose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d", &num);
}
printf("\nMultiply of %d\n", num);
for (multi = 1; multi <= 12; multi++)
{
printf("%d x %d = %d\n", num, multi, num * multi);
}
printf("\nNext program?\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
while (c1 != 1 && c1 != 2 && c1 != 3)
{
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d", &c1);
}
} while (c1 == 1);
} while (c1 == 2);
}
The do..while loop in C executes one time before checking. In the second if: "if (c1 == 2)" your code first performs the test then it asks again to choose. Even if your answer is "3" it executes one time the code in the following do..while section which is actually the code for the "multiplication table".
You can use the while loop instead of the do..while. However the code seems overcomplicated and some sections are repeated I'll suggest you to review the logic (also consider to use the switch statement).
Found the solution. Apparently, do while loop does not work well. Had to change to while loop. Here's the code:
int c1;
int num;
int multi;
int num1,num2;
int answer;
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d",&c1);
while(c1!=1 && c1!=2){
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("Choice: ");
scanf("%d",&c1);
}
while(c1==1 || c1==2){
if(c1==1){
printf("\nChoose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d",&num);
while(num>12){
printf("\nInvalid selection. Please choose again.\n");
printf("Choose a number ranging from 1 to 12.\n");
printf("Choice: ");
scanf("%d",&num);
}
printf("\nMultiply of %d\n",num);
for(multi=1;multi<=12;multi++){
printf("%d x %d = %d\n",num,multi,num*multi);
}
}else if(c1==2){
printf("\nEnter 1st number: ");
scanf("%d",&num1);
printf("Enter 2nd number: ");
scanf("%d",&num2);
printf("%d times %d equals to: ",num1,num2);
scanf("%d",&answer);
if(answer==num1*num2){
printf("Your answer is correct! %d times %d equals to %d.",num1,num2,num1*num2);
}else if(answer!=num1*num2){
printf("Your answer is incorrect! %d times %d equals to %d, not %d.",num1,num2,num1*num2,answer);
}
}
printf("\n\nNext program?\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d",&c1);
while(c1!=1 && c1!=2 && c1!=3){
printf("\nInvalid selection. Please choose again.\n");
printf("1 - Another multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
scanf("%d",&c1);
}
}
You shouldn't rely on getting the correct input from scanf(), the safer option is to read the input using fgets() and convert the value to an int using atoi().
int getValidNumberInput(void)
{
char buf[BUFSIZ];
int value = 0;
if (fgets(buf, sizeof(buf), stdin) != NULL)
value = atoi(buf);
return value;
}
The atoi() function returns 0 for a non numeric value so it's perfectly suitable for you needs.
As mentioned in the comments, the logic of the program is easily simplified by using a while() loop and a switch() statement.
int main(void)
{
bool hasLooped = false;
bool invalidChoice = false;
int choice;
while (1)
{
/* Display the relevant menu */
displayProgramMenu(hasLooped, invalidChoice);
/* Get the selected menu choice */
choice = getValidNumberInput();
if (choice < 1 || choice > 3)
invalidChoice = true;
else
invalidChoice = false;
/* Perform the chosen action */
switch (choice)
{
case 1:
doFirstOption();
hasLooped = true;
break;
case 2:
doSecondOption();
hasLooped = true;
break;
case 3:
exit(0);
break;
default:
break;
}
}
}
The use of the boolean variables allows the complexity to be moved into functions where it is easy to reason about.
void displayMenu(bool hasLooped, bool showInvalidMsg)
{
if (showInvalidMsg)
printf("\nInvalid selection. Please choose again.\n");
else if (hasLooped)
printf("\nNext program?\n");
if (hasLooped)
printf("1 - Another multiplication table\n");
else
printf("1 - Multiplication table\n");
printf("2 - Test\n");
printf("3 - Exit\n");
printf("Choice: ");
}
i m new in programing.
i've written a simple program.
i want to repeat the program again and again and it can only exit when user wants to exit.
here is my program
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
int num1, num2, a, m, s, choice;
float d;
printf("\nEnter The First Number: ");
scanf("%d", &num1);
printf("\nEnter The Second Number: ");
scanf("%d", &num2);
a=num1+num2;
m=num1*num2;
s=num1-num2;
d=(float)(num1/num2);
printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : ");
scanf(" %c", &ch);
switch(ch)
{
case 'A': printf("\nThe Addition Of The Number Is= %d", a);
break;
case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
break;
case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s);
break;
case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
break;
default : printf("\nInvalid Entry");
break;
}
printf("\nPress Any Key To Exit");
getch();
return 0;
}
and here is the output
"Enter The First Number: 10
Enter The Second Number: 10
Enter Your Choice
For Addition Type A
For Multipication Type M
For Division Type D
For Substraction Type S : A
The Addition Of The Number Is= 20
Press Any Key To Exit"
I want a line before the line Press Any Key To Exit
"If You Want To Calculate Again Press Y
or
Press Any Key To Exit"
when press Y then the program should start from the beginning.
How can i do this???
wrap the code inside a do{} while() ?
char answer;
do{
printf("\nEnter The First Number: ");
scanf("%d", &num1);
printf("\nEnter The Second Number: ");
scanf("%d", &num2);
a=num1+num2;
m=num1*num2;
s=num1-num2;
d=(float)(num1/num2);
printf("\nEnter Your Choice \nFor Addition Type A \nFor Multipication Type M \nFor Division Type D \nFor Substraction Type S : ");
scanf(" %c", &ch);
switch(ch)
{
case 'A': printf("\nThe Addition Of The Number Is= %d", a);
break;
case 'M': printf("\nThe Multipication Of The Numbers Is= %d", m);
break;
case 'S': printf("\nThe Substraction Of THe Numbers Is= %d", s);
break;
case 'D': printf("\nThe Division Of The Two Numbers Is= %f", d);
break;
default : printf("\nInvalid Entry");
break;
}
printf("\nPress Y to continue. Press any Key To Exit");
scanf(" %c",&answer); // dont forget type &
}
while(answer == 'y' || answer == 'Y');
Declare a variable, let's say answer, which will store the user answer when you ask for "Press Y to continue. Press any Key To Exit". Check to see what value has that variable. If is 'y' or 'Y', the loop will repeat. If the user pressed other key, the loop is over.
You can also use recursion, which is often used in more functional oriented programming languages.
Pseudo-code:
myfunction = do
...
b <- somethingtodo
...
if b
then myfunction
else return ()
Relative to Jens's solution, it would look like:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main (void)
{
char choice;
int num1, num2, cont;
printf("Enter the first number: ");
scanf("%d", &num1);
getchar ();
printf("Enter the second number: ");
scanf("%d", &num2);
getchar ();
printf("A - addition\n");
printf("S - subtraction\n");
printf("M - multiplication\n");
printf("D - division\n");
printf("[ASMD]? ");
choice = (char)toupper(getchar());
getchar ();
printf("\n");
switch(choice)
{
case 'A':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case 'S':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case 'M':
printf("%d * %d = %d", num1, num2, num1 * num2);
break;
case 'D':
if (num2 == 0)
fprintf(stderr, "The divisor can not be zero");
else
{
printf("%d / %d = %f", num1, num2, (double)num1 / num2);
}
break;
default :
fprintf(stderr, "Invalid entry");
break;
}
printf("\n");
for (;;)
{
printf("Continue [YN]? ");
cont = toupper(getchar());
getchar ();
if (cont == 'Y')
return main(); // the difference.
else if (cont == 'N')
return EXIT_SUCCESS;
}
}
I would move the calculation stuff in it's own function and then use while() in main.
I have tried to fix other problems as well (this solution only uses standard C functions).
#include <stdio.h> // puts, printf, fprintf, scanf, getchar, stderr, EOF
#include <stdlib.h> // exit, EXIT_SUCCESS, EXIT_FAILURE
#include <ctype.h> // toupper
char fail_on_eof (int c)
{
if (c == EOF)
exit (EXIT_FAILURE);
// In case of fail_on_eof (scanf (...)) the return value of this this
// function is not useful
// scanf () returns the number of chars read or EOF
// getchar () returns a char or EOF
return (char) c;
}
void skip_to_next_line (void)
{
char c;
do
{
c = fail_on_eof (getchar ());
} while (c != '\n');
}
char read_upcase_char_line (char* prompt)
{
char c;
printf ("%s ", prompt);
c = fail_on_eof (toupper (getchar ()));
skip_to_next_line ();
return c;
}
int read_num_line (char* prompt)
{
int num;
printf ("%s ", prompt);
fail_on_eof (scanf ("%d", &num));
skip_to_next_line ();
return num;
}
int calculate (void)
{
char choice;
int num1, num2, cont;
num1 = read_num_line ("Enter the first number:");
num2 = read_num_line ("Enter the second number:");
puts("A - addition");
puts("S - subtraction");
puts("M - multiplication");
puts("D - division");
choice = read_upcase_char_line ("[ASMD]?");
puts("");
switch(choice)
{
case 'A':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case 'S':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case 'M':
printf("%d * %d = %d", num1, num2, num1 * num2);
break;
case 'D':
if (num2 == 0)
// Better use stderr for error messages
fprintf(stderr, "The divisor can not be zero");
else
{
printf("%d / %d = %f", num1, num2, (double)num1 / num2);
}
break;
default :
// Better use stderr for error messages
fprintf(stderr, "Invalid entry");
break;
}
printf("\n");
for (;;)
{
cont = read_upcase_char_line ("Continue [YN]?");
if (cont == 'Y')
return -1;
else if (cont == 'N')
return 0;
}
}
int main(void)
{
while (calculate ());
return EXIT_SUCCESS; // Use this constant to avoid platform specific issues with the return code
}
I wrote below code:
#include<stdio.h>
int main() {
char o,r;
int x,y;
do {
printf("Enter operator: '+' or '-' or '*' or '/'\n");
scanf("%c",&o);
printf("Enter first number: ");
scanf("%d",&x);
printf("Enter second number: ");
scanf("%d",&y);
switch(o) {
case '+':
printf("Sum: %d",x+y);
break;
case '-':
printf("Subtract: %d",x-y);
break;
case '*':
printf("Multiply: %d",x*y);
break;
case '/':
printf("Division: %d",x/y);
break;
default:
printf("Wrong operator entered.");
}
printf("\nEnter y or Y to continue: ");
scanf("%c",&r); // r is not getting value?. why?
} while((r=='y')||(r=='Y'));
}
r is not getting value thus it is not doing what it supposed to do.
printf("\nEnter y or Y to continue: ");
scanf("%c",&r);
What seems to be the problem? Am I missing something?
Why does this code work fine in c++ if I use cin, but it fails when I use scanf in c?
You need to add the line (or something similar):
while(getchar() != '\n');
After you enter the second number, '\n', the newline character, is left in stdin and is being placed in r.
You can prove this by adding the following lines after the while loop:
if(r == '\n')
printf("\nnewline\n");
Enter operator: '+' or '-' or '*' or '/'
*
Enter first number: 5
Enter second number: 6
Multiply: 30
Enter y or Y to continue:
newline
check the below
#include<stdio.h>
int main()
{
char o,r;
int x,y;
do{
printf("Enter operator: '+' or '-' or '*' or '/'\n");
scanf(" %c",&o);
printf("Enter first number: ");
scanf("%d",&x);
printf("Enter second number: ");
scanf("%d",&y);
switch(o)
{
case '+':
printf("Sum: %d",x+y);
break;
case '-':
printf("Subtract: %d",x-y);
break;
case '*':
printf("Multiply: %d",x*y);
break;
case '/':
printf("Division: %d",x/y);
break;
default:
printf("Wrong operator entered.");
}
printf("\nEnter y or Y to continue: ");
scanf(" %c",&r);
} while((r=='y')||(r=='Y'));
}