PDA

View Full Version : سوال: cannot appear in a constant-expression (پیام خطا)



ahuray_mazdaa
یک شنبه 11 بهمن 1388, 04:24 صبح
سلام
در لینوکس بعد از کامپایل کد زیر به خطای زیر برخورد کردم

compiling C++ using -ansi -pedantic-errors -Wall
reza.cpp: In function 'int pos(char*)':
reza.cpp:14: error: 'dot' cannot appear in a constant-expression
reza.cpp:15: error: 'dot' cannot appear in a constant-expression
reza.cpp:21: error: 'dot' cannot appear in a constant-expression
reza.cpp:27: error: 'dot' cannot appear in a constant-expression



#include <iostream>

using namespace std;

int pos ( char ipaddr[15]) {
cin >> ipaddr;
int dot,i;
dot=0;
for (i=1;i<15 && dot < 3;i++)
{
if (ipaddr[i]==46) dot++;
int d1=0,d2=0,d3=0;
switch (dot)
{case (dot==0): break;
case (dot==1):{
if (!(i<=4)){
cout << "Wrong IP address format,Try again:";
pos(ipaddr);
d1=i;}
break;}
case (dot==2):{
if (!((i-d1)<=4)){
cout << "Wrong IP address format,Try again:";
pos(ipaddr);
d2=i;}
break;}
case (dot==3):{
if (!((i-d2)<=4)){
cout << "Wrong IP address format,Try again:";
pos(ipaddr);
d3=i;}
break;}
default :
if (!((i-d3)<=3)){
cout << "Wrong IP address format,Try again:";
pos(ipaddr);}}
}
if (!(dot==3))
{ cout << "Wrong IP address format,Try again:";
pos(ipaddr);}
i++;
return i;}

int main()
{
cout << "test" << "\n" << "\n";
cout << "How many files do you want?: ";
unsigned int number;
while ( ! (cin >> number))
{
// Enter this loop if input fails because of invalid data.
cout << "Hey, I said enter a number! Try again: ";
cin.clear (); // reset the "failure" flag
// The input "cursor" is still positioned at the beginning of
// the invalid input, so we need to skip past it.
cin.ignore (1000, '\n'); // Skip to next newline or 1000 chars,
}
cout << "What's your first IP address? :" ;
char ipaddress[15];
cout << pos (ipaddress);
}

amin1softco
یک شنبه 11 بهمن 1388, 08:41 صبح
اشتباه در نوشتن کیس ها:


#include <iostream>

using namespace std;

int pos ( char ipaddr[15]) {
cin >> ipaddr;
int dot,i;
dot=0;
for (i=1;i<15 && dot < 3;i++)
{
if (ipaddr[i]==46) dot++;
int d1=0,d2=0,d3=0;
switch (dot)
{case 0: break;
case 1:{
if (!(i<=4)){
cout << "Wrong IP address format,Try again:";
pos(ipaddr);
d1=i;}
break;}
case 2:{
if (!((i-d1)<=4)){
cout << "Wrong IP address format,Try again:";
pos(ipaddr);
d2=i;}
break;}
case 3:{
if (!((i-d2)<=4)){
cout << "Wrong IP address format,Try again:";
pos(ipaddr);
d3=i;}
break;}
default :
if (!((i-d3)<=3)){
cout << "Wrong IP address format,Try again:";
pos(ipaddr);}}
}
if (!(dot==3))
{ cout << "Wrong IP address format,Try again:";
pos(ipaddr);}
i++;
return i;}

int main()
{
cout << "test" << "\n" << "\n";
cout << "How many files do you want?: ";
unsigned int number;
while ( ! (cin >> number))
{
// Enter this loop if input fails because of invalid data.
cout << "Hey, I said enter a number! Try again: ";
cin.clear (); // reset the "failure" flag
// The input "cursor" is still positioned at the beginning of
// the invalid input, so we need to skip past it.
cin.ignore (1000, '\n'); // Skip to next newline or 1000 chars,
}
cout << "What's your first IP address? :" ;
char ipaddress[15];
cout << pos (ipaddress);
}

ahuray_mazdaa
یک شنبه 11 بهمن 1388, 12:19 عصر
دستتون درد نکنه..ممنون که به مبتدی ها کمک می کنید