你的程序有些错误如string char[monnum]* = 不能用char 当变量名,因为它是C++中的关键字,也许你是想用char定义数组吧,但是char 是不能和string连用的,你学C++的话最好用string定义数组吧,还有 while (2); 也是不用写的,如果你加 while (2); 程序就不能终止了,也就是不会执行return 0;语句了。还有你不要在for()语句里定义sum = 0,不然你在for()循环完了,再输出sum 就错了,因为sum是在for()语句中定义的,只能在for()中使用不能在for()外面输出,我给改了程序如下,你看看怎么样:
#include
#include
using namespace std;
int main()
{
const int monnum = 12;
int sale [monnum];
int sum = 0;
// string smonth[]是用C++中的string 定义的字符串数组
//如果你想用C语言中的指针(呵呵,同样也是C++中的,只是在C++中少用了,大多都用string代替了),你就把 string smonth[]= 用 char *scmonth[]= 代替
string smonth[] =
// char *scmonth[] =
{
"Please enter the distribution records of January: ",
"Please enter the distribution records of February: ",
"Please enter the distribution records of March: ",
"Please enter the distribution records of April: ",
"Please enter the distribution records of May: ",
"Please enter the distribution records of June: ",
"Please enter the distribution records of July: ",
"Please enter the distribution records of August: ",
"Please enter the distribution records of September: ",
"Please enter the distribution records of October: ",
"Please enter the distribution records of November: ",
"Please enter the distribution records of December: "
};
for (int i = 0; i != 12; ++i)
{
cout << smonth[i]; // 你用char *scmonth[] 就把 cout << smonth[i];用cout << scmonth[i];代替
// cout << scmonth[i];
cin >> sale[i];
cout << endl;
sum += sale[i];
}
cout <<"a year sum is " << sum << endl; //输出sum
return 0;
}
//定义一个字符指针数组,相当于字符串数组
char * pchArray[monnum]; // pch 代码 point char 表示类型 ,
或者使用string类对象
string strOut[monnum]; 这样就oK了,这个就不需要指针了
#include
#include
int main()
{
using namespace std;
const int monnum = 12;
int sale [monnum];
string cha[monnum] =
{
"Please enter the distribution records of January: ",
"Please enter the distribution records of February: ",
"Please enter the distribution records of March: ",
"Please enter the distribution records of April: ",
"Please enter the distribution records of May: ",
"Please enter the distribution records of June: ",
"Please enter the distribution records of July: ",
"Please enter the distribution records of August: ",
"Please enter the distribution records of September: ",
"Please enter the distribution records of October: ",
"Please enter the distribution records of November: ",
"Please enter the distribution records of December: "
};
for (int i = 0, sum = 0; i < 12; ++i)
{
cout << cha[i];
cin >> sale[i];
cout << endl;
sum += sale[i];
}
return 0;
}
string char[monnum]* =
char是个关键字,不能用作变量名称
kankan...