Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm using strcpy to copy a word from sentence into array of string. But this code is not working as expected. Please point out error if any..
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int i=0,j,k;
char a[100],*str[100];
char *b;
cout<<"enter a sentence:\n";
cin.getline(a,100);
str[0] = strtok(a," ");
while(str[i]!=NULL)
{
i=i+1;
k=strlen(strtok(NULL," "));
str[i]=new char[k+1];
strcpy(str[i],strtok(NULL," "));
}
for(i=0;;i++)
{
if(str[i]!=NULL)
cout<<"\n"<<str[i];
else
break;
}
return 0;
}
Two successive calls strtok(NULL," ") will not return twice the same pointer to a word in your string, as you seem to expect, but rather pointers to successive words. On the other hand you do seem to expect to get a pointer to a new token when you go around your while loop. That is not how things work.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Here's what I tried
#include <iostream>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
const int n{5},s{9},arr[]{2,3,7,8,11};
vector<vector<bool>>dp;
void print(){
for(auto &x :dp){
for(bool y:x){
cout<<y<<' ';
}
cout<<endl;
}
}
int main(){
print();
}
The program crashes with nothing being printed.
What is the correct way to do it. Also what does auto stand for in this case.
What is the correct way to do it.
Your example program is a correct way.
The program crashes
It shouldn't. Either you are mistaken, or there is something wrong with your system.
... with nothing being printed.
This makes sense. You are printing a vector of vector of bool that is empty. There is nothing to print.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm studing about c++ semantics and syntax, I really don't know what is the problem with this code, it compile but stop working. I will apreciate your help, thanks.
#include <iostream>
#include <string.h>
using namespace std;
char* func(char* M)
{
int initval = 2;
char *x= new char[10];
x="idea";
strcpy(x, M+initval);
return x;
}
int main()
{
char* x;
char s[10]= "alguna";
x= func(s);
cout << *x << endl;
return 0;
}
Before this is closed, the x="idea"; is where your problem lies. You throw away your buffer and point it to a constant value, then try to assign to it, which almost always is illegal (should always be illegal, but apparently it is compiling for you...).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is the code I've written for reversing a string using recursion.
I have tried it in 2 ways:
One with C
One with C++
The C version works as expected but, when I used the C++ version it doesn't give any output and continuously reads input.
void reverse() // C++ version
{
char ch;
cin>>ch;
if(ch!='\n')reverse();
cout<<ch;
}
void reverse() // C version
{
char ch;
scanf("%c",&ch);
if(ch!='\n')reverse();
printf("%c",ch);
}
int main(void)
{
reverse();
return 0;
}
What is the problem with the C++ code?
The problem is that cin>>ch won't read a whitespace character. Instead you can use cin.get.
Like:
std::cin.get (ch);
As #MohitJain Said, you can use cin.read also.
In your C++ reverse(), Use cin.get(ch); instead of cin>>ch; as the latter won't read \n.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been asked to find bug in the following code
This code just prints abcdef, it seems pretty harmless to me but will appreciate any suggestions
#include <iostream>
using namespace std;
const char* append(const char* s1, const char* s2) {
string s(s1);
s += s2;
return s.c_str();
}
void foo() {
const char* total = append("abc", "def");
cout<<"total = "<<total<<endl;
}
int main() {
foo();
return 0;
}
You have a dangling pointer. s is destroyed when it goes out of scope, meaning the pointer to its internal data is no longer valid.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am running the below code. I am getting run time error.
#include <iostream>
using namespace std;
int main() {
char *p="hello";
//p="Hi";
*p='G';
cout<<*p;
return 0;
}
if this is giving error then what is use of const char *p="hello";In this case my string should be constant not in char *p="hello"
char *p="hello";
*p='G';
You make p point to a constant, "hello". But then you try to modify what p points to. By definition, constants cannot be modified.