D
maybe there're something like stack? like if you pointing in one address then you will jump on another and so on?
D
i mean cout << &char_array_1[0] << endl; // Hello world and cout << char_array_1[2] << endl; // l
Aryanshu
if (type_of_query == 1) { int x, y; scanf("%d %d", &x, &y); *(total_number_of_books+x) +=1; *(total_number_of_pages+x)=realloc( (total_number_of_p ages+x), *(total_number_of_books+x)*sizeof(int)); *(*(total_number_of_pages+x)+*(total_number_of_books+ x)-1)=y;
Aryanshu
Can anyone help me understand this code
Anonymous
#include<iostream> using namespace std; int main(){     int num,i;     cout<<"Enter a number:";     cin>>num;     for(i=2;i<num;i++){         if(num%i==0){             cout<<num<<" is not a prime number";             break;         }         else{             cout<<num<<" is a prime number";             break;         }     }     return 0; }
Anonymous
Help me
Anonymous
Help me
Write a code for checking prime
Sid
Also no need to use break inside else statement
Anonymous
Okk
Ludovic 'Archivist'
And that was the laziest thing i read that day from the commitee
Ludovic 'Archivist'
Where does it say that?
God helps if i remember the exact page but that was in a footnote about utf8 support
Anonymous
"A universal-character-name designates the character in ISO/IEC 10646 (if any) whose code point is the hexadecimal number represented by the sequence of hexadecimal-digits in the universal-character-name.   The program is ill-formed if that number is not a code point or if it is a surrogate code point.   Noncharacter code points and reserved code points are considered to designate separate characters distinct from any ISO/IEC 10646 character." [Note 1:  ISO/IEC 10646 code points are integers in the range [0,10FFFF] (hexadecimal).   A surrogate code point is a value in the range [D800,DFFF] (hexadecimal).   A control character is a character whose code point is in either of the ranges [0,1F] or [7F,9F] (hexadecimal).  — end note] This is what I was referring to in my message that you replied to.
Anonymous
God helps if i remember the exact page but that was in a footnote about utf8 support
You can't represent UTF-8, if you restrict a char8_t byte to the range 0-127. I think you misunderstood the standard.
Ludovic 'Archivist'
You can't represent UTF-8, if you restrict a char8_t byte to the range 0-127. I think you misunderstood the standard.
Oh no i didn't, if i was then my team was also misunderstanding it. But these types of shortcuts in standardisation are common tbf, it is what allows smaller c++ compiler to slowly evolve by giving leeway to the implementation
Anonymous
Oh no i didn't, if i was then my team was also misunderstanding it. But these types of shortcuts in standardisation are common tbf, it is what allows smaller c++ compiler to slowly evolve by giving leeway to the implementation
Not sure where in the standard it is mentioned as Undefined Behavior. I will try to search for it in a PDF copy of the standard that I have at home. I am not sure why the standard would define something as Undefined Behavior to give leeway to an implementation. BTW unicode suppprt has had time to mature since C++11. So not sure why the standard committee would feel the need to classify this as Undefined Behavior even in C++17.
Anonymous
Oh no i didn't, if i was then my team was also misunderstanding it. But these types of shortcuts in standardisation are common tbf, it is what allows smaller c++ compiler to slowly evolve by giving leeway to the implementation
I looked up https://eel.is/c++draft/ You are right. C++ standard requires u8 characters to be represented by a single code unit leaving the support of the rest to the implementation. This means code points with 2,3 or 4 bytes are implementation defined. But you were wrong when you said that it is Undefined Behavior. It is implementation defined. Btw g++, clang, Visual Studio, EDG and Apple Clang all have support for the full range of Unicode characters in UTF -8.
klimi
Bruh, wtf?
Sid
void ftoc(int a, int b, int j) { while (a <= b) { int c = (5.0 / 9) * (a - 32); cout << a << "\t" << c; cout << endl; a += j; } }
Sid
can any one tell me what's the error in this code?
Pranjal verma
No remove
Pranjal verma
🆗
Anonymous
can any one tell me what's the error in this code?
What is the error that you are getting? The code looks fine ignoring the double to int conversion.
Sid
Yup
Sid
It's fine
Anonymous
It's fine
Then why are you asking us to identify the error in the code?
Anonymous
lol
Anton
What???
I mean modification of 'a' will not be visible to caller (in case if that was expected).
Anonymous
Hi
Anonymous
I mean modification of 'a' will not be visible to caller (in case if that was expected).
What makes you think he wants the modification to be visible? All that he is doing is printing out Fahrenheit to Celsius table.
Anton
What makes you think he wants the modification to be visible? All that he is doing is printing out Fahrenheit to Celsius table.
Ok. I just thought this guy sent us some tricky question from school/university where one should find potential problems.
Anonymous
What is the best C android ide?
Anonymous
It should be free
Peace
how to store string in char array using vectors ?
ARx
iterate through both vector and copy the value of each char in the new vector
Peace
Is this a good way to store string in char array using vectors Or there is any better approach ? int main(){ string name="Honey Bansal"; vector <char> name_arr(name.begin(),name.end()); for(int i=0;i<name_arr.size();++i){ cout<<name_arr[i]; } return 0; }
olli
You mean working with string is much better than using char ?
Yes - in this case it's basically the same int main(){ string name="Honey Bansal"; for(int i=0;i<name.size();++i){ cout<<name[i]; } return 0; }
olli
What are you trying to do with the string?
Peace
What are you trying to do with the string?
working with databases, so I want as many functions I can use on string or char_array.
olli
working with databases, so I want as many functions I can use on string or char_array.
In this case I would argue it makes more sense to keep the string (which is basically a char array)
olli
okay..
otherwise you end up allocating more memory, end up performing unnecessary copies and so on.
Peace
otherwise you end up allocating more memory, end up performing unnecessary copies and so on.
Is is it a good way of printing string using pointers ? void printString(string *s){ cout<<*s; } int main(){ string name="Honey Bansal"; printString(&name); return 0; }
olli
Is is it a good way of printing string using pointers ? void printString(string *s){ cout<<*s; } int main(){ string name="Honey Bansal"; printString(&name); return 0; }
yes, that's fine. However it might be even better to use a const reference instead of a pointer (e.g. because the reference can't be null).
Sid
Then why are you asking us to identify the error in the code?
actually i had got the error after posting it here
EYE LORD
#include<iostream> using namespace std; int main(){ int cp >= 0; int spp >= 0; cout<<"enter the cost prise "<<endl; cin>>cp; cout<<"enter the selling prise "<<endl; cin>>spp; if (cp>spp) { cout<<"you made a proffit of "; else { cout<<"you enter a odd number"; } return 0;
Dr
Weird
Initialization is weird right!
Elnee
Please help
Lol, you even didn't bother do describe your problem.
Harshit
int cp,spp; cp = spp = 0; Inside first if You made profit of: << (spp-cp) << endl;
Harshit
if you want the inputs to be non negative then assert it after inputs are taken
Elnee
It's better to declare + initialize on different lines for each variable. Just in sake of the better code style: int cp{}; int spp{};
Elnee
And you should use zero-initialization syntax in modern C++
Harshit
Use clang-format as well for better readability
Elnee
https://en.cppreference.com/w/cpp/language/zero_initialization
Harshit
#include <iostream> int main() { int cp; std::cin >> cp; if (cp <= 0) { std::cout << "Cost price shouldn't be less than or 0" << std::endl; exit(1); } int profit; std::cin >> profit; if (profit <= 0) { std::cout << "Selling price shouldn't be less than or 0"; exit(1); } profit -= cp; if (profit > 0) std::cout << "Profit: " << profit; else std::cout <<" In loss of: " << profit; return 0; }
Harshit
pseudo code
Anonymous
Is is it a good way of printing string using pointers ? void printString(string *s){ cout<<*s; } int main(){ string name="Honey Bansal"; printString(&name); return 0; }
No it is not a good idea to use raw pointers in Modern C++. Raw pointer parameters place an additional burden on the caller to determine if the pointer needs to be allocated on the heap or it can be a pointer to data allocated on the stack. This could depend on what is being done inside the code of the called function. Smart pointers help alleviate this problem to an extent but you don't want to use them unless absolutely necessary. Use references to avoid this problem. If the called function doesn't have to change the reference (the name of the function should give you an idea) then it should be a const reference. Otherwise it can be a plain reference. If you want a function to accept only prvalues, then it should be a rvalue reference.
Peace
How could I include my cpp files into other file just like we include iostream and conio.h and all
Anonymous
How could I include my cpp files into other file just like we include iostream and conio.h and all
You shouldn't though nothing stops you from doing #include "a.cpp"
Harshit
Why did you used std everywhere?
Because didn't declare it in global namespace
Harshit
I actually don't understand the use of printString() func tbh
Peace
I actually don't understand the use of printString() func tbh
In future you will not use this way that you saying as you will be defining functions for each cout statements.😀