Subscribe to A Journey Through My Geek Life        RSS Feed
-----

Finally, some results.

Icon 5 Comments
I've gone through the first few chapters of my textbook, so I've decided to write a small program to show off what I've learned thus far. I don't have it on me at the moment, but I can explain it and edit the blog later to include it. It pretty much asks the user for their name, and if I don't input my first name exactly, it quits (showing that I know how to compare strings). After that, it takes the user to a menu (showing that I know how to do if/else/if statements). The first option tells me my name, the second option allows me to append the rest of my name to my first name. The third option allows me to add, subtract, multiply or divide two numbers. Unfortunately, I'm not entirely sure how to loop back to the menu after the selected option is finished with. If I were able to do that, there's a bit of the program that will not allow the user to re-input the rest of their name, and the user would be able to see that their full name is used when they chose option 1 again. I hope I was able to explain it a bit better. Eventually, I'll change it up so that it compares the input name with a text document stored in the same directory.

EDIT: Here's the code. I'm actually in the process of updating the code to use the while statement.

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	string FName; 
	string MName;
	string LName;
	
	int Option = 1;
	bool OptionTwoUsed = 0;

	string Operator;

	double FNum = 0;
	double SNum = 0;
/*--------------------------------------------------------------------
This will ask what the user's name is. If the wrong name is given, the
program will quit.
*/
	cout << "What is your name:\n";
	getline  (cin, FName, '\n');

	if ( FName != "Landon" )
	{
		cout << "\nYou are unauthorized. This session will now end.\n\n";
		return 0;
	}

/*--------------------------------------------------------------------
This will ask the user what they want to do and store the option in 
the variable "Option"
*/
	cout << "Please choose an option (1-4):\n\n1 What is my name?\n2 Give the rest of your name.\n3 Let's do some math!\n4 Quit\n\n";
	cin >> Option;
	cout << "\n\n";
/*--------------------------------------------------------------------
This will check to see if the option the user enters is available. If 
it isn't, it quits.
*/
	if ( Option < 1 || Option > 4 )
	{
		cout << "\nYou have chosen an incorrect option. Quitting\n\n";
		return 0;
	}

/*--------------------------------------------------------------------
This sarcastically tells the user what their name is. 
*/
	else if ( Option == 1 )
	{
		cout << "\nReally? You don't remember your name? It's " << FName << "\n\n";
	}

/*--------------------------------------------------------------------
This asks the user to enter their middle and last name and appends them to 
the FName variable (meaning both First Name and Full Name, though it 
was by accident)
*/
	else if ( Option == 2 && OptionTwoUsed == 0 )
	{
		
		cout << "What is your middle name?\n\n";
		cin >> MName;
		cout << "What is your last name?\n\n";
		cin >> LName;

		FName = FName + " " + MName + " " + LName;

		cout << "Name appended. Hello " << FName << "!\n";
	}


/*--------------------------------------------------------------------
Asks the user to enter an operator and responds accordingly.
*/
	else if ( Option == 3 )
	{
		cout << "Enter an Operator (+, -, *, /)\n\n";
		cin >> Operator;

	/*------------------------------------------------------------
	Asks for two numbers and stores them in FNum and SNum 
	respectively. Then it adds them.
	*/
		if ( Operator == "+" )
		{
			cout << "Please enter two numbers.\nFirst number:\n";
			cin >> FNum;
			cout << "Second Number:\n";
			cin >> SNum;
			cout << FNum << " + " << SNum << " = " << (FNum + SNum) << "\n";
		}

	/*------------------------------------------------------------
	Asks for two numbers and stores them in FNum and SNum 
	respectively. Then it subtracts them.
	*/
		else if ( Operator == "-" )
		{
			cout << "Please enter two numbers.\nFirst number:\n";
			cin >> FNum;
			cout << "Second Number:\n";
			cin >> SNum;
			cout << FNum << " - " << SNum << " = " << (FNum - SNum) << "\n";
		}

	/*------------------------------------------------------------
	Asks for two numbers and stores them in FNum and SNum 
	respectively. Then it multiplies them.
	*/
		else if ( Operator == "*" )
		{
			cout << "Please enter two numbers.\nFirst number:\n";
			cin >> FNum;
			cout << "Second Number:\n";
			cin >> SNum;
			cout << FNum << " * " << SNum << " = " << (FNum * SNum) << "\n";
		}

	/*------------------------------------------------------------
	Asks for two numbers and stores them in FNum and SNum 
	respectively. Then it divides them.
	*/
		else if ( Operator == "/" )
		{
			cout << "Please enter two numbers.\nFirst number:\n";
			cin >> FNum;
			cout << "Second Number:\n";
			cin >> SNum;
			cout << FNum << " / " << SNum << " = " << (FNum / SNum) << "\n";
		}
	}
	else if ( Option == 4 )
	{
		return 0;
	}

/*--------------------------------------------------------------------
Note that this can only be used if I can get the menus to work properly.	
*/
	else
	{	
		cout << "Option 2 has already been used once.\n\n"; //Here, you return to the main menu.
	}
	return 0;
}

5 Comments On This Entry

Page 1 of 1

modi123_1 Icon

06 December 2012 - 12:39 PM

Quote

Unfortunately, I'm not entirely sure how to loop back to the menu after the selected option is finished with.


I am guessing this is a console app? Just use a while loop!

        Dim sInput As String = String.Empty '-- for the user input

	    '-- as long as the value isn't 'x' keep repeating.
	    '-- Also on the first go around - since empty is not 'x' this operates like a do-while loop.
        While sInput <> "x"
	    '-- the menu you want to print.
            Console.WriteLine("My Super Duper Cool Menu")
            Console.WriteLine("~~^~~^~~^~~^~~^~~^~~^~~^")
            Console.WriteLine("1: Do this thing")
            Console.WriteLine("2: Do that thing")
            Console.WriteLine("3: Do it there")
            Console.WriteLine("----------------")
            Console.WriteLine("x: to Exit")
            Console.Write("> ") '-- a nice little carrot to show we are expecting user inptu.

	    '-- Gets the user's input.
            sInput = Console.ReadLine.ToLower
	    '-- Clears the printed menu and the user's input.
            Console.Clear()

	    '-- determine what was entered.
            Select Case sInput
                Case "1"
                    Console.WriteLine("this!")
                Case "2"
                    Console.WriteLine("that!")
                Case "3"
                    Console.WriteLine("there!")
                Case "x"
                    Console.WriteLine("Goodbye!")
                Case Else
                    Console.WriteLine("Invalid input.")
            End Select

        End While

0

Dark_Necros Icon

06 December 2012 - 02:52 PM

modi123_1, on 06 December 2012 - 07:39 PM, said:

Quote

Unfortunately, I'm not entirely sure how to loop back to the menu after the selected option is finished with.


I am guessing this is a console app? Just use a while loop!


I'll look into this, thanks. Could while essentially replace if/then if?
0

modi123_1 Icon

06 December 2012 - 03:12 PM
No.. more like the switch statement would replace the if/else. The while loop is required to keep displaying the menu and accepting input.
0

Dark_Necros Icon

06 December 2012 - 03:20 PM
Hm.. Like I said, I'll look into it. Thanks for the help.
0

Dark_Necros Icon

08 December 2012 - 12:56 AM
I can't believe I forgot to update this post. I'll update to have the code now.
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

October 2014

S M T W T F S
   1234
567891011
12131415161718
19202122232425
2627282930 31  

Tags

    Recent Comments

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)