(VC++) 如何定义字符串 char* 数组?

/***************编译器:Defaultcompiler执行g++.exe...g++.exe"F:\Temp\编程练习_05.04_用循环显示数据.cpp... /***************
编译器: Default compiler
执行 g++.exe...
g++.exe "F:\Temp\编程练习_05.04_用循环显示数据.cpp" -o "F:\Temp\编程练习_05.04_用循环显示数据.exe" -I"D:\Program Files (x86)\DEV-CPP\lib\gcc\mingw32\3.4.2\include" -I"D:\Program Files (x86)\DEV-CPP\include\c++\3.4.2\backward" -I"D:\Program Files (x86)\DEV-CPP\include\c++\3.4.2\mingw32" -I"D:\Program Files (x86)\DEV-CPP\include\c++\3.4.2" -I"D:\Program Files (x86)\DEV-CPP\include" -L"D:\Program Files (x86)\DEV-CPP\Lib"
F:\Temp\编程练习_05.04_用循环显示数据.cpp: In function `int main()':
F:\Temp\编程练习_05.04_用循环显示数据.cpp:15: error: expected primary-expression before "char"
F:\Temp\编程练习_05.04_用循环显示数据.cpp:15: error: expected `;' before "char"

F:\Temp\编程练习_05.04_用循环显示数据.cpp:30: error: expected unqualified-id before "int"
F:\Temp\编程练习_05.04_用循环显示数据.cpp:30: error: expected `,' or `;' before "int"
F:\Temp\编程练习_05.04_用循环显示数据.cpp:32: error: expected primary-expression before "char"
F:\Temp\编程练习_05.04_用循环显示数据.cpp:32: error: expected `;' before "char"
F:\Temp\编程练习_05.04_用循环显示数据.cpp:35: error: `sum' undeclared (first use this function)
F:\Temp\编程练习_05.04_用循环显示数据.cpp:35: error: (Each undeclared identifier is reported only once for each function it appears in.)

执行结束
*************************************************************************************************/

// 编程练习_05.04_用循环显示数据.cpp
/****************************** 第4题 **************************************
4.假设要销售 C++ For Fools 一书。请编写一个程序,输入全年中每个月的销售量
(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char* 数组
(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。
然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
*****************************************************************************/
#include <iostream>
#include <string>
int main()
{
using namespace std;
const int monnum = 12;
int sale [monnum];
string char[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, int sum = 0; i < 12; ++i)
{
cout << char[i]*;
cin >> sale[i];
cout << endl;
sum += sale[i];
}

while (2);
return 0;
}
展开
 我来答
1墨家巨子
2011-05-04 · TA获得超过475个赞
知道小有建树答主
回答量:110
采纳率:0%
帮助的人:103万
展开全部
你的程序有些错误如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 <iostream>
#include <string>

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;
}
鬼谷子1949
2011-05-04 · TA获得超过188个赞
知道答主
回答量:241
采纳率:0%
帮助的人:149万
展开全部
//定义一个字符指针数组,相当于字符串数组
char * pchArray[monnum]; // pch 代码 point char 表示类型 ,

或者使用string类对象
string strOut[monnum]; 这样就oK了,这个就不需要指针了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
北丹丹0cg
2011-05-04 · TA获得超过639个赞
知道小有建树答主
回答量:296
采纳率:0%
帮助的人:263万
展开全部
#include <iostream>
#include <string>
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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
nscboy
2011-05-04 · TA获得超过1054个赞
知道小有建树答主
回答量:826
采纳率:0%
帮助的人:550万
展开全部
string char[monnum]* =
char是个关键字,不能用作变量名称
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
一乐书I
2011-05-04
知道答主
回答量:29
采纳率:0%
帮助的人:11.5万
展开全部
kankan...
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式