0

Hi , i write this code and i get error whene i run it !!! why i cant send a variable to array parameter ???

#include <iostream>
#include <conio.h>
using namespace std;
void main(){
	int a,b,c;
	cin>>a;
	cin>>b;
	c = a+b;
	int array[c];
	cout<<c;
	_getch();
}

i get this errors :

Error 1 error C2057: expected constant expression c:\users\mahdi\documents\visual studio 2008\projects\add number\add number\add.cpp 9 Add number

Error 2 error C2466: cannot allocate an array of constant size 0 c:\users\mahdi\documents\visual studio 2008\projects\add number\add number\add.cpp 9 Add number

Error 3 error C2133: 'array' : unknown size c:\users\mahdi\documents\visual studio 2008\projects\add number\add number\add.cpp 9 Add number

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
0

Array size must be a constant determinable at compile time (yours requires the user to enter numbers at run time). You can dynamically allocate your array with int * array = new int[c]; . Some compilers have extensions to their C compilers to do what you did but the results are not portable and non-standard.

Also, look at line 10. It is not going to give you the intended result. Loop through the elements to display them

Edited by jonsca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.