Thread: C++ Function Program Help Needed - Assignment due Very Soon Please Help!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    C++ Function Program Help Needed - Assignment due Very Soon Please Help!

    Hi everyone,
    I will try to give you a breakdown of what my assignment is in the fastest terms. I need to write a program where I create my own functions and use single while loops. The user is to enter a "user choice," which is only supposed to be 1 or 2. If the user picks 1, then the user is supposed to enter a height and a symbol as well, and then the program is supposed to print a right triangle out of the symbol and height entered. If the user picks 2, then they also enter the symbol and height, but also the width. This choice is supposed to print a rectangle. If the user picks something other than 1 or 2, then the program is supposed to provide an error message and ask again UNTIL the user enters either 1 or 2. In addition, only the first function I create can have inputs, after that the other functions can only have single while loops and no inputs. I know that this is a lot of information, but I'm just trying to be as detailed as possible.

    Here is my code:
    Code:
    #include <iostream>
    
    using namespace std;
    
            // variable declarations
            int height; // used to store the height
            int width; // used to store the width
            int userChoice; // used to store the user's choice
            int num; // used to store the number of symbols
            char symbol; // used to store the symbol the user enters
            
            
            // function prototypes
            void DisplayMenu();
            void DrawLineOfSymbol(int num, char symbol);
            void DrawTriangle(int height, char symbol);
            void GetRectangleDimension(int& height, int& width);
            void DrawRectangle(int height, int width);
            
            
            // funtion definitions
            void DisplayMenu() // function heading
            
            
            // Precondition: None
            // Postcondition: Menu is printed
            
            // function body for DisplayMenu
            {
                // display a menu and read and process the user's choice
                
                cout << "______________________________________" << endl;
                cout << " " << endl;
                cout << "This program reads and processes the" << endl
                     << "user's choice until the user wants to" << endl
                     << "quit." << endl;
                cout << "______________________________________" << endl;
                
                cout << " " << endl;
                
                cout << "Please enter your choice (1 or 2): " << endl;
                cin >> userChoice;
                
                cout << "Please enter the number of symbols: " << endl;
                cin >> num;
                
                // function call for DrawLineOfSymbol function
                //DrawLineOfSymbol(int num, char symbol); 
                
                // get height and symbol if user choice is 1
                
                if (userChoice == 1)
                {
                    cout << "Please enter the height: " << endl;
                    cin >> height;
                    cout << "You entered: " << height << endl;
                    
                    cout << "Please enter the symbol: " << endl;
                    cin >> symbol;
                    cout << "You entered: " << symbol << endl;
                    
                    DrawTriangle(height, symbol); // function call
                }
                
                // call DrawRectangle function if user choice is 2
                
                if (userChoice == 2) // Function call for DrawRectangle Function
                {
                    GetRectangleDimension(height, width);
                    DrawRectangle(height, width); 
                }
                
                // Print error message if user choice is not 1 or 2
                
                if (userChoice !=1 || userChoice !=2)
                {
                    cout << "Invalid choice. Please try again." << endl;
                    
                }
            }
            
            void DrawLineOfSymbol(int num, char symbol) // function heading
            
            // Precondition: DisplayMenu function has been called
            // Postcondition: Prints line of symbols
    
            // Function body for DrawLineOfSymbol
            // Pass-by-reference parameter
            
            {
                
                
                while (num > 0) // test
                {
                    cout << symbol;
                    num--; // update
                }
            }
            
            void DrawTriangle(int height, char symbol) // function heading
            
            // Precondition: userChoice == 1 
            // Postcondition: Draws right triangle of symbol
    
            // Function body for DrawTriangle
            // Pass-by-reference parameter
            
            {
                int counter = 0; // initalize counter
                
                while(counter <= height)
                {
                        DrawLineOfSymbol(num, symbol);
                        counter++;
                }
            }
            
            void GetRectangleDimension(int& height, int& width) // function heading
            
            // Precondition: userChoice == 2 
            // Postcondition: Gets the dimension of a rectangle from the user
    
            // Function body for GetRectangleDimension
            // Pass-by-reference parameter
            
            {
                    cout << "Please enter the height: " << endl;
                    cin >> height;
                    cout << "You entered: " << height << endl;
                
                    cout << "Please enter the width: " << endl;
                    cin >> width;
                    cout << "You entered: " << width << endl;
            }
            
            void DrawRectangle(int height, int width) // function heading
            
            // Precondition: userChoice == 2
            // Postcondition: Draws a rectangle of the user's symbol and dimensions
            
            // Function body for DrawRectangle
            // Pass-by-reference parameter
            
            {
                int counter = 0; // initalize counter
                
                while(counter <= height)
                {
                        DrawLineOfSymbol(num, symbol);
                        counter++;
                }
            }
                
            
            // Main function
            int main()
            {
                DisplayMenu(); // function call
            
                return 0;
            }
    This code is just a work in progress, so I probably have some additional errors, but the first one I am running into is that it says that I am not entering a valid symbol in my output. Here is my output:

    Code:
    ______________________________________
     
    This program reads and processes the
    user's choice until the user wants to
    quit.
    ______________________________________
     
    Please enter your choice (1 or 2): 
    1
    Please enter the number of symbols: 
    6
    Please enter the height: 
    5
    You entered: 5
    Please enter the symbol: 
    *
    You entered: *
    ************************************Invalid choice. Please try again.
    Can someone please help me with my code and show me the right way to have this so it outputs correctly? Thank you for helping me!

  2. #2
    spaghetticode
    Guest
    I haven't read all of your code by now, since I have to leave for work in a few minutes. But the first thing I encountered is this:

    Code:
    if (userChoice !=1 || userChoice !=2)
                {
                    cout << "Invalid choice. Please try again." << endl;
                     
                }
    which may not do what you want it to do. Use && here, because if the user enters 2, this will just check for the first condition userChoice != 1, which holds true for 2. In any || statement this enough to make the whole statement true, which causes the if statement to ignore the second condition.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24

    I hope this is what you wanted to implement

    Please do avoid global variables...I have just tweaked your program....

    Code:
    #include <iostream>
     
    using namespace std;
     
            
             
             
            // function prototypes
            void DisplayMenu();
            void DrawLineOfSymbol(int num, char symbol);
            void DrawTriangle(int height, char symbol);
            void DrawRectangle(int height, int width,char symbol);
             
             
            // funtion definitions
            void DisplayMenu() // function heading
             
             
            // Precondition: None
            // Postcondition: Menu is printed
             
            // function body for DisplayMenu
            {
                // display a menu and read and process the user's choice
                 // variable declarations
                 int height; // used to store the height
                 int width; // used to store the width
                 int userChoice; // used to store the user's choice
                 int num; // used to store the number of symbols
                 char symbol; // used to store the symbol the user enters
            
                cout << "______________________________________" << endl;
                cout << " " << endl;
                cout << "This program reads and processes the" << endl
                     << "user's choice until the user wants to" << endl
                     << "quit." << endl;
                cout << "______________________________________" << endl;
                 
                cout << " " << endl;
                 do
                 {
                cout << "Please enter your choice (1 or 2): " << endl;
                cin >> userChoice;
                 
                // function call for DrawLineOfSymbol function
                //DrawLineOfSymbol(int num, char symbol); 
                 
                // get height and symbol if user choice is 1
                 
                if (userChoice == 1)
                {
                    cout << "Please enter the height: " << endl;
                    cin >> height;
                    cout << "You entered: " << height << endl;
                     
                    cout << "Please enter the symbol: " << endl;
                    cin >> symbol;
                    cout << "You entered: " << symbol << endl;
                     
                    DrawTriangle(height, symbol); // function call
                }
                // call DrawRectangle function if user choice is 2
                else if (userChoice == 2) // Function call for DrawRectangle Function
                {
                    cout << "Please enter the height: " << endl;
                    cin >> height;
                    cout << "You entered: " << height << endl;
                 
                    cout << "Please enter the width: " << endl;
                    cin >> width;
                    cout << "You entered: " << width << endl;
                    cout << "Please enter the symbol: " << endl;
                    cin >> symbol;
                    cout << "You entered: " << symbol << endl;
                    DrawRectangle(height, width,symbol); 
                }
                // Print error message if user choice is not 1 or 2
                else
                {
                    cout << "Invalid choice. Please try again." << endl;
                     
                }
                }while(userChoice!=1 && userChoice!=2);
            }
             
            void DrawLineOfSymbol(int num, char symbol) // function heading
             
            // Precondition: DisplayMenu function has been called
            // Postcondition: Prints line of symbols
     
            // Function body for DrawLineOfSymbol
            // Pass-by-reference parameter
             
            {
                 
                int i=0; 
                while (i<num) // test
                {
                    cout<<symbol;
                    i++; // update
                }
            }
             
            void DrawTriangle(int height, char symbol) // function heading
            // Precondition: userChoice == 1 
            // Postcondition: Draws right triangle of symbol
     
            // Function body for DrawTriangle
            // Pass-by-reference parameter
            {
                int i=1;
                while(i<=height)
                {
                                DrawLineOfSymbol(i,symbol);
                                cout<<endl;
                                i++;
                }
            }
             
             
            void DrawRectangle(int height, int width,char symbol) // function heading
             
            // Precondition: userChoice == 2
            // Postcondition: Draws a rectangle of the user's symbol and dimensions
             
            // Function body for DrawRectangle
            // Pass-by-reference parameter
             
            {
                int counter = 1; // initalize counter
                 
                while(counter <= height)
                {
                        DrawLineOfSymbol(width, symbol);
                        cout<<endl;
                        counter++;
                }
            }
                 
             
            // Main function
            int main()
            {
                DisplayMenu(); // function call
                cin.get();
                cin.get();
                return 0;
            }
    Why are you using number of symbols???
    Last edited by subhash.rao; 10-17-2011 at 11:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Desperate help needed. C++ Uni Assignment.
    By ILLaViTaR in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2009, 01:30 PM
  3. Help with a C program assignment. Please?
    By Loopah in forum C Programming
    Replies: 3
    Last Post: 11-16-2005, 08:37 PM
  4. Replies: 2
    Last Post: 05-27-2005, 05:05 PM
  5. Help with Program Assignment
    By kdb3395 in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 09:11 PM

Tags for this Thread