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; }
最新回答
陌上柳絮倾城雪
2024-05-04 06:22:47
你的程序有些错误如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; }
#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]; }