Thread: More qusetions...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    More qusetions...

    I seem to be asking alot of questions but C++ is fun ! I was wandering how I could get my program to exit whenever 7 is entered and I was wandering about how I would clear the screen when the user wnats to do another calculation.

    Code:
    #include <iostream>
    
    unsigned short int MenuAndMenuChoice();
    double Add();
    double Subtract();
    double Multiply();
    double Divide();
    double CircleArea();
    double CircleCircumference();
    
    int main()
    {
        
        //Function - MenuAndMenuChoice is called to print the menu and get the user's choice
        switch (MenuAndMenuChoice())
        {
              
              //A descision is then made hear depending on what the user entered
              case 1: std::cout << "\nThe two numbers added together equal: " << Add();
              break;
              
              case 2: std::cout << "\nThe second number subtracted from the first number equals: " << Subtract();
              break;
              
              case 3: std::cout << "\nThe first number multiplied by the second equals: " << Multiply();
              break; 
              
              case 4: std::cout << "\nThe first number divided by the second equals: " << Divide();
              break;
              
              case 5: std::cout << "\nThe circle's area is: " << CircleArea();
              break;
              
              case 6: std::cout << "\nThe circle's circumference is: " << CircleCircumference();
              break;
              
        }
        
        std::cin.get();
        return 0;
    }
    
    //Function MenuAndMenuChoice code starts here
    unsigned short int MenuAndMenuChoice()
    {
        
        int MenuChoice;
        
        std::cout << "1.Add\n";
        std::cout << "2.Subtract\n";
        std::cout << "3.Multiply\n";
        std::cout << "4.Divide\n";
        std::cout << "5.Circle Area\n";
        std::cout << "6.Circle Circumference\n";
        std::cout << "7.Exit\n\n";
    
        std::cout << "What would you like to do? ";
        std::cin >> MenuChoice;
        
        return MenuChoice;
        
    }
    
    //Function Add code starts here
    double Add()
    {    
         
        double FirstNumber, SecondNumber, AddAnswer;
         
        std::cout << "\nWhat is the first number: ";
        std::cin >> FirstNumber;
        std::cin.ignore(80,'\n');
        
        std::cout << "What is the second number: ";
        std::cin >> SecondNumber;
        std::cin.ignore(80,'\n');
        
        AddAnswer = FirstNumber + SecondNumber;
        
        return AddAnswer;
        
    }
    
    //Function Subtract code starts here     
    double Subtract()
    {
        
        double FirstNumber, SecondNumber, SubtractAnswer;
        
        std::cout << "\nWhat is the first number: ";
        std::cin >> FirstNumber;
        std::cin.ignore(80,'\n');
        
        std::cout << "What number would you like to take away from the first number: ";
        std::cin >> SecondNumber;
        std::cin.ignore(80,'\n');
        
        SubtractAnswer = FirstNumber - SecondNumber;
        
        return SubtractAnswer;
        
    }
    
    //Function Multiply code starts here
    double Multiply()
    {
        
        double FirstNumber,SecondNumber, MultiplyAnswer;
        
        std::cout << "\nWhat is the first number: ";
        std::cin >> FirstNumber;
        std::cin.ignore(80,'\n');
        
        std::cout << "What do you want to multiply the first number by: ";
        std::cin >> SecondNumber;
        std::cin.ignore(80,'\n');
        
        MultiplyAnswer = FirstNumber * SecondNumber;
        
        return MultiplyAnswer;
        
    }
    
    //Function Divide code starts here
    double Divide()
    {
       
        double FirstNumber, SecondNumber, DivideAnswer;
        
        std::cout << "\nWhat is the first number: ";
        std::cin >> FirstNumber;
        std::cin.ignore(80,'\n');
        
        std::cout << "What do you want to divide the first number by: ";
        std::cin >> SecondNumber;
        std::cin.ignore(80,'\n');
        
        DivideAnswer = FirstNumber / SecondNumber;
        
        return DivideAnswer;
        
    }
    
    //Function CircleArea code starts here
    double CircleArea()
    {
        
        double CircleRadius, CircleAreaAnswer;
        const double Pi = 3.141592654;
     
        std::cout << "\nWhat is the circle's radius: ";
        std::cin >> CircleRadius;
        std::cin.ignore(80,'\n');
        
        CircleAreaAnswer = Pi*CircleRadius*CircleRadius;
        
        return CircleAreaAnswer;
        
    }
    
    //Function CircleCircumference code starts here
    double CircleCircumference()
    {
           
        double CircleRadius, CircleCircumferenceAnswer;
        const double Pi = 3.141592654;
        
        std::cout << "\nWhat is the circle's radius: ";
        std::cin >> CircleRadius;
        std::cin.ignore(80,'\n');
        
        CircleCircumferenceAnswer = 2*Pi*CircleRadius;
        
        return CircleCircumferenceAnswer;
        
    }

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    9
    Quote Originally Posted by ComDriver
    I was wandering how I could get my program to exit whenever 7 is entered and I was wandering about how I would clear the screen when the user wnats to do another calculation.

    Code:
    #include <iostream>
    
    unsigned short int MenuAndMenuChoice();
    double Add();
    double Subtract();
    double Multiply();
    double Divide();
    double CircleArea();
    double CircleCircumference();
    
    int main()
    {
        
        //Function - MenuAndMenuChoice is called to print the menu and get the user's choice
        switch (MenuAndMenuChoice())
        {
              
              //A descision is then made hear depending on what the user entered
              case 1: std::cout << "\nThe two numbers added together equal: " << Add();
              break;
              
              case 2: std::cout << "\nThe second number subtracted from the first number equals: " << Subtract();
              break;
              
              case 3: std::cout << "\nThe first number multiplied by the second equals: " << Multiply();
              break; 
              
              case 4: std::cout << "\nThe first number divided by the second equals: " << Divide();
              break;
              
              case 5: std::cout << "\nThe circle's area is: " << CircleArea();
              break;
              
              case 6: std::cout << "\nThe circle's circumference is: " << CircleCircumference();
              break;
              
        }
        
        std::cin.get();
        return 0;
    }
    Well your main doesn't look like it will keep prompting the user for choices until 7 is entered; looks like it will only prompt once and exit. So add
    Code:
    case 7:
    // Do nothing.
    break;
    to the above switch() statement and the program exists.

    Clearing the screen (console screen I assume) is a different story, depending on Windows or UNIX.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Clear the Screen

    I personally would move your switch ... case statement into your MenuAndMenuChoice() function, and make your main() function read:

    Code:
    while (1) { MenuAndMenuChoice() }
    That will just loop the menu until your user hits 7, in which case you add

    Code:
    case 7:
         exit(0);
    break;
    to your switch statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More qusetions 2 ...
    By ComDriver in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2005, 06:42 PM