0

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 
2
  • 1
    What do you want to take as input a char or a string? Commented Dec 8, 2020 at 10:54
  • i want to declare a char array BUT with string members @Tushar Commented Dec 8, 2020 at 11:02

3 Answers 3

1
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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];

1 Comment

Doesn't seem to be the answer. Please consider it moving to the comments.
1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.