Repost-Same code compiled sucessfully on a desktop but failed on a laptop  
Author Message
SamSam698





PostPosted: Visual C++ General, Repost-Same code compiled sucessfully on a desktop but failed on a laptop Top

Repost due to the display error.---

Below code can buit sucessfully on a DELL desktop but failed on a compaq laptop.The compiler said that snack 's size is unknow.

#include<iostream>
#include<string>


using std::cout;
using std::cin;
using std::endl;
using std::string;

struct CindyBar{

string brand;
float weight;
int calory;

};


int main(){

cout<<"Plesae enter the size to determine the array:\n";
int size;
(cin>>size).get();

CindyBar *snack=new CindyBar[size];


cout<<"Please enter the items one by one:\n";

for(int i=0;i<size;i++){
cout<<"Please enter the No."<<i+1<<" 's brand:\n";
getline(cin,snack[ i ].brand);
cout<<"Pleae enter the No."<<i+1<<" 's weight:\n";
(cin>>snack[ i ].weight).get();
cout<<"Please enter the No."<<i+1<<" 's calory:\n";
(cin>>snack[ i ].calory).get();

}

int i=0;
while(i!=size){
cout<<snack[ i ].brand<<"\t"<<snack[ i ].weight<<"\t"<<snack[ i ].calory<<endl;
i++;}

delete [ ]snack;
system("PAUSE");
return 0;
}

is there anybody would like to help me

Thanks.

Regards,




Visual C++1  
 
 
cgraus





PostPosted: Visual C++ General, Repost-Same code compiled sucessfully on a desktop but failed on a laptop Top

You should post the actual error. It comes with a number that we can look up to see an example of what the specific problem is. The brand of PC has nothing to do with it, what compilers are you using Are they exactly the same



 
 
SamSam698





PostPosted: Visual C++ General, Repost-Same code compiled sucessfully on a desktop but failed on a laptop Top

Hi there,

1.The compiler on both pcs are the same :VC++2005 Express;

2.The error code is as below:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'snack' : unknown size

3.Thanks for your patience on my wrong post .

Thanks for your help.

Regards,



 
 
cgraus





PostPosted: Visual C++ General, Repost-Same code compiled sucessfully on a desktop but failed on a laptop Top

And it's physically the same size, no chance of a typo Those errors are all on the line that declares the array



 
 
Martin Richter





PostPosted: Visual C++ General, Repost-Same code compiled sucessfully on a desktop but failed on a laptop Top

On what lines did this errors occur

 
 
Viorel.





PostPosted: Visual C++ General, Repost-Same code compiled sucessfully on a desktop but failed on a laptop Top

I think the problem appears in your erroneous program in a line that looks like this:

CindyBar snack[size];

This fragment really gives mentioned error. (But we do not see such line in your original sample).


 
 
SamSam698





PostPosted: Visual C++ General, Repost-Same code compiled sucessfully on a desktop but failed on a laptop Top

Hi All,

The problem has been fixed and Thanks so much for all your help!!

Regards,