• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

C++ 1D Array Question

Joined
Feb 11, 2008
Messages
612 (0.10/day)
Location
DFW, Texas, USA
System Name Built By Me
Processor AMD 9800X3D
Motherboard Gigabyte X870 Aorus Elite WiFi7
Cooling Water Cooling - CPU Only - Heatkiller IV Pro - TG PhaseSheet PTM
Memory 32GB (2 x 16) - GSkill FlareX5 DDR5 6000 Mhz
Video Card(s) RTX 4080 - ASUS ROG Strix 16GB OC - P Mode
Storage 2TB Inland Performance Plus NVMe
Display(s) Alienware AW2723DF @ 280 Hz @ 1440P
Case Fractal Design Define S2
Audio Device(s) Corsair Virtuoso Pro Headset
Power Supply 1000W MSI MPG A1000G PCIE5
Mouse Razer Viper V3 Pro @ 2k Hz
Keyboard Asus ROG Strix Scope II 96 Wireless - ROG NX Snow Switches
Software Windows 11 Pro
Why won't this work? My compiler tells me the only errors are located where the bold is.


#include <iostream>
using namespace std;

int main()
{
int t, s, guess;
cout << "Enter in the amount of storage spaces you need. ";
cin >> s;
int nums;

for (t = 0; t < s; t++)
{
cin >> guess;
nums[t] = guess;
"\n";
}
for (t = 0; t < s; t++) cout << "\tYou entered " << nums[t];
return 0;
}
 
Off the top of my head, I don't see anything wrong. What is the error message? Is it a run time error or compile error?
 
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'nums' : unknown size
 
Ah, "expected constant expression." You can't define the array size at run time, it has to be defined at compile time. That is, it needs to be constant.

For instance...

Code:
#include <iostream>
using namespace std;

int main()
{
int t, s, guess;
cout << "Enter in the amount of storage spaces you need. ";
cin >> s;
int nums[100];
for (t = 0; t < s; t++)
{
cin >> guess;
nums[t] = guess;
"\n";
}
for (t = 0; t < s; t++) cout << "\tYou entered " << nums[t];
return 0; 
}
s tells you how many are actually there. In the example above, I fixed the size to 100 which is the maximum number it will fit.


You could also change it to a vector. There's an example here:
http://www.codeguru.com/forum/archive/index.php/t-339603.html
 
As FordGT90Concept said, you can define a maximum value for the array size, if you are sure that that value won't be reached, but you may want to try replacing this:
Code:
int nums[s];
with:
Code:
int* nums = malloc(s * sizeof(int));

Explanation: in C (and C++) all arrays are pointers to a continuous area in the memory were values are stored, so instead of creating this memory area at compile time you should create it at runtime (since it has a variable size). To do this you need to create a pointer (signaled by *) and on this example a pointer to an integer (so "int *"). Then you need to Allocate Memory (malloc), to assing to the pointer, with the size of the data you want, wich you think that should be s, but the array size is measured in bytes and not in items, so we need s times the size of one integer (measured at compile time by "sizeof(int)"; on an a "regular" x86 machine this number will be 4 (each integer is made-up/ocuppies 4 bytes on RAM)). To finish and since pointers are arrays and vice-versa, we will use "nums" to access our array without needing to change anything.

This might sound a bit technical but it's the right approach, you should read a bit about arrays and it's relation to pointers (if this is an class example don't worry you will). I tried to explain it the best way I can without talking about many other stuff, hope you understand.
 
I finally got it:). I used this line of code in bold.

#include <iostream>
using namespace std;

int main()
{
int t, s, guess;
cout << "Enter in the amount of storage spaces you need. ";
cin >> s;
int *nums = new int;
for (t = 0; t < s; t++)
{
cin >> guess;
nums[t] = guess;
"\n";
}
for (t = 0; t < s; t++) cout << "\nYou entered " << nums[t];
cin >> s;
return 0;
}
 
Back
Top