Anonymous
Anshul
Anonymous
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]);
}
Thomas shelby
Anonymous
Hanz
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
Anshul
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.
Anonymous
Anshul
char *ptr=strtok(input," ");
string cmp(ptr);
Anshul
char input[100];
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
Golden Age Of
G
+
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
Pritam
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
klimi
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
klimi
because that's the size of pointer
klimi
you are defining a pointer to char
Anonymous
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
Anonymous
klimi
you're welcome :)
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
klimi
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
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)
dipesh
Anonymous
Iam just a fresher
Anonymous
Can anyone guide me to where do I start
バレンタインがいない柴(食用不可)
Evans
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
Pavel
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);
Anshul
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)
Pavel