Hello, I'm fairly new to C++.
I need help with a bit of code for the beginning of a menu program: I can get it to list the menu, but I am having trouble with the IF statements. Thoughts?
Also, what does
using namespace::std
mean? I cannot get any program to run without it or I get the error "undeclared identifier" for about every line of code I have.
//Project 2-1A
//Programmer: Argon
//Purpose: Introduce basics of creating a simple menu.
#include <iostream>
#include <string>
//The main function
int main ()
{
//Why is this required?
usingnamespace::std;
//Initialize the menu choice variable
int Choice;
//The following displays the user's menu choices on the screen
cout << "1) Add a New Record" << endl;
cout << "2) Modify an Existing Record" << endl;
cout << "3) Delete and Existing Record" << endl;
cout << "4) View/Print and Existing Record" << endl;
cout << "5) Exit" << endl;
//The following IF statements are supposed to provide output based on the //user's choice.
if (Choice==1)
cout << "You chose to add a new record. ";
if (Choice==2)
cout << "You chose to modify an existing record. ";
if (Choice==3)
cout << "You chose to delete an existing record. ";
if (Choice==4)
cout << "You chose to view/print an existing record. ";
if (Choice==5)
cout << "You chose to exit. ";
//This command pauses the program and prompts the user to press any key to continue.
system ("pause");
//The return statement ends the function.
return 0;
}
usingnamespace std;
Is used for accessing methods inside standard (std in short) namespace. cout is 1 method in that namespace. You can instead of cout << "Hello" << endl;
create same effect using std::cout << "Hello" << std::endl;
It just makes progams "easier" to create to use using-directive.
On your program:
Where do you use cin (or std::cin) operation to ask from user a input? cin >> Choice;
or std::cin >> Choice;