C++ :: How To Access Certain Elements And Store Multiple Inputs
Jan 20, 2014
am trying to create a program that asks the user personal questions.
std::vector<std::string> name, age, favsinger;
std::cout << "Hello, what is your Name Age Favorite_Singer? ";
std::cin << name; //i want to store the user's info along with the sibling's
[Code]....
View 4 Replies
ADVERTISEMENT
Sep 27, 2014
I used to use map to access elements. map has a good feature that it sort the element automatically. Now I need a map which can access element through multiple key values. So I choosed boost::multi_index_container. I defined a container as follows.
struct elem {
int a,b;
elem(int aa,int bb):a(aa),b(bb) {}
};
typedef multi_index_container <
[Code] ....
What I am wondering is whether boost::multi_index_container can sort elements automatically. Specifically, are all elements extracted through iterator from begin to end shown below having b values between 2 and 100?
test t;
test::iterator begin = t.lower_bound(make_tuple(1,2));
test::iterator end = t.upper_bound(make_tuple(1,100));
View 3 Replies
View Related
Feb 14, 2013
For a program I am required to use a cin that accepts 4 variables. The first describes a function such as add(), remove(), print(), or quit(). The problem is that to use add() I need to input all 4 variables but for remove(), only 2 variable input is needed.
I want the input to be "add 9 James Bond"
or be "remove 341"
Here is my current code.
int command(string command, int Id, string first, string last){
while (command != "quit"){
cout << "customers> ";
cin >> command >> Id >> first >> last;
if (command == "add")
[Code] .....
View 1 Replies
View Related
Nov 26, 2013
It's not something trivial but I would like to know the best way to process multiple outputs, for example:
Input
First line of input will contain a number T = number of test cases. Following lines will contain a string each.
Output
For each string, print on a single line, "UNIQUE" - if the characters are all unique, else print "NOT UNIQUE"
Sample Input
3
DELHI
london
#include<iostream>
Sample Output
UNIQUE
NOT UNIQUE
NOT UNIQUE
So how can I accomplish outputs like that? My code so far is:
Code:
int main(int argc, char *argv[]) {
int inputs, count=0;
char str[100];
char *ptr;
scanf("%d",&inputs);
[Code] ....
But the above will obviously print the output after each input, so I want to know how can I achieve the result given in the problem. Also another thing I want to know is, I am using an array of 100 char, which it can hold a string up to 100 characters, but what do I have to do if I want to handle string with no limit? Just declaring char *str is no good, so what to do?
View 5 Replies
View Related
Jan 23, 2014
I am beginner at C and I was working on a program where I have to read in a line such as Digit, String, Float. The string can have any amount of spaces between it. and each input is separated by a space character.
The input is of the format:
10000000000 hello my n ame is 30.2
So I used fgets(x,100,stdin) to read the line in.
So I need to read that line into an int, array, and float.. so I was thinking of using this:
sscanf(x, "%d %[^/n] %f", &number, &username, &numberfloat);
Now, obviously I can't use the /n to read in the username with spaces because I need to read the 30.2 into a float variable.
View 5 Replies
View Related
May 28, 2014
I have a C++ code reading large data from an input txt file, doing some calculation on the data, and writing the result of calculation in another txt file.
I have about 300 input files, and the calculation time for each input file is pretty long (~4 days on a single CPU), so I would like to run the same code on multiple CPUs for different inputs.
Which is the most appropriate strategy in this case, multithreading, mpi or something else?
View 4 Replies
View Related
Feb 26, 2015
The tools Hal's sells are: Hammers: $10.99 Wrenches: $10.99 Levels: $19.99 Tape Measures: $4.99 Screwdrivers: $8.99
In order to make things easier in the long run you have decided to make a c++ program that takes in all the orders for each register and tallies them at the end of the day.
Write a c++ program that does the following:
Create a Register class that can store all of the receipts for the day and the totals for that register.
This class could have the following member functions:
Contructor(int) - Creates a number of receipts based on an integer that is passed into the constructor (this is the largest number of orders that the register can take). You may also be required to create additional variables to make the program work but that's up to you to determine.
void getorder() - Asks the user how many of each item they want an stores that in the first available receipt.
void returnreceipts() - This function returns the details of all the receipts and the total for the day.
Register operator+(const Register &right)- This is an overloaded addition operator. When two objects are added together an empty temporary register is created (with 0 receipts) and only the totals of both objects are added to it. The temporary register is returned by the function.
Register operator=(const Register &right)- This overloaded assignment operator copies only the totals from the Object on the right.
In the Main function I would like:
Create 3 Register Objects. The first two registers are the ones in the store and should be initialized to take 10 orders. The third is used at closing time and can be initialized with 0 receipts as it will only take in the totals from the other registers.
Run the getorder function for registers 1 and 2 twice.
Run returnreceipts for register 1 and register2.
Reg3=Reg1+Reg2;
Run returnreceipts for register 3.
[Code]...
this is what I have so far I'm trying to get the input part working first, so the user would be asked each item individual how many they want? this will happen for 2 users, then the next register will do the same?
View 2 Replies
View Related
Mar 1, 2012
Say I have 5 vectors of unsigned char each of size 5. I want to take the max of each index and store it in a new vector. What is the most optimal way to accomplish this?
My slow code:
Code:
vector<unsigned char> vec1;
vector<unsigned char> vec2;
vector<unsigned char> vec3;
vector<unsigned char> vec4;
[Code]....
View 3 Replies
View Related
Oct 18, 2013
I've been looking through containers in the reference section and I can't figure out how to access (i.e. READ) elements in a list...
View 2 Replies
View Related
May 5, 2013
I am currently trying to implement a 2d vector to store x and y of type int.
I have successfully passed it to the function, however i am unable to store the values in it. It always returns with a segmentation fault and my program terminates from there. May i know how do i store them properly and call them out?
Below is my code snippet
int reconstructSecret(int x, int y, vector< vector<int> > &inVec ,int constructSecret) {
int getX,getY,formula,accum,count,getSecret,startPosition,nextPosition,numerator,denominator;
getX=x;
getY=y;
int result;
[Code] .....
The main method
vector< vector<int> > inVec;
for(int i=0;i<constructSecret;i++) {
cout<<"please key in the "<<i<< "share of x value:";
cin>>x;
[Code] ...
View 9 Replies
View Related
Jun 15, 2013
How I would store all the data. Also how would I access it after I store it. Side Note we are not allowed to use structures or objects.
For the purposes of this gradebook you should first provide a menu with the following options
-Add a new course Add a new student to a course Add grades for a student in a course Print a list of all grades for a student in a course Print a list of all students in a course Compute the average for a student in a course Compute the average for a course Store Gradebook (to a disk file) Load Gradebook (from a disk file)
-Each of these menu items should correspond to a function you will write.
-For the basic program each student will be represented by an ID number And each course by a course number
-Grades will be whole numbers only (no fractional part)
As indicated in the menu you will need to store and load using a disk file so that the data is retained.
Here are so limiting values in defining your data structures: Maximum Number of students (total) 100 Max number of courses 25 Max number of courses per student 4 Max number of grades per student per course 10
View 6 Replies
View Related
Dec 12, 2014
I have:
An Access database in which the picture right now is saved by its directory, together with other field like title, publisher, developer,..
Visual Studio form in which a load button gets that directory as text
Details button that reads the ID of a selected item (I have a list of games) and displays the corresponding picture by reading the directory
The problem right now is that once someone else opens the program on his/her computer, the directory changes, making the whole thing useless.
Is there a way to get around this problem? The books I've been reading don't really adress this.
Directory is a string in class 'Game'; it's short text in the database
private void buttonFoto_Click(object sender, EventArgs e) {
LoadNewFile();
}
private void LoadNewFile() {
OpenFileDialog ofd = new OpenFileDialog();
[code].....
View 9 Replies
View Related
Oct 3, 2013
I was writing generic class Array (based on counting access to elements)and i got compiling error I cannot even understand (in vs2012).
Code: [URL] ....
Error: [URL] ....
View 8 Replies
View Related
Feb 19, 2014
Write code to do the following:
1) declare a variable ptr as a pointer to int and initialize it to NULL
2) dynamically allocate memory for an array of 100 elements
3) read 100 elements from the standard input device and store them in the array.
This is what I have so far, I'd like to know if its ok or if something is wrong.
int *ptr = NULL;
ptr = new int[100];
cin >> dataPtr [arr];
View 2 Replies
View Related
Mar 31, 2013
Say I have a class with a few member functions, and only two data members: an int* Table; and an int Size;, to store the number of elements in Table.
I'm using a copy constructor that takes in two parameters: int* table, int size. In this case, is the address that table points to the same address as the object that table is part of? And furthermore, is it possible to say table.Size? I want to compare the passed array's size to the passed size.
View 2 Replies
View Related
Jan 10, 2013
I have to complete a project that i want to read float numbers from keyboard and store them into an array of 10 elements.! Every time that a number stored into array i want to compare with previous one if they have +-10 difference .. I want to keep only 10 elements into my array so every time that i give value a[0] replace a[1], a[1] replace a[2],a[2] replace a[3]. . . .and a[10] deleted.. So when all elements of the array are similar with +-10 values print out the array.!
View 1 Replies
View Related
May 1, 2015
I am working on a text based RPG. As with most RPGs the character has attributes that grant modifiers. Lets take strength for instance. Suppose the character can have a strength score that ranges from 1 to 10. Based on strength the modifiers could be like the following:
Strength = 1 grants +1 to hit and +1 damage
Strength = 2 grants +1 to hit and +2 damage
Strength = 3 grants +2 to hit and +3 damage
I want to set these values at design time and be able to retrieve the modifiers based on the strength value from multiple places in my program.
What is the best method of designing this. I looked around online and saw references to Lists with Tuples and Dictionaries with Tuples but these did not seem to be a very efficient way of handling the scenario above.
View 4 Replies
View Related
Sep 7, 2013
I was trying out programs based on extern and as i understand, this is useful when accessing variables across multiple files having only one definition. But i tried a simple program as below without "extern" and thing seem to work when i expected it would fail during linking process
Code:
file5.c
1 #include <stdio.h>
2 #include "var.h"
3
4 int main() {
5
6 printf("
File5.c a = %d", a);
[Code] .....
As i have included "var.h" in all header files without extern, "int a" would be included in both the .c file and during linking, compiler should have thrown a warning or error message but it compiles file without any issue. Shouldn't var.h have the following "extern int a"?
View 8 Replies
View Related
Oct 13, 2013
int statarray[] = {61, 66, 47, 50, 372,
62, 66, 47, 50, 372, 50, 54, 64, 45, 331, 49, 52, 58 69, 356,
58, 80, 50, 48, 389, 76, 64, 60, 59, 401, 76, 59, 57, 54, 422,
53, 45, 45, 52, 253};
I'm getting a bunch of different errors with this, how do we do this?
View 2 Replies
View Related
Aug 14, 2012
I'm having a bit of trouble with something. What I'm trying to do is create a new Access table by combining other already existing Tables.
For instance, let's consider Table1 (with columns A, B and C), Table2 (with columns A, D and E) and Table3 (with columns A, F and G), where Table1.A = Table2.A = Table3.A.
From these, I want to create a new table TableResult, with columns A, B, C, D, E, F and G, with all the data contained in them. There's one catch: I want the code to get the names of the other columns, apart from column A.
I've thought about using Union, but I need to actually create a new table by combining the previous tables.
View 11 Replies
View Related
Jan 11, 2015
I have array of 100 elements and I'm trying to write elements which index is smaller than the index of the maximum element of the array. My problem is that I can't determine how big this array should be . I found which is my biggest index.
Code:
#include <visual_2013.h>
#include <stdio.h>
#include <stdlib.h>
#define DIM 10
void enterArray(int x[]);
void array2(int x[]);
int maxAndIndex(int x[], int n, int *index);
int main()
[Code]...
View 3 Replies
View Related
Feb 4, 2015
If I have a class object and multiple vectors, how do I make sure I store the same instance of that object in both vectors? For instance:
Object object;
std::vector<Object> vectorOne;
std::vector<Object> vectorTwo;
vectorOne.push_back(object);
vectorTwo.push_back(object);
Are the objects in both vectors the same instance of the object? Like if I called vectorOne[0].setValue(somethingDifferent); would the value be changed for the object in both vectorOne and vectorTwo? If not, how do I make sure that I only have one instance of the object I'm trying to store in multiple vectors?
View 15 Replies
View Related
Apr 18, 2014
Is it possible to store multiple project files in one projects solution explorer?
View 2 Replies
View Related
Oct 9, 2014
My function "MatrixMul" returns an int array with multiple values Let's say, res[0] and res[1]
When I'm calling the array in a for loop for multiple times, and when I'm storing the results in another array, in each iteration the results are over-written with the new results.
If the first call returns [0,1] the array will store [0,1] at index [0] and [1], which is fine, but when I'm calling the function again, the new results are stored at the same indexes [0] and [1] How can I avoid that?
The code is:
class Hill_Cipher
{
string AtoZ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public string Hill_Cipher_Enc(string input, int[,] key)
[Code].....
For example, my outPut contains the following: "TH","IS","AT" when I'm calling the function with the first element of array "TH", it converts "T" to its equivalent number and apply some calculations and same with "H". Let's say the final answer is 20 for "T" and 30 for "H". The problem is that every time, encChars will store the values at index 0 and 1: encChars[0]=20 encChars[1]=30 When I call the function again it will store the new values at 0 and 1.... That's because I'm not changing the index value for encChars on each call, so how do I do that?
View 1 Replies
View Related
Nov 6, 2013
we conform to the ISO C standard and this snippet of code : Code: vector<tree*> *leaves = new vector<tree*>[num_threads]; where num_threads is specified from command line arguments so not dynamically allocating it violates the standard.
Let's also assume num_threads is greater than one.
What I want to do is scan each vector in leaves for duplicates. If any two vectors in the set have matching addresses, they both immediately go onto the "unsafe" pile and will no longer be subject for testing.
If a vector clears one vector, we test it against the others in the set.
So if we have 3 vectors, A, B and C we test A against B then A against C. For efficiency, we then then just test B against C.
Like I said, I want a "safe" and "unsafe" pile. Every vector in "safe" is fully unique while every vector in "unsafe" is not unique.
I thought about just using a for-loop to loop through leaves and then iterate through each element but I'm not sure if that'll work just right out of the box.
View 14 Replies
View Related
Apr 19, 2013
I have a txt file that looks like this:
Student ID:97707; Grades: 87.73, 90.41, 91.74, 95.04, 99.13; Name:Davis, Artur
Student ID:23628; Grades: 58.09, 65.18, 68.62, 68.62, 98.05; Name:Davis, Susan
Student ID:49024; Grades: 18.37, 66.06, 68.07, 80.91, 96.47; Name:DeGette, Diana
-I need to read the id, grades and names;
-The id to a separate array
-The grades to a 2-d array
-And the names to a c style string.
Where do I start, im having trouble read the dummy to start
int main() {
char filename[15] = "ex.txt";
string names[MAX_NAMES];
double grades[MAX_ROWS][MAX_COLS];
int id[MAX_IDS];
int index = 0;
ifstream fin;
[Code] .....
View 3 Replies
View Related