1 Replies - 265 Views - Last Post: 10 December 2012 - 06:19 PM Rate Topic: -----

#1 Iky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 26-November 12

strings and loops?

Posted 10 December 2012 - 05:38 PM

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:
int main()
{
    string telephone;
    
    for (int x = 0; x < telephone.length(); x++) {
   readoriginalnumber(); 
        
    }


here is the function:
string readoriginalnumber () {
    string telephone;
    
    cout<< "Enter your telephone number" << endl; 
    cin>> telephone;  
    cout<< endl << telephone << " is your original number" << endl;
    return telephone; 
}


Is This A Good Question/Topic? 0
  • +

Replies To: strings and loops?

#2 Iky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 26-November 12

Re: strings and loops?

Posted 10 December 2012 - 06:19 PM

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1