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:
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);
Code: #include <iostream> #include <iomanip> using namespace std; int main() { int dayNumber = 1;
[Code] ....
The code, when run, prompts the user to input an integer like this:
Code:
Day 1 Andy :>12 Bill :>7 Charlie :>15
Day 2 Andy :>5 Bill :>25 Charlie :>14 . . . etc.
Ok, so the code prompts the user to enter an integer for each of the 3 persons and then increments the "Day" and so on... When the user enters a negative value (-1, -2, etc.), the loop is broken and the program ends. Alright, everything is hunky-dory so far. 2 things which I can't figure out.
1. when the user doesn't enter anything and just hits return, the cursor drops a line and is still awaiting an input but doesn't show the prompt "Andy :>", for example. How can I get it to repeat the last prompt when the user doesn't enter anything? Like:
Code:
Day 1 Andy :> Andy :> Andy :>12 Bill :>25 Charlie :>15 . . etc.
2. When the user enters a letter or a special character, the program blows up. How can I correct this?
I've tried changing the data type for the variable used for the input, tried using getline, get, etc. With my current beginner knowledge, I'm missing something. So how can I get this to work?
The program should allow only integers to be entered, while allowing a negative number to trigger the loop to break or the program to end, and while re-prompting the last person if the user entered an invalid input.
#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))"
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 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?
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 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?
Write a program that asks the user to type 10 integers of an array. The program will then display either "the array is growing", "the array is decreasing", "the array is constant", or "the array is growing and decreasing."
(Hint: compare consecutive numbers in array and check whether it is increasing or not or constant) I wrote the program very well, but do I have to use "bool" like in c++ ? I haven't known how to code in c++ yet.So, I don't want to use "bool" in C.
I also wrote code about it but it looks like backslash and one,two,three,four,five how can I do it like this ?
Enter as many numbers as you want as long as the user doesn't type 'e' to exit.
Problem: When I enter a number, it works fine, but if I enter e then it'll go in an infinite loop since the letter is being stored in an int variable. How can I (when I press 'e') make it convert to a char to make it end the program?
#include <iostream> using namespace std; int main() { int num; cout << "Enter a number or press e to exit:";
[Code] ....
Our class has just started c++ and we have not learned arrays and classes yet, so I'm guessing there is a way to do this without it? Or no?
I want to create a new data type called an inf_t. It's basically infinity (which for C++ is 1.7e+308). The only reason I want this is because I want to overload the cout << operation to print out INF/inf. Should I do this in a struct?
If I am asked to declare a data type for Date which should be in the format DD/MM/YY, which data type should i use for it? Is there any data type known as Date in C?
I am getting a compilation error from the code below. It is when i am naming a variable with my user defined type.
#include<iostream> #include<cstring> #include<cstdlib> using namespace std; class person {
[Code] .....
C:Dev-CppTRIAL.PASS.!!!.cpp In function `int main()': 66 C:Dev-CppTRIAL.PASS.!!!.cpp expected primary-expression before "p" 66 C:Dev-CppTRIAL.PASS.!!!.cpp expected `;' before "p" 74 C:Dev-CppTRIAL.PASS.!!!.cpp `p' undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.) 83 C:Dev-CppTRIAL.PASS.!!!.cpp `X' undeclared (first use this function)
I have two char variables, m_GPSOffset[13] and m_FileName[100]. When m_GPSOffset has a value assigned to it, say for instance +11:25:30. The first entry of the value, in this case +, is always stored in m_FileName. I am clueless on why this is occurring.
I know that an int is usually 4 bytes, ranging from -2^31 to 2^31-1 for a signed int and 0 to 2^32-1 for an unsigned int. My question is simply, bit-wise (I know they are labelled in the code), how does it determine whether to show -2^31 or 2^32-1 if it was 11111111 11111111 11111111 11111111 in bits? Is there a 5th byte to tell the compiler what data type to treat the input as?
Working on a Project Euler problem and the question asks for the largest prime number that is a factor of 600851475143. As you can see, this is significantly larger than the maximum of a long data type, which maxes out at 2147483647.
I'm running on Windows 32, so int64 is not a valid option for me. It seems like I'll likely have to use a different language to solve this problem.
I'm currently trying to solve a programming assignment and i got the logic of it, however i find it hard to implement.
What i need to do basically is fill an array with objects. Each object is a class that contains only one type of data. This means i can place int, double and string for example in one simple array.
However i can't figure out how to read data and then decide what it is. Even if i use templates once i call the function i have to give it a type, so getType<int> for example will not work with double or string.
I know about typeID and how to use it, i just can't figure out where to use it.
I know how to store numeric data using keywords int, long, float, and so on. I'm making my own program called "Who is your soul-mate".The only question I want to ask is what's the keyword for storing alphabet data? As you can see below on my source file. I want to replace "int" keyword with another keyword that can store alphabet data. It's all in standard C.
#include <stdio.h> int soulm01, soulm02, soulm03; int year_of_birth; int main(void)