Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 14-11-2008, 06:40 PM   #1 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default Very Basic C++ question


I am new to programming in C++, just now I tried to create an array with 'n' index'es where n is an input no. This is what i coded:
Code:
#include<iostream>
using namespace std;

int main()
{
    int n=10,m;
    cin >> n >> m;
    int stor[n];
    return(0);

}
But, VC++ 6.0, returns, the following errors:
Quote:
Compiling...
Cpp2.cpp
C:\Documents and Settings\Darshit Shah\C++\Euler\IOI\Cpp2.cpp(8) : error C2057: expected constant expression
C:\Documents and Settings\Darshit Shah\C++\Euler\IOI\Cpp2.cpp(8) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\Darshit Shah\C++\Euler\IOI\Cpp2.cpp(8) : error C2133: 'stor' : unknown size
Error executing cl.exe.
Please help... how do i create such an array??
This syntax used to work in Java
__________________
My new Tech Blog : http://technewspaper.blogspot.com/
redhat is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 14-11-2008, 07:53 PM   #2 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: Very Basic C++ question

The size of an array must be known at compile-time. ie, if you had done :

Code:
int stor[10];
it would have worked, but since the size of an array is a variable, it won't work. That's why it says "expected constant expression".

To do what you intend, try :

Code:
int* stor = new int[n];
Java != C++.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 14-11-2008, 08:01 PM   #3 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default Re: Very Basic C++ question

Thanks a lot... Im new to C++, and all this hasnt been taught to us yet in college, just tried doing this with my existing programming knowledge, many syntax's worked though wasnt aware of this rule in C++

Code:
#include<iostream>
using namespace std;

int main()
{
    int n=10,m;
    cin >> n >> m;
    int* stor = new int[n];
    int* sums = new int[m];
    for(int i=0; i<n; i++)
    {
        int x;
        cin >> x;
        stor[i] = x;
    }
    for(int k=0; k<m; k++)
    {
        int s,e;
        cin >> s>> e;
        int sum=0;
        for(int j=(s-1); j<e; j++)
        {
            sum+=stor[j];
        }
        sums[m] = sum;
    }
    for(int l=0; l<m; l++)
    {
        cout << sums[l];
    }
    return(0);

}
Can someone please correct this code and tell me why am I not getting a output, after some checks I realised that the array sums[] does contain the perfect values that it should, but when trying to print it in the last loop, it is giving garbage values
__________________
My new Tech Blog : http://technewspaper.blogspot.com/

Last edited by redhat; 14-11-2008 at 08:33 PM. Reason: Automerged Doublepost
redhat is offline  
Old 14-11-2008, 09:03 PM   #4 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: Very Basic C++ question

What do you mean, garbage values? If it looks like a really big, but single number, then it would be because you're not inserting spaces between the numbers. Try :

Code:
cout << sums[l] << endl;
If that still doesn't work, simplify the program until you get an answer, and then add to it.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 14-11-2008, 09:28 PM   #5 (permalink)
The Smaller Bang
 
MetalheadGautham's Avatar
 
Join Date: Sep 2007
Location: Gautham City
Posts: 7,492
Default Re: Very Basic C++ question

use the new operator for the stor.

Try int *stor new int[n];
I am sure it will work.
You need to use pointers for dynamic initialisation of an array.
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
MetalheadGautham is online now  
Old 15-11-2008, 06:47 PM   #6 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default Re: Very Basic C++ question

@MetalheadGautham : I HAVE used pointers for the initialisation.. see the code.. yea.. initially i wasnt aware. but now I know that I must use pointers for dynamic initialisation.

@Sykora : No, it isnt one big number.. I would've realised that folly very easily!! It shows the same particular number again and again... and that value is in no way related to the program
__________________
My new Tech Blog : http://technewspaper.blogspot.com/
redhat is offline  
Old 15-11-2008, 07:22 PM   #7 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Very Basic C++ question

stor[] is bounded by n. Your summing loop doesn't take care of that and thus out of bound garbage values may get added!

That, and your sums[m] = sum; line must read sums[k] since that is the counter and m is the size, which is just outside the boundary.
__________________
Harsh J
www.harshj.com

Last edited by QwertyManiac; 15-11-2008 at 08:25 PM.
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
question based on basic diffrential calculus xbonez Chit-Chat 2 05-07-2007 02:43 PM
C++/C to Basic Shikhar Programming 2 24-03-2006 01:48 PM
Help me buy a Basic UPS (what VA)? dragonball QnA (read only) 2 17-07-2005 10:01 PM
Basic in 3d max rajat22 Tutorials 1 19-05-2005 12:59 PM
Visual Basic programming question sohummisra Programming 8 17-02-2005 11:17 AM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 02:59 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2