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;
}
Related
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;
}
I have the following code:
#include <iostream>
using namespace std;
int main()
{
int length = 0;
int arrA[length];
cout << "Enter the length : ";
cin >> length;
cout << "Enter " << length << " integers for array : ";
for(int i = 0; i < length; i++)
{
cin >> arrA[i];
}
cout << "Array A : ";
for(int i = 0; i < length; i++)
{
cout << arrA[i] << " ";
}
cout << endl;
}
The above require the user to input the length of array followed by the integers that will be store in array. It work but the printing of the array value is incorrect when I input length 8 and above.
Working
Enter the length : 7
Enter 7 integers for array : 7 6 5 4 3 2 1
Array A : 7 6 5 4 3 2 1
Not working
Enter the length : 8
Enter 8 integers for array : 8 7 6 5 4 3 2 1
Array A : 8
Could this be due to memory issue or something?
int length = 0;
int arrA[length];
This creates an array of size zero
cout << "Enter the length : ";
cin >> length;
This sets the length variable to a value entered by the user but doesn't change the size of arrA which has already been created.
You obviously think that when the length variable changes that the array arrA will change as well, but this is not true.
Because you are accessing elements of an array which has zero size the behaviour of your program is undefined.
In addition this whole approach is incorrect because
int length;
...
int arrA[length];
is not legal C++. Because length is a variable this is a variable length array or VLA. VLAs are legal in C but are not legal in C++.
The best way to write this code in C++ is to use a vector. A vector is the C++ improvement on a C array.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int length;
cout << "Enter the length : ";
cin >> length;
vector<int> arrA(length);
...
int length = 0;
int arrA[length];
This is not valid C++.
What you may want to do is:
int *arrA = new int[length];
after cin >> length.
You will have to free the pointer later with delete[] arrA;
It is not due to a memory issue. But because variable length arrays are not part of the C++ standard.
length is a variable length array.
Read more about variable length arrays
Here
Here is the code if you do not want to use a vector. I would suggest not to use this for multifarious reasons. But go ahead and knock yourself out.
#include <iostream>
using namespace std;
int main()
{
int length = 0;
int *arrA = new int [length];
cout << "Enter the length : ";
cin >> length;
cout << "Enter " << length << " integers for array : ";
for (int i = 0; i < length; i++)
{
cin >> arrA[i];
}
cout << "Array A : ";
for (int i = 0; i < length; i++)
{
cout << arrA[i] << " ";
}
delete[] arrA;
cout << endl;
return 0;
}
The array should be dynamic, but it is fixed.
Printing the array gives nothing.
I'm trying to create a dynamic char array, get a string line from user, save line string char by char into the array.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char* lenptr = NULL; // declare a pointer initialized with null
int leng, arrsz;
string mystr;
cout << "enter a string: ";
getline(cin, mystr); // input a line of string
leng = mystr.length(); // get length of string
cout << "string length: " << leng << endl;
lenptr = new char[leng+1]; // declare a dynamic char array with length+1
for (int i = 0; i < leng; i++)
{
lenptr[i]=mystr[i]; // fill array with saved string
}
lenptr[leng] = '\0'; // save '\0' in last array cell
arrsz = sizeof(lenptr); // get size of array
cout << "size of array after saving " << leng << " chars: " << arrsz << endl;
cout << "string in array: ";
for (int j = 0; j < leng; j++) // print array
{
lenptr[j];
}
cout << endl;
// delete pointer
delete[] lenptr;
lenptr = NULL;
system("pause"); // pause system
return 0;
}
enter a string: helloworld
string length: 10
size of array after saving 10 chars: 8
string in array:
Press any key to continue . . .
You can't get the size of an array allocated with new[]. Although std::string is the recommended way to go about this, a vector would've been a more viable choice if the string absolutely had to be stored in a data structure.
At any rate, your program wasn't printing the characters of the array because you forgot a std::cout in the for loop:
for (int j = 0; j < leng; j++) // print array
{
cout<<lenptr[j];
}
and to actually print out the size of the array just use the leng variable you defined earlier:
cout << "size of array after saving " << leng << " chars: " << leng + 1 << endl;
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;
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.