C++ :: Allow User To Select Repeatedly Operation To Be Performed From Displayed Menu Of Actions
May 1, 2013
Write a user-friendly C++ program that allows a user to select repeatedly an operation to be performed from a displayed menu of actions. The menu includes the following items:
The menu actions are described by the following points:
Simple Statistics: This action includes finding the average, and standard deviation of a list of data items. The option should allow the user to specify the name of a file from which the data are read into an array. All the results of this option are displayed on the screen.
Vector Addition: This action allows the user to enter the name of a file from which two vectors, V1 and V2, of equal size N are read. Accordingly, the two vectors are added, and displayed along with the resultant vector on the screen. Your program should check that the two added vectors are of the same size. Otherwise, the program should display a message and allow the user to make another choice from the menu.
Dot Product: This action is similar to the Vector Addition in terms of input, output, and checking validity of the vector operands.
Exit: This action terminates the program.
Your program should include at least the following functions:
1. A function, called ReadVector(), which reads a vector of any size from a given file name, and returns the vector and its size to the caller. The function prototype is defined as follows:
void ReadVector(double V[], int &size, ifstream infile);
2. A function, called DisplayMenu(), which displays the menu and returns the user’s choice as an integer value between 1 and 4. The function prototype is defined as follows:
int DisplayMenu (void);
3. A function, called VectorAdd(), which performs the addition of two vectors, A and B, and returns the resultant vector, C. The function prototype is defined as follows:
void VectorAdd(const double A[], const double B[], double C[], int size);
The sum of two vectors, A = (a1, a2, …, an) and B = (b1, b2, …, bn) of size n, is defined as: C = A + B.
Where, C = (c1, c2, …, cn), and ci = ai + bi, for1 i n .
4. A function, called DotProduct(), which performs the dot product of two vectors, A and B, and returns the result. The function prototype is defined as follows:
void DotProduct(const double A[], const double B[], int size, double &result);
The dot-product of two vectors A = (a1, a2, …, an) and B = (b1, b2, …, bn) of size n is defined as
n n
n
i
i i b a ....... b a b a b a B . A
1 1 2 2
1
5. A function, called Simple_Stat(), which finds the maximum, minimum, average, and standard deviation of a list of data items passed to it by a one-dimensional array. The function returns the average and standard deviation values. The function prototype is defined as follows:
void Simple_Stat(const double list[], int size, double &average, double
&std_dev);
I have created a generic array-like template to create arrays of different types. Now I need to get user input from a menu about which type of array they are building. I tried having the user enter a number and using that number to pick a string from an array of constant strings. But pretty obviously, that threw a type conversion error. Is there a way to convert the literal string of a type into it's type or refer to a type directly.
Here is the code I need to fit a type into at runtime:
I am looking for direction on what topic I should be reading up on. I am new to C++ and Windows MFC.
This is my real world problem, in the context of the application user. (these term do not refer to OPP concepts)
I want to create shapes (containers) in an application that will respond and collect other objects;
Imagine a Windows frame, containing several 2 dimensional squares. I want to be able to drag and drop marbles into the squares,and have the square retain and display the marbles in the square, in the order that they were dropped in.
How do I create the shapes, and how will the square sense when a marble is over it?
How would I create irregular shapes (a combination of lines and curves) that would be responsive to the marbles?
I am asking for input for a char by using the _getch() function. The thing is that when a key is pressed multiple times it screws the program because it executes every single key that is entered.
I am asking for the user to enter a letter that will determine an action to be used in battle, like attack, magic, "use item", etc.
char option; cout<<"Enter your choice for battle: "; option=getch(); if(option=='a') //Executes an attack else if(option=='b') //Opens magic menu else if(option=='c') //Opens item menu
Suppose the user enters a character, then the program executes an action by the enemy monster. This is where the problem arises, if the user entered multiple keys or if he enters input during the time the monster attacks, the next time it is the user's turn it will execute the first attack automatically because it keeps reading the input.
I want to know how to cut it off, so that it doesn't ruin the program like that.
#include <iostream> #include <ctime> #include <cstdlib> //This program prompts the user to select a number between 2 and 12 //computer throws two dices //player win if selected number matches the sum of the two dices thrown by computer
how to re-prompt a user with a switch statement menu that I prompted them earlier with. There are 4 options and after the user chooses an option the menu is supposed to pop up again unless the user chooses option 4, how do I do this? For reference I'll put up my source code.
Code: int main(){ int init_bal,choice,balance,investment, donation; printf("Welcome!
I've read that we can write a program that displays a menu of commands for the user to choose from.We can write some functions that implement these commands, then store pointers to the functions in the array :
We can get a similar effect with a switch statement but using an array of function pointers gives us more flexibility since the elements of the array can be changed as the programm is running.
I decided to write an example about this in order to understand better the context, I am facing some problems though :
Code: TEST2.c:9: warning: initialization from incompatible pointer type TEST2.c:9: warning: initialization from incompatible pointer type TEST2.c:25: error: too few arguments to function "*(arr + (long unsigned int)((long unsigned int)i * 8ul))"
Question: State the values of each of these int variables after the calculation is performed. Assumed that, when each statement begins executing, all variables have the integer value 5.
I have a datagridview with many columns. I want a context menu to show up when user right-clicks on the fifth column (column index = 4).
private void dgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right) { DataGridView.HitTestInfo hit = dgv.HitTest(e.X, e.Y); if (hit.ColumnIndex == 4) { contextMenuStrip1.Show(Cursor.Position); } } }
The problem is, the hit.ColumnIndex value is either 0, -1 or 1, no matter where I click. So the condition never met and the context menu never shows up. Why the values 0, -1 or 1?
I want the phrase "BUY TICKET" to trigger something, i'm thinking an if statement, and then assign the number after the phrase to another variable. There will also be other commands entered from the input file such as "BUY RAFFLE' "TOTAL REVENUE" on the following lines. Sounds like I need to compare a string(?) to a constant phrase, and if they match have it run the specific code?
Code:
//scan in from file fscanf(ifp, "%c", &input); if (input == 'BUY TICKET'){ //Run this }
I'm tying to create a program that evaluates all possible actions for a certain problem.
So what i'm basically trying to do is to create a sequence of actions, evaluate them to check if it's the best sequence, change the sequence, evaluate again and so on until all possible scenarios are exhausted, leaving the best one in the end.
My approach to this at first was to create a tree of all possible options and then evaluate each branch. Since there are a lot of possible cases i ran out of memory while the program was still creating the tree. I changed this to create just a branch, evaluate it and then modify it.
I was still getting memory problems. I declared a class tNode and declared a vector<tNode*> branch. Then i created all the nodes i needed for that branch with branch.push_back( new tNode() ). When i wanted to modify the branch i simply used branch.pop_back() and again a branch.push_back( new tNode() ). I figured i was getting the problem because although i clear the vector, i don't actually clear the reference in memory. Is this correct? If so, how can i actually delete the memory space and not just the pointer in the vector?
I am trying to write a menu program that will be broken down into a series of calls to function for each of the menu items. Two of the menu items will be simple programs which I wrote.
I want two of the functions to run one of the two programs I am trying to include as items in the menu.
So far I am only familiar with variables, loops, if statements, and I just learned how to write functions.
The problem I am have is that I don't quite understand how to write a function that will run one of the two programs. Also I am having a hard time writing the program in away that would allow the user to select the menu items.
I'm creating a program that is based on a menu system. Basically each menu is its own user-defined function. The problem is at each menu you can input 'b' to go to the previous menu. I just have it call the function of that menu.
However, do the functions need to complete themselves eventually? If I just keep calling functions will I just keep going further and further deeper into ever running functions?
If so how do I avoid this and yet keep the option to go back to a previous menu/function?
#include <stdio.h> #include <math.h> int main(void){ int a,b,c; printf("Choose the values of a, b and c for the equation ax^2 + bx + c"); scanf("%d%d%d ",a,b,c);
I have a certain piece of code that I want to run every 2 minutes. One of my ideas is to get the time and modules that with whatever number represents 2 minutes. Would this work?
you have to use the brackets to enclose just like I used in my code right here.It is a simple while loop that is a countdown.
#include <iostream> using namespace std; int main() { int n =10; while(n>0){ cout << n << ", ";
[code].....
when I take the brackets away from my while loop, my code will still execute but it just prints out the number "10" repeatedly. I was just wondering why it does this and why the compiler does not throw out an error when I leave off the brackets after the "while()".
Am having a problem with the following program not displaying the cout after the while loop, it doesn't ask if I want to enter another employee but just prompts for the employee payrate again.
#include <iostream> using namespace std; int main() // Parameters: None // Returns: Zero // Calls: None
This is what I have so far, it gives me a nice menu to select from and it all works but after I make a selection it closes the program. I tried to return to it but it won't work for me
#include <stdio.h> int main() { int choice; int deposit; int balance = 500; { printf(" CHOOSE FROM THE FOLLOWING
I'm trying to determine (from my Win32 process) if a Metro (or a Modern UI) app is currently displayed on the screen. I found the IAppVisibility::GetAppVisibilityOnMonitor method that can do just that, and I even found a C++ sample, but my issue is that I'm compiling it with the Visual Studio 2008 that does not have the definitions for the IAppVisibility interface, so the following:
I have a code which is a set of rgb frames to be displayed at 25 fps to make it look like a video. While I createWindow invalidate and update it based on a timer, the video plays fine, but the buttons of Play, Pause etc hangs during the time video is playing.?
I have two menu items. When item 1 is disabled, I want item 2 to be disabled as well. In the OnUpdate handler of menu item 1, I have tried to use "t_pMenu = pCmdUI->m_pMenu;", "t_pMenu = pCmdUI->m_pSubMenu;" and "t_pMenu = pCmdUI->m_pParentMenu;" but I always get NULL t_pMenu. How can I achieve this purpose?