Anonymous
What can be the reason for this Terminate called after throwing an instance of std::logic_error
Check where it is throwing the exception and post that part of the code here.
Anonymous
How to identify it?
Use a debugger
Thomas shelby
Hi
Thomas shelby
I tried to write a code to get input from user
Hanz
Use a debugger
btw, 50%+ of programmers does not know how to debug. Any for-beginner resource? I need this too, ngl. I can only use valgind until now.
Thomas shelby
But can't figure out what's the problm with my code #include<stdio.h> void main() {     char string[50];     char ch;     int i;     printf("\n\n\t enter the name:");     for(i=0;i<50;i++)     {         ch= getche();         if(ch == 13)             break;         string[i] = ch;      }     string[i]="\0";     printf("\n\n\t hello");     for(i=0;string[i]!="\0";i++)     printf("%c",string[i]); }
Anshul
Use a debugger
Is it some application?
Anonymous
btw, 50%+ of programmers does not know how to debug. Any for-beginner resource? I need this too, ngl. I can only use valgind until now.
Not sure of good links. Check youtube tutorial videos and once you get a hang of it move on to reading the documentation for the debugger in question
Thomas shelby
Hanz
Yea
uh i just wrote a solution for linux 😂
Thomas shelby
😂
Hanz
Here's a solution for linux, lol: here
Anshul
Reading symbols from a.out...done. (gdb) run Starting program: /home/a.out 7 terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid Program received signal SIGABRT, Aborted. 0x00007ffff7519c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56 56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory. (gdb)
Anshul
this is what debugger returned
Anonymous
for this.....
Just type backtrace at the gdb command prompt and follow the sequence of calls to see where the exception was thrown in your code.
Anshul
#0 0x00007ffff7519c37 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56 #1 0x00007ffff751d028 in __GI_abort () at abort.c:89 #2 0x00007ffff7b2ae63 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #3 0x00007ffff7b2ffa6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #4 0x00007ffff7b2ffe1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #5 0x00007ffff7b30213 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #6 0x00007ffff7b2c709 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #7 0x00007ffff7b8b049 in char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #8 0x00007ffff7b8b3e0 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #9 0x000000000040128f in main () at main.cpp:132
Anshul
char *ptr=strtok(input," "); string cmp(ptr);
Anshul
char input[100];
Anonymous
char *ptr=strtok(input," "); string cmp(ptr);
strtok will return a null pointer if there are no tokens left. And if you try to construct a std::string from that null char* pointer, you will get the exception logic_error thrown
Anshul
but i'm making this call to strtok at start when i have tokens available
Anshul
int n; cin>>n; char input[100]; while(n--) { cin.getline(input,1000);
Anshul
int n; cin>>n; char input[100]; while(n--) { cin.getline(input,1000); char *ptr=strtok(input," "); string cmp(ptr); ...............................}
Anonymous
but i'm making this call to strtok at start when i have tokens available
Dont mix C and C++ code. Check the pointer returned by strtok to see if it is null. You can use the debugger. Just set a breakpoints at the line after the one where ptr is set and use GDB to print the value of ptr. It is in all likelihood a null pointer which is what is causing the exception. I cant help you anymore with this.
G
Write a complete C ++ program by using recursive function to calculate the sum of following Serie: sum=3+3/2+1+3/4+3/5+....3/n For example: input: inter the value of n: 10 out put: sum=8.7869..
G
solution please
G
No exam questions here
No, this is question
Golden Age Of
No, this is question
It's not question...
+
Hello again guys, after some testing I found out using the vector for store my chars is the best metohd for me so now Im reading all the data in a vector for some part I need to first read part of file into an structure and after that push it back to the vector ResourceHeader RCHeader{}; // structure file.read((char*)&RCHeader, sizeof(ResourceHeader)); std::vector<char*> buffer; buffer.push_back((char*)&RCHeader); // store header to vector for the structure I can read it to vector like this, so there is no problem but if I want to read some part of the file directly in a vector (I mean push it back at the end of it) how can I do it? do I need to first read it to a char buffer and then push_back that buffer to vector or I can do it directly?
Pritam
Hey
Anonymous
Hi
Pritam
Any one can help me for developing project in c language
klimi
what's the problem? (what's it about etc)
Anonymous
Hello guys I was wondering why I always get '8' as output when checking the size of a string. e.g. print(sizeof(very_long_string)) -> 8
Anonymous
A string made of a single char and an escape symbol would output 8 as well
Anonymous
through scanf
klimi
that's not the definition
Anonymous
Oh
klimi
do you define it as const char* = ... or char* = ... or char[] = ... ?
Anonymous
Sorry char* line = (char*)malloc(sizeof(char) * 20);
klimi
because that's the size of pointer
klimi
you are defining a pointer to char
Anonymous
well ofc it will return 8
Oh, true, thanks a lot
klimi
Oh, true, thanks a lot
if you need to get the length of the string you wil need to use strlen or similar function
klimi
you're welcome :)
Suka
btw, 50%+ of programmers does not know how to debug. Any for-beginner resource? I need this too, ngl. I can only use valgind until now.
usually it comes with ide/editor you're using. this for vscode https://code.visualstudio.com/docs/cpp/cpp-debug and this for qtcreator https://doc.qt.io/qtcreator/creator-debugging.html oc you can use gdb/lldb from command line. just read their manual. cmiiw
dipesh
//Write a program in C to find the maximum and minimum element in an array #include<stdio.h> #include<conio.h> int main() { int a[100],n,max,min,i,j,temp; printf("Enter the size of array\n"); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { for(j=1+i;j<n;j++) { if(a[j]>a[j+1]) { max=a[j]; } } for(j=1+i;j<n;j++) { if(a[j]<a[j+1]) { min=a[j]; } } } printf("Max=%d\nMin=%d",max,min); }
dipesh
Help me im not getting the ans
dipesh
The out put is not correct so...
klimi
The out put is not correct so...
please use the brain of yours.... what do you expect to get from us if you just say "i am not getting the ans" and "output is not correct"? We don't know anything about your problem, you haven't explained anything....
dipesh
I will sort it myself never mind
klimi
ffs...
Anonymous
//Write a program in C to find the maximum and minimum element in an array #include<stdio.h> #include<conio.h> int main() { int a[100],n,max,min,i,j,temp; printf("Enter the size of array\n"); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) { for(j=1+i;j<n;j++) { if(a[j]>a[j+1]) { max=a[j]; } } for(j=1+i;j<n;j++) { if(a[j]<a[j+1]) { min=a[j]; } } } printf("Max=%d\nMin=%d",max,min); }
You should initialize min to MAX value of integer and max to MIN value of integer. And you don't need nested for loops to find max and min. Your logic is wrong because you are comparing successive elements. So in your case you will print the smaller of the last 2 elements and the bigger of the last 2 elements as min and max respectively. You just need a single loop. Compare each element in the array to min and max. If the element is less than min set min to that element and likewise for max
klimi
Oh thanks
my bad, i didn't see the message above (idk i am blind sry)
Anonymous
Iam just a fresher
Anonymous
Can anyone guide me to where do I start
バレンタインがいない柴(食用不可)
Can anyone guide me to where do I start
C++ course in Coursera e.g.
Evans
https://en.cppreference.com/w/
Wow, Thanks 😊😊🤗
Anshul
source.cpp: In member function 'int User::getSlot(int, int, int)': source.cpp:348:26: error: invalid use of non-static member function 'bool User::compare(User::Event, User::Event)' 348 | sort(e.begin(),e.end(),compare); | ^~~~~ source.cpp:341:7: note: declared here 341 | bool compare(User::Event a,User::Event b) | ^~~~~
Anshul
what does this error mean
Anshul
vector<User::Event> e; bool compare(User::Event a,User::Event b) { return a.starting_time<b.starting_time; } int getSlot(int initial_time,int last_time,int duration) { sort(e.begin(),e.end(),compare);
Pavel
yes
It should be either a static or a free function. Otherwise it can't be used here (because it can't be called without an object)
Anshul
so by making it static function it won't have any problems?
Pavel
And also you probably need to pass an adress of the function (e.g. &compare)