dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Two Beginner Questions

  1. #1
    Join Date
    Jul 2015
    Posts
    4

    Exclamation Two Beginner Questions

    I am a beginner c++ learner and I'm trying to learn how to build classes with minimal use of pass by reference so I learn more object oriented programming. I am building a very small phone simulator. This is my code.

    Code:
    // Example program
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
        static int live = 0;
        static int text = 0;
    
    class PhoneNumber {
        
        public:
        
        static int MaxPhones;
        PhoneNumber();
        PhoneNumber(string, string, string, int);
        void input();
        void dial(PhoneNumber &);
        void txt(PhoneNumber &);
        void display();
        bool maxTextsreached();
        void displaySummaryinfo(PhoneNumber&, PhoneNumber &);
    
    
        
        private:
        
    
        const int MAXTEXTS = 10;
        string areaCode;
        string exchange;
        string line;
        int nlive;
        int ntext;
    
    
     
    };
    
    PhoneNumber::PhoneNumber(){
        nlive = 0;
        ntext = 0;
        
    }
    
    
    
    PhoneNumber::PhoneNumber(string aC, string e, string l, int m){
        aC = areaCode;
        e = exchange;
        l = line;
        m = MAXTEXTS;
    }
    
    void PhoneNumber::input(){
            
       cout << "Enter phone number in (NNN) NNN-NNNN format: ";
       cin.ignore(0);
       cin >> setw( 4 ) >> areaCode;
       cin.ignore( 1 ); // skip ( and space
       cin >> setw( 4 ) >> exchange;
       cin.ignore();
       cin >> setw( 5 ) >> line;  // input line
    }
    
    void PhoneNumber::display(){
        
        cout << "(" << areaCode << ")" << exchange << "-" << line; 
    }
    
    void PhoneNumber::dial(PhoneNumber &p2){
            
        this->display();
        cout << " is Calling phone number ";
        p2.display();
        cout << " ...call made." << endl;
         cout << "Printing nlive: " << live << endl;
            
        live++; 
        nlive++;
    }
    
    void PhoneNumber::txt(PhoneNumber &p1){
        
        display();
        cout << " is texting number " ;
        p1.display();
        cout << " .....message sent." << endl;
         cout << "Printing ntext: " << text << endl;
        
        
       text++;
       ntext++;
    }
    
    bool PhoneNumber::maxTextsreached(){
     
        if (text <= 10){
            return true;
        } else { 
            cout << "The maximum number of texts has been reached." << endl;
        return false; 
      } 
    }
    
    void PhoneNumber::displaySummaryinfo(PhoneNumber &p1, PhoneNumber &p2){
        cout << "A total of " << live << " calls and " << text << " texts were made." <<endl;
        cout << "Phone number " << PhoneNumber::p1.display << " made " << nlive << "calls and " << ntext << " texts." << endl;
        cout << "Phone number " << PhoneNumber::p2.display <.< " made " << nlive << "calls and " << ntext << " texts." << endl;
        
    }
    int main() {
    
     PhoneNumber p1, p2;  // create two instances of PhoneClass
     char answer;
     bool maxTextsreached = false;
    
    
    
     p1.input(); // input the phone number into the first phone object
     p2.input(); // input the phone number into the second phone object
    
     p1.dial(p2);  // simulate phone 1 dialing phone 2
     p2.txt(p1);   // simulate phone 2 sending a text to phone 1
    
    do {
     cout << "Enter c to make a a call, t to text, s for summary information, or x to exit. " << endl;
        cin >> answer;
        
        
            if (answer == 'c'){
                p1.dial(p2);
                
                
            }
        
            else if (answer == 't'){
            bool maxTextsreached(text);
            p2.txt(p1);
                
            }
               
            else if (answer == 's'){
                ;  
            }
               
     } while (answer != 'x');
        
        cout << "You made " << live << " calls and " << text << " texts." << endl;
            
        return 0;
     
    if (answer == 'y'){
        main();
    }
    else 
    return 0;
    }
    My bool function won't work, and I'm trying to create a function where I can count the number of phone calls each phone makes, but I can't call the display function in displaySummaryInfo, even with passing by reference. I'm stuck. Does anyone know how to help me?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,925

    Re: Two Beginner Questions

    displaySummaryInfo() shouldn't be a class member function. Within the class provide getter functions for nlive and ntext.

    maxTextsreached() does what it is supposed to do. The issue is with how you use it. Try
    Code:
    if (maxTextsreached())
        p2.txt(p1);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  3. #3
    Join Date
    Jul 2015
    Posts
    4

    Re: Two Beginner Questions

    It's telling me that I can't use maxTextsreached as a function

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,925

    Re: Two Beginner Questions

    Sorry, I transcribed it incorrectly waiting for a compiler build . It should be
    Code:
    else if (answer == 't') {
            if (p2.maxTextsreached())
               p2.txt(p1);
    }
    as maxTextsreached() is a member function of PhoneNumber class. You don't need the main() variable maxTextsreached.
    Last edited by 2kaud; August 2nd, 2015 at 04:48 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)