Anonymous
scanf("%c",&op); this code isn't running
Anonymous
Anonymous
how to get input from the user?
Anonymous
as a seperate input
Official hooligan of Pius XII
how to get input from the user?
printf("Enter a number: ");
scanf(" %d", &num1);
printf("Enter an operator [d = /, m = *, a = +, s = - ]: ");
scanf(" %c",&op);
printf("Enter another number: ");
scanf(" %d", &num2);
Official hooligan of Pius XII
do it this way, the space will ignore the white sign
Official hooligan of Pius XII
Another possible way is to use getchar() to ignore this \n
printf("Enter a number: ");
scanf("%d", &num1);
getchar();
printf("Enter an operator [d = /, m = *, a = +, s = - ]: ");
scanf("%c",&op);
getchar();
printf("Enter another number: ");
scanf("%d", &num2);
getchar();
Pavel
Pavel
Anonymous
Official hooligan of Pius XII
because int/int always gives you int
Anonymous
idk what's the reason, code gives me wrong results,
Enter a number: 66
Enter an operator (d = /, m = *, a = +, s = - ): d
Enter another number: 11
Answer = 55.000000
...Program finished with exit code 0
Press ENTER to exit console.
Here's the code again:
#include <stdio.h>
int main()
{
float num1,num2,value;
char op;
printf("Enter a number: ");
scanf(" %f", &num1);
printf("Enter an operator (d = /, m = *, a = +, s = - ): ");
scanf(" %c",&op);
printf("Enter another number: ");
scanf(" %f", &num2);
if (op = 'd') value = num1/num2;
if (op = 'm') value = num1*num2;
if (op = 'a') value = num1+num2;
if (op = 's') value = num1-num2;
printf("Answer = %f", value);
return 0;
}
Anonymous
Anonymous
Because of this
I changed like this
if (op == 'd') value == (num1/num2);
if (op == 'm') value == (num1*num2);
if (op == 'a') value == (num1+num2);
if (op == 's') value == (num1-num2);
but still results are same
Futuristic Programmer
idk what's the reason, code gives me wrong results,
Enter a number: 66
Enter an operator (d = /, m = *, a = +, s = - ): d
Enter another number: 11
Answer = 55.000000
...Program finished with exit code 0
Press ENTER to exit console.
Here's the code again:
#include <stdio.h>
int main()
{
float num1,num2,value;
char op;
printf("Enter a number: ");
scanf(" %f", &num1);
printf("Enter an operator (d = /, m = *, a = +, s = - ): ");
scanf(" %c",&op);
printf("Enter another number: ");
scanf(" %f", &num2);
if (op = 'd') value = num1/num2;
if (op = 'm') value = num1*num2;
if (op = 'a') value = num1+num2;
if (op = 's') value = num1-num2;
printf("Answer = %f", value);
return 0;
}
Your assigning op = ‘d’ which in turn is assigned to ‘m’ , to ‘a’ and lastly to ‘s’,,therefore making you program execute the last statement...
Instead use == instead of = in you if statements.
Futuristic Programmer
Anonymous
Futuristic Programmer
Official hooligan of Pius XII
Remember that == is used for comparing two values, while = assigns rvalue to the lvalue
Eturnus
I want to make a simple calculator, What's wrong in this code?
#include <stdio.h>
int main()
{
int num1,num2,value;
char op;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter an operator [d = /, m = *, a = *, s = - ]: ");
scanf("%c",&op);
printf("Enter another number: ");
scanf("%d", &num2);
if (op = d), value = num1/num2;
if (op = m), value = num1*num2;
if (op = a), value = num1+num2;
if (op = s), value = num1-num2;
printf("Answer = %d", value);
return 0;
}
#include <stdio.h>
int main()
{
float num1,num2;
float value;
char op;
printf("Enter an operator [d = /, m = *, a = +, s = - ]: ");
scanf("%c",&op);
printf("Enter a number: ");
scanf("%f", &num1);
printf("Enter another number: ");
scanf("%f", &num2);
if (op=='d')
{
value = num1/num2;
printf("Answer = %f", value);
}
else if (op == 'm')
{ value = num1*num2;
printf("Answer = %f", value);
}
else if (op == 'a')
{ value = num1+num2;
printf("Answer = %f", value);
}
else if (op=='s')
{ value = num1-num2;
printf("Answer = %f", value);
}
else
printf("Please try again...");
return 0;
}
Anonymous
I want printf to print a number with 20 digits
Anonymous
But it only show 10 digits
Anonymous
I want printf to print a number with 20 digits
How are you storing the 20 digits? What is the data type that you are using to represent the number.
Like I said earlier you have to use GMP's arbitrary precision integer and use the printing capability that the library provides. The built in types in C are not capable of representing numbers that long. Dont keep asking this same question again and again
Anonymous
Kush
can anyone tell me how to find greatest number among 4
Kush
I tried it myself but there is something wrong in that so please anyone help me
𝙊𝑙ẽ𝘨
hello guys! Really need your help
𝙊𝑙ẽ𝘨
can somebody tell me how to use nghttp2 to send requests with proxy
Anonymous
I am a beginner please suggest from where and how to learn
BranchScope
Anonymous
Anonymous
Thankyou both of you
•‿•
i want to learn series problem in c programming i search a lot YouTube video but i didn't get
can someone suggest me from where i should learn ?
Francis
hello ladies and gents can someone help me out with this code, the task is to find the minimum difference between the sum of the ints in the vector and the closest prime number. Here is what I have done so far but I keep failing in one of the hidden tests
Anonymous
Francis
Francis
Anonymous
#include <stdio.h>
//#include <iostream>
// VC: _MSC_VER
// GCC / G++: GNUC
#define min_days 4
#define max_days 10
int main() {
int nums = 0;
printf("---=== BTM Temperature Calculator V2.0 ===---\n");
printf("Please enter the number of days, between 3 and 10, inclusive: 2\n");
int tmpnum = 0;
tmpnum = 41;
if (!tmpnum < 41) {
tmpnum = 41;
}
#ifdef _MSC_VER
scanf_s("%d", &tmpnum);
#endif
#ifdef GNUC
scanf("%d", &tmpnum);
#endif
nums = tmpnum;
//scanf_s("%d", &nums);
while (nums!=4) {
printf("Invalid entry, please enter a number between 3 and 10, inclusive: 4\n");
int tmpnum = 0;
tmpnum = 41;
if (!tmpnum < 41) {
tmpnum = 41;
}
#ifdef _MSC_VER
scanf_s("%d", &tmpnum);
#endif
#ifdef GNUC
scanf("%d", &tmpnum);
#endif
nums = tmpnum;
}
while (nums < 4 || nums > 10 ) {
//printf("Please enter the number of days, between 3 and 10,");
printf("Please enter the number of days, between 3 and 10, inclusive: ");
int tmpnums = 0;
#ifdef _MSC_VER
scanf_s("%d", &tmpnums);
#endif
#ifdef GNUC
scanf("%d", &tmpnums);
#endif
nums = tmpnums;
//printf("inclusive: %d\n", nums);
printf("%d\n", nums);
}
int hi_temps[max_days];
int low_temps[max_days];
int ii = 0;
for (; ii < max_days;ii++) {
low_temps[ii] = 41;
hi_temps[ii] = 41;
}
int iii = 0;
for (; iii < nums; iii++) {
int hitemps;
int tmplowtemps;
#ifdef _MSC_VER
scanf_s("%d", &hitemps);
scanf_s("%d", &tmplowtemps);
#endif
#ifdef GNUC
scanf("%d", &hitemps);
scanf("%d", &tmplowtemps);
#endif
hi_temps[iii] = hitemps;
low_temps[iii] = tmplowtemps;
//Day 1 - High:
printf("Day %d - High: %d\n", iii, hi_temps[iii]);
printf("Day %d - Low: %d\n", iii, low_temps[iii]);
}
printf("Day Hi Low\n");
//printf("Day %d - Low: %d\n", iii, low_temps[iii]);
int iiii = 0;
for (; iiii < nums;iiii++) {
printf("%d %d %d\n", iiii, hi_temps[iiii],low_temps[iiii]);
}
return 0;
}
Anonymous
In line number 3 of your output:
The output should be:
Please enter the number of days, between 3 and 10, inclusive: Invalid entry, please enter a number between 3 and 10, inclusive: Day 1 - High: Day 1 - Low: Day 2 - High: Day 2 - Low: Day 3 - High: Day 3 - Low: Day 4 - High: Day 4 - Low: Day 5 - High: Day 5 - Low: Day Hi Low
^
But your output is:
Please enter the number of days, between 3 and 10, inclusive: 2
^
Unmatched character details:
The character in column 63 is supposed to be:
[I] ASCII code(73)
but you printed
[2] ASCII code(50)
Anonymous
please help me !!!
Anonymous
its not match
Talula
its not match
The above program is not even close to what your output is supposed to be.
Talula
And based on the output you want I can't even understand what exactly is needed.
Good
I am trying to store the vector of class A objects in a function and returning it.I have one doubt is there any issue with the objects after returning to caller function
Good
Because I am thinking the objects are created inside the function and its local to the function so got confused whether it's a proper way of doing or not?
Good
Okk that's for the clarification I made the vector as member variable so it's should works fine I think
Good
I am trying to understand Cpp memory layout.I understand functions and variables memory details but I have one question related to class members .I know local variable stored I. Stack and static and global variables Store in data segment but where exactly class members stores
Good
Is it in stack if I create the class object in function ?
Good
Good
Am I Correct?
Good
Good
Yaa but the vector stores the values in heap right?
Good
Vector member variable stores in stack
Anonymous
Yaa but the vector stores the values in heap right?
Yes the values you push back into the vector are stored in the heap. But the vector class itself maintains some members that are used to track info about the vector. So if you have vector as a data member of your class and your class object is on the stack, then these specific data members of the vector class are also stored on the stack along with your class's data members.
Good
Good
I have a class which having std::mutex as member variable , I try to create the class object and add to the vector compiler throwing error saying use of deleted function for std::mutex, after googling I understand mutex can't be copyable, not movable (not sure).How can I resolve this problem I tried to create the plain object and try with smart pointers still it's same error while doing push_back
Anonymous
Good
Ya will paste it
Anonymous
Ya will paste it
No use a site like pastebin.com and just post the link here.
Good
Yaa will use pastebin
Good
Anonymous
https://pastebin.com/8jWGMJuj
Why do you need to do this? Mutexes cant be copied and hence objects that contain them can also not be copied which is a requirement for objects being stored in a vector.
If you must do this, then you can use std::ref to store a reference instead in the vector.
Good
Yaa I need to do this.Can I use reference_wrapper ?
Good
okk will try that thanks for the information
Anshul
When any memory Is dynamically allocated then that memory exists from the start of the program till the end of the program and gets released once program ends. Then why is it advised to delete that memory manually?
And also if I create an object dynamically, then at the time when program ends why the destructor doesn't run for this dynamically created object?