How to use pointers with strings? - c++

I know my question is not specific but let me explain it this code
char name[5][30];
for (int i = 0; i < 5; i++)
cin >> name[i];
for (int i = 0; i < 5; i++)
cout<<name[i];
in the example above i created an array of characters where you can input five words each with 30 bit length. and it works just fine but when i try to use a pointer like so when you don't know how many words you are about to input. I get an error in line 5 saying a value of type int cant be asigned to char and i understand the error but how how to get pass this problem?
int n;
cout << "Number of names" << endl;
cin >> n;
int *name;
name = new char[n][30];
for (int i = 0; i < 5; i++){
cin >> *name;
name++;
}
for (int i = 0; i < 5; i++){
cout << *name;
name++;
}

Use char, not int.
Incrementing name doesn't seem good idea because it have to be returned to the first element before printing. I used array indexing operator.
I guess n input & output should be done instead of fixed 5 input & output.
int n;
cout << "Number of names" << endl;
cin >> n;
char (*name)[30];
name = new char[n][30];
for (int i = 0; i < n; i++){
cin >> name[i];
}
for (int i = 0; i < n; i++){
cout << name[i];
}
delete[] name;

Related

Why am I getting a "vector out of range" error?

When I try to run my code, it compiles fine, but during runtime it gives an out of range vector error. Can anyone help me out?
I have written my code in Xcode:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numOfRows = 0;
cout << "Enter number of rows: ";
cin >> numOfRows;
vector<vector<int>> vec;
int sizeOfAnotherArray = 0;
int value = 0;
for (int i = 0; i < numOfRows; i++) {
cout << "Enter size of another array: ";
cin >> sizeOfAnotherArray;
vec.resize(numOfRows,vector<int>(sizeOfAnotherArray));
for (int j = 0; j < sizeOfAnotherArray; j++) {
cout << "Store Value: ";
cin >> value;
vec.at(i).at(j) = value;
}
}
for (int i = 0; i < numOfRows; i++) {
for (int j = 0; j < sizeOfAnotherArray; j++) {
cout << vec.at(i).at(j) << " ";
}
cout << "\n";
}
return 0;
}
The odd thing about your code is that you enter sizeOfAnotherArray multiple times and therefore resize your whole array multiple times. But note that you only change the number of rows. Each row you add will have the latest size but earlier rows will keep the size they originally had.
This means that if one of the later values for sizeOfAnotherArray is larger than one of the earlier values then you are going to get an out of range error, because the earlier row will still have the smaller size.
I'm guessing that the code you meant to write is this. It creates a ragged array, which is an array where the number of columns varies depending on which row you are on.
cout << "Enter number of rows: ";
cin >> numOfRows;
vector<vector<int>> vec(numRows); // create array with N rows
for (int i = 0; i < numOfRows; i++) {
cout << "Enter size of another array: ";
cin >> sizeOfAnotherArray;
vec.at(i).resize(sizeOfAnotherArray); // resize this row only
for (int j = 0; j < sizeOfAnotherArray; j++) {
...
}
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec.at(i).size(); j++) { // loop on the size of this row
cout << vec.at(i).at(j) << " ";
}
cout << "\n";
}

Error: Cannot bind 'std::istream {aka std::basic_istream<char>}' lvalue to 'std::basic_istream<char>&&'

I am taking an input from user for the size of an array, and then the elements of it.
In the below code, cin>>A[i] in the first for loop is giving me an error.
From other questions similar to this one, it's a simple operator error, and the script is similar to working three dimensional ones I've seen. Is new creating a 3 dimensional array by default, meaning I'd need to define columns too? If not, where would I be missing the operator?
int** A;
int s;
cin >> s;
A = new int*[s];
for(int i=0;i<s;i++)
{
A[i]=new int[s];
cout<< "Enter value: ";
cin>>A[i];
}
cout<< "Array:\n";
for(int j=0;j<s;j++)
{
cout << A[j] << " ";
}
A[] is an int* pointer, not an int value.
There is no operator>> that can read an int value into an int* pointer. Since you want to read an int value, you have to read into an int variable, so change A[i] in your 1st loop to *A[i] instead:
cin >> *A[i];
You need to do the same with A[j] in the 2nd loop:
cout << *A[j] << " ";
This is because there is no operator<< to write an int value from an int* pointer, but there is an operator<< that can write the value of a memory address held by a void* pointer, and int* is implicitly convertible to void*.
Don't forget to delete[] your arrays when you are done with them:
int s;
cin >> s;
int** A = new int*[s];
for(int i = 0; i < s; ++i)
A[i] = new int[s];
for(int i = 0; i < s; ++i)
{
cout << "Enter value: ";
cin >> *A[i];
}
cout << "Array:\n";
for(int j = 0; j < s; ++j)
cout << *A[j] << " ";
for(int j = 0; j < s; ++j)
delete[] A[j];
delete[] A;
That being said, you are wasting memory for the second dimension when s > 1, since you are filling in and using only the 1st column and ignoring additional columns. The code you showed only really needs a 1-dimensional array instead:
int s;
cin >> s;
int* A = new int[s];
for(int i = 0; i < s; ++i)
{
cout << "Enter value: ";
cin >> A[i];
}
cout << "Array:\n";
for(int j = 0; j < s; ++j)
cout << A[j] << " ";
delete[] A;
If you really want a 2-dimensional array, try something more like this instead:
int rows, columns;
cin >> rows;
cin >> columns;
int** A = new int*[rows];
for(int i = 0; i < rows; ++i)
A[i] = new int[columns];
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < columns; ++j)
{
cout << "Enter value for (" << i << "," << j << "): ";
cin >> A[i][j];
}
}
cout << "Array:\n";
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < columns; ++j)
cout << A[i][j] << " ";
cout << endl;
}
for(int i = 0; i < rows; ++i)
delete A[i];
delete[] A;
That being said, you really should be using std::vector instead of new[] directly.
What array do you want to create? Two-dimensional SxS, or just S-sized?
Because you are creating an array of arrays, while trying to access it as a single-dimensional.
Changing int** A to int* A, A = new int*[s] to A = new int[s], and getting rid of A[i]=new int[s] in the first loop makes the code correct.

