C++ :: Build Array Of A Size After Receiving User Input
Sep 25, 2013
I'm trying to build an array of a certain size, after receiving user input. The method I'm trying is
int var = 3;
int* ar = new int[var];
When doing tests I did this (and it worked) and red flags popped off in my head.
int var = 3;
int* ar = new int[var];
ar[0] = 0;
ar[1] = 1;
ar[2] = 2;
ar[3] = 3;
ar[4] = 4;
cout << ar[0] << ar[1] << ar[2] << ar[3] << ar[4] << endl;
This method is considered safe, and possibly tell me why I'm not getting stack errors when trying to overload the array?
View 9 Replies
ADVERTISEMENT
Jun 24, 2013
My program works primarily by receiving user input however; using 'char' and 'fgets' i have to stipulate how many characters i want assigned, and this isn't practical for what I am after. Example below.
Code:
char example[50];
printf("What colour is the sky?
");
fgets(example, 50, stdin); Is it possible that the assigned number (in this case '50') is determined strictly by the user input?
for example, user input is Blue then 50 is then 5?
View 9 Replies
View Related
Nov 1, 2014
I am making program that allows the user to determine how big the array size will be and then asks the user to make up numbers to fill the array. Every time run the program on Dev C++ it says "program has stopped working"
Heres My Code:
//Assignment 19 Program 2
#include <iostream>
using namespace std;
int main()
[Code].....
View 3 Replies
View Related
Apr 27, 2014
So I'm trying to make a program, Which has nothing to do with the topic. But I ran into a problem.
I need to get a char array size of 6 doing:
char myChar[6];
but the size (6) is undefined until user input.
So I need to do char myChar[var]; (Var being 6 for now).
When I do:
char myChar[6];
It works!!!
But when I do:
int val = 6;
char myChar[val];
It doesn't work.
View 3 Replies
View Related
Mar 13, 2014
How can i write a program that allows a user to enter a size of pizza and then print the price for that size.
Example: if a user enters size ''s'' then it should display the price stored under ''s''
View 3 Replies
View Related
Oct 12, 2014
I am not sure why I am receiving the error message:
Error C2466: cannot allocate an array of constant size 0
When I run the code:
Code:
int s;
cout<<"Enter the size:
";
cin>>s;
int array[s];
C++ masters,
View 7 Replies
View Related
Jul 23, 2014
The instructions call for the user to define the size of the array and all I have ever done is use a predefined size for the array and then let the user fill it. Here is what I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Display (void);
void random (int *, int);
void Ascending (int *, int);
void Descending (int *, int);
[code]....
View 7 Replies
View Related
Dec 1, 2014
is it possible to use this code ?
#include <iostream>
int main(int argc, const char * argv[]) {
int number = 0;
std::cout << "napis cifro za array
";
std::cin >> number;
[Code]...
It does work on xcode but doesn't in microsoft visual studio 2013. Where is the problem?
View 3 Replies
View Related
Apr 5, 2013
How can I stomp the arr size of 50 with the value the user inputs? The function doesn't sum up the even integers from 0 to the value the user inputs. Instead it sums up exactly that many even integers.
Is there a way I can stomp the size of the array with what the user inputs so that the sum calculation never goes past the value the user inputs?
Code:
void sumIntegers () {
int arr[50];
int i = 0;
int num = 0;
int sum = 0;
printf("
I want to sum up even integers <= 50.
[Code] .....
View 4 Replies
View Related
Apr 22, 2013
I'm doing an exercise which involves for the user to enter the size of the dynamic array and then enter the numbers, but then it needs to create another dynamic array with the same numbers expect if the number repeats it only has one of it. I've done the first part of the exercise but I'm having trouble with creating the new array.
View 10 Replies
View Related
Oct 20, 2013
I am trying to use an array of a size that will be determined by the user, therefore I must use a vector, right?
In class I was told that this is how I call a vector:
vector <int> x;
Is the vector called vector? Is it called x?
Can I do this?
for(int i=0;i<=10;i++)
{
cout<<x[i];
}
Some basic ways of implementing a vector? How it works or how I can do anything with it.
View 2 Replies
View Related
Dec 2, 2013
Dynamic memory allocation in array in c programming. I am trying to make the user to choose the size of array they want to engage in the game.
However, i have remove the global variable which contribute the error to my code previously. Now I assigned all the arr individually but not using the global variable. However, i still not get the desired board i want. i still keep getting 9x9 array board.
And i also need limit the board size only from 4 to 9. And how do i do that.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <malloc.h>
#include <ctype.h>
#include <stdbool.h>
[Code] .....
View 3 Replies
View Related
Nov 8, 2013
I'm now working on a class to handle matrices and matrix operations. I dynamically allocate a two dimensional array with its size as an input. My problem is that i need to resize the matrix structure (to fit to the size of a product of two matrices or to adjust the size after deleting a row or column for example). I make the resizing by deleting the allocated 2d array after saving its first element address to another pointer to pointer, after saving the useful data in an other array, then i use new[] operator to create a new 2d array using the same address of the old deleted array.
void My_matrix ::delete_row(int a) {
int i,j;
int buffer_row_index = -1; //note that our work is zero based, as we acess the private member of an object of the same class we are working on and we access to the array as it is (zero based) so if this variable is -1, it will be 0 when it enters for the first time and saves the data in the right position.
My_matrix buffer(number_of_rows-1,number_of_columns);
[Code] ....
I do this in a way that i think is unsafe. This code is sometimes unstable and gives an unhandled exception whose reason is unknown. I need to know whether this way is good enough or if there any other better ways.
View 5 Replies
View Related
Aug 29, 2014
I'm a little lost with this program. The idea is to dynamically allocate an array and increase its size every time a new integer is inputted by the user. I believe it is a memory leak but as we have just started learning this I'm not sure how to recognise it. Sometimes I can input as many integers as I want other times 2 or 3 before it crashes. When I can input enough values i exit the loop and send it to the sort function and mean calculator function, all works fine there except the last number inputted becomes this huge value not hexadecimal though... As such I'm at a loss as what to look at next, so here you go:
#include <iostream>
using namespace std;
void SelectSort(int [], int);
float MeanCalc(int [], int);
float MedianCalc(int* [], int);
[Code] .....
View 14 Replies
View Related
Apr 25, 2014
I have written a program and i had to pass an array into a function and now i have a variable for the subscript of the array and i was the user to input data, specificlly a string, more specifically first and last name without having to create two arrays, i have to do this with other things in the program as well, here is part of the program:
int addFunc(const int totNum, string city[],string state[],string street[],string name[],
int addNum[],int zip[],int telNum[],double bal[],int dateLp[], int addCount, int accNum, int usNum)//function will add a new account {
int countAf = 1;
if (usNum < 20)
[Code] ....
it should be able to take firstname space lastname
View 5 Replies
View Related
Mar 7, 2014
My parameters are that the users input has to be from 1 to 9 and the same number can't be entered twice. How do i modify this code to make sure that the user did those things or an error message should appear that their input is invalid.
#include <iostream>
using namespace std;
int main() {
int square[3][3];
[Code] ......
View 6 Replies
View Related
Mar 28, 2013
Say you the user inputs x number of names and then is to put in x amount of values for each name. How would you display these values in a 2d array and be able to add the values for each row which will represent each name?
View 5 Replies
View Related
Mar 26, 2014
Were supposed to be able to sort an array which is user input, but the first number they input is supposed to be the length of the array, and everything after that is the actual array.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int size;
int list[80];
bool isSorted (const int list[], int size);
[code]....
I'm stuck on how I'm supposed to use the first digit of input for the array size.
View 6 Replies
View Related
Dec 1, 2013
how to insert a variable for the size of array and prompt user to insert the elements for the array?
View 13 Replies
View Related
Apr 6, 2014
I need to build an array in a function and return that array. Well, two arrays actually, which means I can't return it in the return statement.
Long term, this function/method is supposed to return two populated buffers for further processing. The buffers/arrays are loaded from a file. So, the size of the array is completely unknown until after the file is read. So, you can't have a fixed size array until the function is almost ready to return.
I'm hardcoding the arrays here for testing purposes, but eventually they will be loaded in from a file. That's the whole purpose of this.
Anyway, I need to pass the arrays that are created in this function/method to the caller. But I can't figure out how to do it. The code "almost" works, except that it's overwriting my array when it returns.
Here's the method:
bool GameModelClass::LoadBuffersFromPUNCFile(std::wstring PUNCFileName, VERTEX* &VertexListPointer, unsigned int* &IndexListPointer) {
VERTEX GroundVertexList[4] = {
[Code]....
VertexListPointer returns the correct data structure (actually its an array of structures). The problem is that the first time I use the other variables it over rights the array. The pointer still points to the same spot, but VertexBufferDescription is next to the VertexListPointer and so completely overwrites it.
What I "think" I want is to allocate the array in a totally different area of memory. VertexListPointer should be a pointer, not a contiguous block of memory. It should point to the contiguous block of memory elsewhere.
This is where my weak understanding of pointers comes back to bite me in the posterior end.
In the end, I just want to pass back two arrays/buffers to be used by the calling code.
View 14 Replies
View Related
Mar 19, 2014
I am compiling using Microsoft Visual C++ and I am trying to create a program that ask's the user for 10 numbers, and these numbers will have to be loaded into a one dimensional array of 10 elements. I read up online as well as my book and looked at sample programs, then created mine which was similar to the sample programs I looked at, but the sample ones compile , while I get C2059 and C2061 Syntax Errors. I am new to programming so I understand it could be one small thing but I after looking over it I am completely clueless.
Heres my program:
Code:
#include
<iostream>
int
main()
[Code] .....
My program is also attached
View 5 Replies
View Related
Apr 5, 2013
If the user puts in a 6. I need the array to display the even number from 0 - 6. Right now what it does is it displays the first six even numbers. Okay as I'm writing this I'm starting to see where my problem might be..
Code:
void sumIntegers () {
int arr[50];
int i = 0;
int num = 0;
int sum = 0;
[Code] .....
View 1 Replies
View Related
Oct 11, 2013
I am trying to create a global array with user-defined dimensions.the code is:
Code:
int matr_size()
{
int x = 0;
printf("Please enter the number of nodes: ");
scanf( "%d", &x);
printf("There are %d nodes in this simulation.", x);
getchar();
return x;
}
[code]....
I read that an array cannot be defined by a variable in C so I assume that is the issue, but I'm not sure how else to do it. Previously the size was defined by #define NODES and it worked fine but I need this user input.
View 5 Replies
View Related
Nov 8, 2014
I'm trying to code the program that will store item data.And I'm having problems to receive user-input string to array.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iomanip>
using namespace std;
[code].....
View 6 Replies
View Related
Oct 25, 2013
I have been reading up on arrays and string array. I created a string string text[0] and it is defined by user input. I am trying to sort the input. I want Michael to read Macehil.
When I wasn't using an array and just a string I did this:
return sort(text.begin(), text.end()); a
And it worked fine. Do I need to change my string into a char? If so, would I static cast that?
View 9 Replies
View Related
Mar 20, 2014
how to do this? ask the user to input entries of 10x10 array. sort each column into increasing order. print out the array with sorted columns?
View 5 Replies
View Related