Cannot convert string to const char/string* to int* - c++

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;

Related

Not able to print or access last character of a char array in c++

I am trying to store individual character(including the spaces) of a sentence in a char array like "Do or die" but whenever I print the array (char arr) it does not print the last character in the array i.e. 'e'.
Can somebody tell why is this happening . I am a beginner in c++.
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the length of the array\n";
cin >> n;
char arr[n + 1];
cin.ignore(n, '\n');
cout << "Enter the characters of the array\n";
cin.getline(arr, n);
for(int i = 0; i < n; i++)
cout << arr[i] << endl;
return 0;
}
Here you need to understand how cin.getline(arr,n) works.
it will extract characters from the stream and stores them into arr until n characters have been written into arr (including the terminator null character).
So if you change this cin.getline(arr, n) to cin.getline(arr, n+1) this, it will work perfectly.
First in C++, the size of an array must be a compile-time constant.So, take for example the following code snippets:
int n = 10;
int arr[n]; //INCORRECT because n is not a constant expression
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, the following(which you did in your code example) is incorrect:
int n;
cout << "Enter the length of the array\n";
cin >> n;
char arr[n + 1]; //INCORRECT
Second the reason you're not getting the last character printed on screen is because the size of you array is n+1 but in your for loop:
for(int i = 0; i < n; i++)
you're only going upto n. You should replace the above with:
for(int i = 0; i < n + 1; i++)// note that +1 ADDED HERE
A better solution would be to use std::string for your purpose as shown below:
#include<iostream>
int main()
{
std::string inputString;
std::cout << "Enter the string"<<std::endl;
std::getline(std::cin, inputString);
//first way to print the string
std::cout << inputString << std::endl;
//second way to print the string
for(int i = 0; i < inputString.size(); ++i)
{
std::cout << inputString[i];
}
return 0;
}

input string as character array in c++

I have search a lot but i haven't find my answer
I want to write a program that will give an array with 3 members
and then i want to print array with 3 members :
char array[3] ;
for(int i=0;i<3;i++){
cin>>array[i] ;}
But the point is , i don't want to cin array members in character ,and i want this in string ;
but the another point is i don't want to use #include <string.h>
what i have to do ?
I want to give input array from user in this form:
char array[3]={"input1","input2","input3"}
for(int i=0;i<3;i++){
cin>>array[i] ;
}
cout<<array[0]<<" "<<array[1]<<" "<<array[2] ;
//output = input1 input2 input3
char array[3][10];
for (int i = 0; i < 3; i++) {
cin >> array[i];
cout << array[i] << endl;
}
Where the 2nd size of the array depends on the length of the input string.
char array[n] will only store n characters; use char array[n][m]. Remember, a c string (char[]) is different than string. In your case, you would want `char array[3][length]' where length is the max space to 'reserve' for each word.
char array[3][32]; //you wont need to fill this with data right away
for (int i = 0; i < 3; i++)
{
std::cin >> array[i];
}
std::cout << " " << array[0] << " " << array[1] << " " << array[2];
Hope This Solves!
#include <iostream>
using namespace std;
int main()
{
char arr[3][100]; // Declaring the two dimensional character array 3 denotes number of inputs whereas 100 dentoes the length.
for(int i=0;i<3;i++){
cin>>arr[i];
}
for(int j=0;j<3;j++){
cout<<arr[i]<<" ";
}
return 0;
}

Cannot Delete a pointer array inside a stuct pointer (c++)

Im having trouble with the syntax here. We are studying structures and pointers in class currently and are tasked with creating a dynamic array of a single structure with a pointer array inside to both be allocated and deleted by the end of the program. (Hopefully that made sense)
Here are the snippits of code im working with, note how the entry of scores works:
std::cin << stuArray[i].stuScore[j]
But then the deletion in a similar manner, does not:
delete[] stuArray[count].stuScore[j];
Deletion Code:
do
{
for (unsigned short j = 0; j < numTests; j++)
{
delete[] stuArray[count].stuScore[0]; //Syntax???????
}
count++;
} while (count < numStudents);
delete[] stuArray;
Score Entry Code (Which Works)
bool ScoreEntry(Student * stuArray, unsigned short numStudents, unsigned short numTests)
{
//Local Variables
unsigned short idTempChoice = 0;
//Get Id Number
std::cout << "\nChoose a student by ID and enter the test scores: ";
std::cin >> idTempChoice;
//Id lookup
for (unsigned short i = 0; i < numStudents; i++)
{
//Id Check
if (idTempChoice == stuArray[i].stuId)
{
std::cout << "Student selected: " << stuArray[i].stuName << std::endl;
//Score Entry
for (unsigned short j = 0; j < numTests; j++)
{
std::cout << "Test " << j + 1 << "'s Score: ";
std::cin >> stuArray[i].stuScore[j];
}//End For Loop j
return true;
}
}//End For Loop i
//Student Id not found
std::cout << "Student not found!\n";
return false;
}
Allocation Code (Struct):
void MemAllocation(Student * &stuArray, unsigned short &numStudents)
{
//Get Number of students
std::cout << "How many students have taken the test: ";
std::cin >> numStudents;
std::cout << std::endl;
//Dynamically allocate pointers
stuArray = new Student[numStudents];
}
Allocation Code (Pointer inside struct):
for (unsigned short i = 0; i < numTests; i++) //Allocate Dynamic array for each student
{
stuArray[i].stuScore = new float[numTests];
}
This is Literally all the code you need reference, this is not a bug its a syntax problem :)
Try delete[] stuArray[count].stuScore;
not delete[] stuArray[count].stuScore[j];
delete [] is made to delete an array allocated with new type[n]
You want to delete the pointer to the memory, not the actual memory.
You can delete delete[] stuArray[count].stuScore but not delete[] stuArray[count].stuScore[j] - drescherjm
Fixed using:
do
{
delete[] stuArray[count].stuScore;
count++;
} while (count < numTests);

How to use pointers with strings?

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;

Multi Dimension Arrays issue

OK so basically i want this code to get the user to input a value each time into the array and they get to specify the amount of rows and columns. I think i that the problem is that each time the user enters the value it goes into the correct column but also into both rows so by the end it only prints out the last lot of numbers the user had entered in all of the rows.
Sorry if that was hard to understand as this is my first post to this site and as you can probably tell i am only learning c++. So if you could help it would be greatly appreciated.
#include <iostream>
using namespace std;
int main()
{
int row;
int column;
int value[row][column];
cout << "How Many Rows?\n";
cin >> row;
cout << "How Many Columns\n";
cin >> column;
for(int x = 0; x<row; x++) {
for(int y = 0; y<column; y++) {
cout << "Enter Value Now\n";
cin >> value[x][y];
}
cout << endl;
}
cout << endl;
for(int a = 0; a < row; a++) {
for(int b = 0; b < column; b++) {
cout << value[a][b] << " ";
}
cout << endl;
}
}
int value[row][column];
declares an array whose dimensions are based on 2 uninitialised values.
If you don't have to use a C-style array, you could use
std::vector<std::vector<int>> value;
and choose its dimensions based on user input.
Alternatively, you could continue to use a C-style array if you allocate it like
int** value;
// input row/column
value = new int*[row];
for (int i=0; i<row; i++) {
value[i] = new int[column];
}
If you use the latter approach, make sure to also delete all dynamically allocated memory later.