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
Hi guys I try to print a matrix with characters.I thought like this:
#include <iostream>
using namespace std;
int main()
{
char a[3][3]={"a","b","c","d","e","f","g","h","i"};
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j];
}
cout<<"\n";
}
}
What I doing wrong? Thank you in advance.
You have a 3 by 3 array of ints. You're trying to initialise each element of that array with string literals. There simply is no implicit conversion from the type of a string literal (const char[2] in this case) to int.
If you just want a matrix of characters, then make your array element type char. Then you need to use character literals with single quotes, instead of string literals.
char a[3][3]={'a','b','c','d','e','f','g','h','i'};
You probably want this:
char a[3][3]={'a','b','c','d','e','f','g','h','i'};
Change this:
int a[3][3]={"a","b","c","d","e","f","g","h","i"};
To:
char a[3][3]={{"a","b","c"},{"d","e","f"},{"g","h","i"}};
Try changing:
int a[3][3]={"a","b","c","d","e","f","g","h","i"};
To:
char a[3][3]={'a','b','c','d','e','f','g','h','i'};
That should get rid of casting characters to ints.
Edit: Changed the double quotations to single quotations
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
When I write a function like below and use it in main(), it doesn't work.
Why does this happen? (I am a beginner).
void addBst(char *name, char *num);
int main(void)
{
addBst("a", "b");
return 0;
}
In C++, a string literal is a const char[N] array, where N is the length of the string, including the null terminator.
Since C++11, it is illegal to assign a string literal to a non-const char* pointer, as your code is doing. You need to use a const char* pointer instead, eg:
void addBst(const char *name, const char *num);
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
I am new at the c++. I made basic random string program but I cant print the string to console. These are my codes :
you did not include <string> and also you take the string by copy, to edit it you'll have to pass it by reference using &
#include <string>
void randomString(std::string& line)
you don't need to assign a number to int and then assign it to char, char is an integer value in c++ so you can:
char character = rand()%122 + 97;
with this method your random numbers will not be very good, you can look into How to generate a random number in C++?
you access your string with [] operator and it has not yet been defined, to add a character to a string just use
line += character;
also if you want a random number length, there is no need for the boolean and i++ stuff:
int charNumb = rand() % 8 + 4;
while(charNumb--)
will do just fine and it looks much cleaner.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I was trying to send the address of a string to a function in C++ I'm but facing problems like in the code below:
#include<iostream>
#include<string>
using namespace std;
void try2(string *a){
cout<<a[2];
}
int main(){
string a;
getline(cin,a);//string length is greater than 10
try2(&a);
}
I am doing the same that we do for string in C. But I don't have that much idea about C++. Please suggest and describe how string behaves in C++.
You can pass a string into a function as an argument and take a string reference as a parameter in the function. But you need to understand that a string isn't an array, it is an object of the string class. The reason you can use indexing on a string is because the [] operator is overloaded for the string class.
What you were doing above is passing a pointer. For it to work the way you had done it your function would have to dereference the pointer:
#include<iostream>
#include<string>
using namespace std;
void try2(string *a){
cout<<(*a)[2];
return;
}
int main(){
string a;
getline(cin,a);//string length is greater than 10
try2(&a);
}
This would allow you to print the third character of the string object passed by reference to the function.
Note: don't use try as a function name as it is already reserved by many languages for the try-catch block syntax and can confuse the compiler.
You are accessing an single string as array of strings, which it is not. If you are trying to access the 2nd char of the string you have to dereference it first and then access the char like this:
#include<iostream>
#include<string>
using namespace std;
void try(string *a){
cout<<(*a)[2];
}
int main(){
string a;
getline(cin,a);//string length is greater than 10
try(&a);
}
This is because in c++ arrays and pointer are very close to each other.
*a and a[0] are almost the same which means using a[2] on a pointer isd the same as using *(a+2).
You have to keep in mind that you are not using an c-string here bit an c++-std::string which is an object and not an simple memory structure like in c.
So you first have to dereference the object before you can use it's overloaded []-operator to acess it's members (chars in this case).
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.
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 trying initialize element of array
array[m][n] == char("X");
after printing that element i'm getting the value of it equals д (Russian d); how deal with it, and I'm not even able to initialize that element without parsing const char to char.
You have to write simply as
array[m][n] = 'X';
where 'X' is a character literal.
Or if you like very much string literals then:)
array[m][n] = *"X";
or
array[m][n] = "X"[0];
EDIT: I am sorry. You have also to use the assignmnet operator (=) instead of the comparison operator (==)