Cstring question

Hi, first post here. I'm a new programmer so I'm still trying to get familiar with everything.
I have an assignment where I need to read in a phone number into any array and I can't use the string data type. Assuming I need to use the getline function, how do I do this? I'm guessing it's with the cstring or .c_str() functions? I don't know. Thanks!
Function getline deals with either std::string or a character array.
right, so how do I make this work with a char array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  long int phone;
  
  cin >> phone; //Read in phone number
  int size = log10(phone) + 1;
  
  int Arr[size];
  for (int a = 0; a < size; ++a)
  {
    //Slice n' dice
    Arr[a] = phone / (pow(10, size -a -1));//fill array with individual numbers
    phone -= Arr[a] * (pow(10, size -a -1));
    cout <<Arr[a] ;
  }
  cout <<endl;
  return 0;
}


$ ./string
2130123  
2130123


OR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
  char phone[15];
  
  cout << "Enter phone number: ";
  cin.getline (phone, 15);
  
  
  for (int a = 0; phone[a] != '\0'; ++a)
    cout << phone[a];
  cout <<endl;
  return 0;
}


$ ./string
Enter phone number: 2130123
2130123
Last edited on
I need to read in a phone number into any array

Use a loop and cin to read in the individual digits of the phone number.
1
2
3
4
5
6
7
8
9
const PN=11;//assumes phone # has 11 digits
int digit=0;
int arr[PN]={0};
cout<<"enter eleven successive digits (no spaces)";
for(int i=0;i<PN;i++)
   { 
    cin>>digit;
    arr[i]=digit;
   }
Topic archived. No new replies allowed.