Cannot convert string to const char/string* to int*

Firstly, I want to say that I'm a beginner. Sorry for my stupid questions.
My program should ask for the amount of words you want to put in. It's specifically said that this tab length is the length of pointers tab pointing to the words tab (may sound confusing, but English isn't my first language, my apologies, I also dont really understand pointers yet).
The words tab should also have exact length for each word, hence the strlen. What am I doing wrong?
int il,len;
string x;
cout<<"Amount of words: ";
cin>>il;
int **t;
t=new int*[il];
for(int i=0; i<il; i++)
{
cout<<"Word: ";
cin>>x;
len=strlen(x);
t[i]=new string[len];
cout<<endl;
}
cout<<"You wrote:"<<endl;
for(int i=0; i<il; i++)
{
cout<<t[i];
delete [] t[i];
}
delete[] t;
strlen() doesn't take a class string object but instead it takes a pointer to character string char*:
len = strlen(x); // error so correct it to:
len = x.length();
also you cannot initialize a pointer to an integers to class string:
int **t;
t[i]=new string[len];
you really want an array of strings but the code is really a mess so if you want this how:
int il;
cout << "Amount of words: ";
cin >> il;
string *t;
t = new string[il];
for(int i = 0; i < il; i++)
{
cout << "Word: ";
cin >> t[i]; // there's no need for a temporary string `x`; you can directly input the elements inside the loop
cout << endl;
}
cout << "You wrote: " << endl;
for( int i = 0; i < il; i++)
cout << t[i] << endl;
delete[] t;

why does my counter increase according to the lens of char input in C++

I'm picking up on C++ recently and is trying to code a program which prompts for names for a defined no. of times and inserts each of the input into an array of size-5. The problem happened when I tried to run the following code, my counter, i increases according to the no of len the user input. Why is that so?
#include <iostream>
using namespace std;
int main(){
const int SIZE = 5;
char name[SIZE];
int i;
for (i = 0; i < SIZE; i++){
if (strlen(name) <= 50) {
cout << "Enter a name: \n";
cin >> name[i];
}
}
for (i = 0; i < SIZE; i++){
cout << name[i] << endl;
}
return 0;
}
Output:
if (strlen(name) <= 50) {
You should not call strlen on array which is not initialized.
Use array of strings otherwise
cout << name[i] << endl;
refers to i-th character, not entire string. Or if you want to go with char arrays, you'd need a two dimensional array.
I thing what you indended to do was :
#include <iostream>
using namespace std;
int main(){
const int SIZE = 5;
string names[SIZE];
int i;
for (i = 0; i < SIZE; i++){
cout << "Enter a name: \n";
string name;
cin>>name;
if (strlen(name) <= 50) {
cin >> names[i];
}
}
for (i = 0; i < SIZE; i++){
cout << name[i] << endl;
}
return 0;
}
UNTESTED
The second for loop, which does the output, does this in single characters, incrementing i each time.
To output the string all at once assign a string pointer to name[0] and send that to cout.

Program that receives 2 arrays and checks how many times 1 is included in the other

I need to write a program that receives 2 arrays and checks how many times 1 is included in the other...
But I cant find what is wrong with my program! tx!!
#include <iostream>
using namespace std;
int main()
{
int vector1[500];
int vector2[100];
int a = 0, b = 0, count = 0, k = 0;
cout << "enter size of first array:" << endl;
cin >> a;
cout << " enter first array values:" << endl;
for (int i = 0; i < a; i++)
cin >> vector1[i];
cout << "enter size of second array:" << endl;
cin >> b;
cout << "enter secound array values:" << endl;
for (int i = 0; i < b; i++)
cin >> vector2[i];
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i + k] == vector1[j])
{
count++;
k++;
}
else
k = 0;
cout << count << endl;
system("pause");
return 0;
}
Why at all do you need k? The problem is about all inclusions of all elements right? If O(n^2) complexity is fine, then...
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i] == vector1[j])
count++;
One obvious disadvantage of the code above is that you'll get the total sum of all occurences of elements from vector1 in vector2. The key idea remains the same in case you need to know, which elements exactly appeared in another array and how many times, you'll just have to use map or other vector.