Anonymous
scanf("%c",&op); this code isn't running
Official hooligan of Pius XII
scanf("%c",&op); this code isn't running
I mean, you technically enter "3\n". 3 goes to the num1, while '\n' stays in buffer and then goes to the op variable. Try entering for example 6d6 and you'll see the difference
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();
Official hooligan of Pius XII
You still have the error with using assignment = instead of comparison == in this code
The syntax problems are already solved, now he needed help with ignoring '\n' from buffer
Official hooligan of Pius XII
thank you so much, now it works perfectly
also, one more thing To get float result, you have to have at least one num value made float as well
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
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
Anonymous
Use single = to assign the value operation
thank you so much, now it's works perfectly
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
Ravi
I want printf to print a number with 20 digits
use long long int data type , find the reference pn google
Anonymous
use long long int data type , find the reference pn google
Even unsigned long long cant represent all 20 digit numbers. The maximum it can represent is less than 1.9 * 10^20.
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
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
Official hooligan of Pius XII
can anyone tell me how to find greatest number among 4
just make num1 a cache and then compare num2, num3 num4 with it if any of them is lesser than cache, make it cache
Anonymous
I am a beginner please suggest from where and how to learn
Anonymous
I tried it myself but there is something wrong in that so please anyone help me
Show us what u tried? And whats the error? Just dont say I tried.. show it. Use online pasting services to show the code.
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
Francis
#include <vector> //#include <set> #include <math.h> #include <numeric> //#include <algorithm> using namespace std; //function prototypes bool isPrime(int N); int getDifference(int N); bool isPrime(int N) { for (int i = 2; i <= sqrt(N); i++) { if (N % i == 0) return false; }; return true; } int getDifference(int N) { if(N == 2) return 2; if (N == 1) return 1; if(isPrime(N)) return 0; int aboveN, belowN, n1, n2; //finding the firstPrime above N n1 = N + 1; while(true) { if(isPrime(n1)) { aboveN = n1; break; } else n1++; }; //finding the first prime below N n2 = N -1; while(true) { if(isPrime(n2)) { belowN = n2; break; } else n2 --; }; int aboveDifference = aboveN - N; int belowDifference = N - belowN; return std::min(aboveDifference, belowDifference); }; int minimumNumber (vector <int> numbers ) { /*std::set <int> s1; for (int j{0}; j < numbers.size(); j++) { s1.insert(numbers[j]); };*/ int sumOfVector = accumulate(numbers.begin(), numbers.end(), 0); return getDifference(sumOfVector); }
Francis
This seems correct but it is a very slow algorithm. What are the constraints on the vector elements? Is it possible that the result of accumulate call is bigger than an int? In that case there will be an overflow and you will definitely return the wrong answer. And in getDifference, when N is 2 shouldnt you return 0?
Yeah for N == 2, it should be 0 sorry about that, the algos is good for a small vector but I get an error for huge vectors, it may be an overflow, any pointers on that plus also on the optimisation. For the vector constraints please ellaborate?
Anonymous
Yeah for N == 2, it should be 0 sorry about that, the algos is good for a small vector but I get an error for huge vectors, it may be an overflow, any pointers on that plus also on the optimisation. For the vector constraints please ellaborate?
By vector constraints, I meant the number of elements and the range of the elements (I.e. how big can they be) Use an unsigned long long instead of an int for all your computations. For IsPrime, your algo is very slow. You can make it twice as fast by skipping the even numbers. You can use Miller Rabin's test to filter out composite numbers much more faster. Only use isPrime for those numbers which are suggested to be primes by Miller Rabin's test.
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?
Anonymous
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?
Objects stored in a vector are copied into the vector. So it doesnt matter if it is a local object or a global object that you are copying into the vector. When you return the vector, it should be safe as long as you are not returning a reference to a vector object in local scipe
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 ?
Anonymous
Is it in stack if I create the class object in function ?
Yes. Any class data members (non static) are stored in stack when the class object is created in the stack. This is usually the size returned by sizeof. But your class itself could allocate memory on the heap.
Good
Am I Correct?
Anonymous
Allocate memory on the heap you mean if I use STL containers or smart pointers as a member variables it will store in heap
Even a regular pointer that you initialize with the return value from heap constructed memory. The pointer will be stored on the stack along with rest of your non static data members but it may be pointing to memory in heap.
Anonymous
Allocate memory on the heap you mean if I use STL containers or smart pointers as a member variables it will store in heap
Btw if you have a STL container as a data member like say a vector, then the non static data members of vector will be stored on the stack along with your class's data members.
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
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
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
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 ?
Anonymous
Yaa I need to do this.Can I use reference_wrapper ?
std::ref is an utility that creates a 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?