strings in loops?

hi. im trying to make a loop that will end when the user types in 8 numbers for the telephone number. a function will read in the telephone number itself and the loop will call that function. the function has to send back the string either using a reference parameter or by returning a value. im not sure how to do this but this is what i have so far.
here is the main program:
1
2
3
4
5
6
7
8
int main()
{
    string telephone;
    
    for (int x = 0; x < telephone.length(); x++) {
   readoriginalnumber(); 
        
    }


this is the function:
1
2
3
4
5
6
7
8
string readoriginalnumber () {
    string telephone;
    
    cout<< "Enter your telephone number" << endl; 
    cin>> telephone;  
    cout<< endl << telephone << " is your original number" << endl;
    return telephone; 
}
When you read the input from cin, it reads the entire line. You don't need to loop through and read each character, which is what i think you are intending to do.
ok so i have found a way to use the while loop but i dont know how to make it stop after a user writes more than 14 numbers. i counted all the spaces and () and i made the number 14 instead of just numbers. and what do i print the function so that the original number is printed. right now the words print and not the numbers in front of them.
here is what i have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
using namespace std;
void readoriginalnumber (string &);

int main()
{
    string telephone[14];
    int i = 0;
    
    while (cin) {
        i++;
        readoriginalnumber (telephone[i]);
    }

    
    return 0;
}


void readoriginalnumber (string &) {
    string telephone[14];
    int i = 0;
    
    cout<< "Enter your telephone number" << endl; 
    getline (cin, telephone[i]);
    cout<< endl << telephone[14] << " is your original number" << endl;
    return;
}
I am confused why you are using a loop? It seems to me you are just taking in a phone number and then displaying it? Why not just do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;


int main()
{
   string number;

   cout<<"Enter your phone number"<<endl;
   getline(cin, number);

   cout<<"Your number is: "<<number<<endl;

   cout<<"Enter anything to continue..."<<endl;
   cin.ignore();

    
    return 0;
}



sorry i forgot to mention that i have to make the program read in more than 6 numbers and then break them down. for example the output should look like this:

(718) 343-3243      is the  original number
718 is the area code         343 is the exchange      3243 is the extension 


the function that i have above prints only the number and another function breaks it down and prints the rest. im not sure how the main program will send the original telephone number back to the function that breaks them down. im using a function with three reference parameters but i dont know what to do after that.
Ah! Okay... In that case I would still suggest reading in the numbers as one string. Then parse through the string pulling out exactly what you need. For example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include "AP_String_Helper.h"
using namespace std;


int main()
{
   string number,temp;
  AP_String_Helper help;


   cout<<"Enter your phone number"<<endl;
   getline(cin, number);

   cout<<"Your number is: "<<number<<endl;


   temp=help.remove(number,"("); //removes "("
   temp=help.remove(temp,")");
   temp=help.remove(temp,"-");

	cout<<"the area code is: "<<help.from_to(temp,0,3)<<endl;       
	cout<<"the exchange  is: "<<help.from_to(temp,3,6)<<endl;   
	cout<<"the extension is: "<<help.from_to(temp,6,temp.size())<<endl;   
	   
	  




   cout<<"Enter anything to continue..."<<endl;
   cin.ignore();

    
    return 0;
}


String Helper class can be found here: http://www.binary-thoughts.net/aplibrary.php
Topic archived. No new replies allowed.